這篇“JDK中Collections的線程安全怎么實(shí)現(xiàn)”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來(lái)看看這篇“JDK中Collections的線程安全怎么實(shí)現(xiàn)”文章吧。
創(chuàng)新互聯(lián)建站專(zhuān)業(yè)為企業(yè)提供臨清網(wǎng)站建設(shè)、臨清做網(wǎng)站、臨清網(wǎng)站設(shè)計(jì)、臨清網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、臨清企業(yè)網(wǎng)站模板建站服務(wù),十余年臨清做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
方法:
public static
return new SynchronizedSet
}
static class SynchronizedSet
extends SynchronizedCollection
implements Set
private static final long serialVersionUID = 487447009682186044L;
SynchronizedSet(Set
super(s);
}
SynchronizedSet(Set
super(s, mutex);
}
public boolean equals(Object o) {
synchronized(mutex) {return c.equals(o);}
}
public int hashCode() {
synchronized(mutex) {return c.hashCode();}
}
}
// use serialVersionUID from JDK 1.2.2 for interoperability
private static final long serialVersionUID = 3053995032091335093L;
final Collection
final Object mutex; // Object on which to synchronize
SynchronizedCollection(Collection
if (c==null)
throw new NullPointerException();
this.c = c;
mutex = this;
}
SynchronizedCollection(Collection
this.c = c;
this.mutex = mutex;
}
public int size() {
synchronized(mutex) {return c.size();}
}
public boolean isEmpty() {
synchronized(mutex) {return c.isEmpty();}
}
public boolean contains(Object o) {
synchronized(mutex) {return c.contains(o);}
}
public Object[] toArray() {
synchronized(mutex) {return c.toArray();}
}
public
synchronized(mutex) {return c.toArray(a);}
}
public Iterator
return c.iterator(); // Must be manually synched by user!
}
public boolean add(E e) {
synchronized(mutex) {return c.add(e);}
}
public boolean remove(Object o) {
synchronized(mutex) {return c.remove(o);}
}
public boolean containsAll(Collection> coll) {
synchronized(mutex) {return c.containsAll(coll);}
}
public boolean addAll(Collection extends E> coll) {
synchronized(mutex) {return c.addAll(coll);}
}
public boolean removeAll(Collection> coll) {
synchronized(mutex) {return c.removeAll(coll);}
}
public boolean retainAll(Collection> coll) {
synchronized(mutex) {return c.retainAll(coll);}
}
public void clear() {
synchronized(mutex) {c.clear();}
}
public String toString() {
synchronized(mutex) {return c.toString();}
}
private void writeObject(ObjectOutputStream s) throws IOException {
synchronized(mutex) {s.defaultWriteObject();}
}
}
List和Map方法同理,這樣,我們利用了裝實(shí)模式,給我們的Map和List穿上了交通協(xié)管員的制服,減少了類(lèi)爆炸,這就是裝實(shí)模式;
package org;
public class AImp implements IA {
public synchronized void say() {
;
}
}
package org;
public interface IA {
public void say();
}
sychronized標(biāo)識(shí)符是不影響接口的實(shí)現(xiàn)和繼承的
以上就是關(guān)于“JDK中Collections的線程安全怎么實(shí)現(xiàn)”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。