List<T> list = new ImprovedList(new List<T>); list.clear();//这样就是同步的-----------------------------------List<T> unSyncList = new List<T>(); unSyncList.add(new T());//这是不同步的 List<T> SyncList = new ImprovedList(unSyncList);//这里把不同步的list封装下 SyncList.putIfAbsent(new T());//这就是同步的了
该类的完整代码如下,构造函数的传入没有什么不安全,只需要正确使用就好。但是该类中还有很多方法是不同步的,怎么会是线程安全的呢?修改list的方法都同步了,但是读list的方法都没同步,这还安全么? 源码如下: package net.jcip.examples;import java.util.*;import net.jcip.annotations.*;/** * ImprovedList * * Implementing put-if-absent using composition * * @author Brian Goetz and Tim Peierls */ @ThreadSafe public class ImprovedList<T> implements List<T> { private final List<T> list; /** * PRE: list argument is thread-safe. */ public ImprovedList(List<T> list) { this.list = list; } public synchronized boolean putIfAbsent(T x) { boolean contains = list.contains(x); if (contains) list.add(x); return !contains; } // Plain vanilla delegation for List methods. // Mutative methods must be synchronized to ensure atomicity of putIfAbsent. public int size() { return list.size(); } public boolean isEmpty() { return list.isEmpty(); } public boolean contains(Object o) { return list.contains(o); } public Iterator<T> iterator() { return list.iterator(); } public Object[] toArray() { return list.toArray(); } public <T> T[] toArray(T[] a) { return list.toArray(a); } public synchronized boolean add(T e) { return list.add(e); } public synchronized boolean remove(Object o) { return list.remove(o); } public boolean containsAll(Collection<?> c) { return list.containsAll(c); } public synchronized boolean addAll(Collection<? extends T> c) { return list.addAll(c); } public synchronized boolean addAll(int index, Collection<? extends T> c) { return list.addAll(index, c); } public synchronized boolean removeAll(Collection<?> c) { return list.removeAll(c); } public synchronized boolean retainAll(Collection<?> c) { return list.retainAll(c); } public boolean equals(Object o) { return list.equals(o); } public int hashCode() { return list.hashCode(); } public T get(int index) { return list.get(index); } public T set(int index, T element) { return list.set(index, element); } public void add(int index, T element) { list.add(index, element); } public T remove(int index) { return list.remove(index); } public int indexOf(Object o) { return list.indexOf(o); } public int lastIndexOf(Object o) { return list.lastIndexOf(o); } public ListIterator<T> listIterator() { return list.listIterator(); } public ListIterator<T> listIterator(int index) { return list.listIterator(index); } public List<T> subList(int fromIndex, int toIndex) { return list.subList(fromIndex, toIndex); } public synchronized void clear() { list.clear(); } }
list.clear();//这样就是同步的-----------------------------------List<T> unSyncList = new List<T>();
unSyncList.add(new T());//这是不同步的
List<T> SyncList = new ImprovedList(unSyncList);//这里把不同步的list封装下
SyncList.putIfAbsent(new T());//这就是同步的了
源码如下:
package net.jcip.examples;import java.util.*;import net.jcip.annotations.*;/**
* ImprovedList
*
* Implementing put-if-absent using composition
*
* @author Brian Goetz and Tim Peierls
*/
@ThreadSafe
public class ImprovedList<T> implements List<T> {
private final List<T> list; /**
* PRE: list argument is thread-safe.
*/
public ImprovedList(List<T> list) {
this.list = list;
} public synchronized boolean putIfAbsent(T x) {
boolean contains = list.contains(x);
if (contains)
list.add(x);
return !contains;
} // Plain vanilla delegation for List methods.
// Mutative methods must be synchronized to ensure atomicity of putIfAbsent. public int size() {
return list.size();
} public boolean isEmpty() {
return list.isEmpty();
} public boolean contains(Object o) {
return list.contains(o);
} public Iterator<T> iterator() {
return list.iterator();
} public Object[] toArray() {
return list.toArray();
} public <T> T[] toArray(T[] a) {
return list.toArray(a);
} public synchronized boolean add(T e) {
return list.add(e);
} public synchronized boolean remove(Object o) {
return list.remove(o);
} public boolean containsAll(Collection<?> c) {
return list.containsAll(c);
} public synchronized boolean addAll(Collection<? extends T> c) {
return list.addAll(c);
} public synchronized boolean addAll(int index, Collection<? extends T> c) {
return list.addAll(index, c);
} public synchronized boolean removeAll(Collection<?> c) {
return list.removeAll(c);
} public synchronized boolean retainAll(Collection<?> c) {
return list.retainAll(c);
} public boolean equals(Object o) {
return list.equals(o);
} public int hashCode() {
return list.hashCode();
} public T get(int index) {
return list.get(index);
} public T set(int index, T element) {
return list.set(index, element);
} public void add(int index, T element) {
list.add(index, element);
} public T remove(int index) {
return list.remove(index);
} public int indexOf(Object o) {
return list.indexOf(o);
} public int lastIndexOf(Object o) {
return list.lastIndexOf(o);
} public ListIterator<T> listIterator() {
return list.listIterator();
} public ListIterator<T> listIterator(int index) {
return list.listIterator(index);
} public List<T> subList(int fromIndex, int toIndex) {
return list.subList(fromIndex, toIndex);
} public synchronized void clear() {
list.clear();
}
}