引用
1 :关注要点,为什么在有synchroniezed方法的同时会出现 Collections.synchronizedList
2 :知识背景: 您可能需要了解java Synchronized方法的加锁的各种机制,包括如何上锁,锁对象
3 : plus: 您需要不断的深化 Java加锁的各种机制
Java代码
1.@NotThreadSafe
2.class BadListHelper <E> {
3. public List<E> list = Collections.synchronizedList(new ArrayList<E>());
4.
5. public synchronized boolean putIfAbsent(E x) {
6. boolean absent = !list.contains(x);
7. if (absent)
8. list.add(x);
9. return absent;
10. }
11.}
这个示例希望实现的功能是为List提供一个原子操作:若没有则添加。因为ArrayList本身不是线程安全的,所以通过集合Collections.synchronizedList将其转换为一个线程安全的类,然后通过一个辅助的方法来为List实现这么个功能。初看起来这个方法没问题,因为也添加了synchronized关键字实现加锁了。
但是仔细分析,你会发现问题。首先对于synchronized关键字,需要说明的是,它是基于当前的对象来加锁的,上面的方法也可以这样写:
Java代码 收藏代码
1.public boolean putIfAbsent(E x) {
2. synchronized(this) {
3. boolean absent = !list.contains(x);
4. if (absent)
5. list.add(x);
6. return absent;
7. }
8.}
所以这里的锁其实是BadListHelper对象, 而可以肯定的是Collections.synchronizedList返回的线程安全的List内部使用的锁绝对不是BadListHelper的对象,应为你在声明和初始化这个集合的过程之中,你尚且都不知道这个对象的存在。所以BadListHelper中的putIfAbsent方法和线程安全的List使用的不是同一个锁,因此上面的这个加了synchronized关键字的方法依然不能实现线程安全性。
下面给出书中的另一种正确的实现:
Java代码 收藏代码
1.@ThreadSafe
2.class GoodListHelper <E> {
3. public List<E> list = Collections.synchronizedList(new ArrayList<E>());
4.
5. public boolean putIfAbsent(E x) {
6. synchronized (list) {
7. boolean absent = !list.contains(x);
8. if (absent)
9. list.add(x);
10. return absent;
11. }
12. }
13.}
如果你要分析这个实现是否正确,你需要搞清楚Collections.synchronizedList返回的线程安全的List内部使用的锁是哪个对象,所以你得看看Collections.synchronizedList这个方法的源码了。该方法源码如下:
Java代码 收藏代码
1.public static <T> List<T> synchronizedList(List<T> list) {
2. return (list instanceof RandomAccess ?
3. new SynchronizedRandomAccessList<T>(list) :
4. new SynchronizedList<T>(list));
5. }
通过源码,我们还需要知道ArrayList是否实现了RandomAccess接口:
Java代码 收藏代码
1.public class ArrayList<E> extends AbstractList<E>
2. implements List<E>, RandomAccess, Cloneable, java.io.Serializable
查看ArrayList的源码,可以看到它实现了RandomAccess,所以上面的synchronizedList放回的应该是SynchronizedRandomAccessList的实例。接下来看看SynchronizedRandomAccessList这个类的实现:
Java代码 收藏代码
1.static class SynchronizedRandomAccessList<E>
2. extends SynchronizedList<E>
3. implements RandomAccess {
4.
5. SynchronizedRandomAccessList(List<E> list) {
6. super(list);
7. }
8.
9. SynchronizedRandomAccessList(List<E> list, Object mutex) {
10. super(list, mutex);
11. }
12.
13. public List<E> subList(int fromIndex, int toIndex) {
14. synchronized(mutex) {
15. return new SynchronizedRandomAccessList<E>(
16. list.subList(fromIndex, toIndex), mutex);
17. }
18. }
19.
20. static final long serialVersionUID = 1530674583602358482L;
21.
22. /**
23. * Allows instances to be deserialized in pre-1.4 JREs (which do
24. * not have SynchronizedRandomAccessList). SynchronizedList has
25. * a readResolve method that inverts this transformation upon
26. * deserialization.
27. */
28. private Object writeReplace() {
29. return new SynchronizedList<E>(list);
30. }
31. }
因为SynchronizedRandomAccessList这个类继承自SynchronizedList,而大部分方法都在SynchronizedList中实现了,所以源码中只包含了很少的方法,但是通过subList方法,我们可以看到这里使用的锁对象为mutex对象,而mutex是在SynchronizedCollection类中定义的,所以再看看SynchronizedCollection这个类中关于mutex的定义部分源码:
Java代码 收藏代码
1.static class SynchronizedCollection<E> implements Collection<E>, Serializable {
2. // use serialVersionUID from JDK 1.2.2 for interoperability
3. private static final long serialVersionUID = 3053995032091335093L;
4.
5. final Collection<E> c; // Backing Collection
6. final Object mutex; // Object on which to synchronize
7.
8. SynchronizedCollection(Collection<E> c) {
9. if (c==null)
10. throw new NullPointerException();
11. this.c = c;
12. mutex = this;
13. }
14. SynchronizedCollection(Collection<E> c, Object mutex) {
15. this.c = c;
16. this.mutex = mutex;
17. }
18.}
可以看到mutex就是当前的SynchronizedCollection对象,而SynchronizedRandomAccessList继承自SynchronizedList,SynchronizedList又继承自SynchronizedCollection,所以SynchronizedRandomAccessList中的mutex也就是SynchronizedRandomAccessList的this对象。所以在GoodListHelper中使用的锁list对象,和SynchronizedRandomAccessList内部的锁是一致的,所以它可以实现线程安全性。