国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

集合類型的并發(fā)

2019-11-08 01:50:41
字體:
來源:轉載
供稿:網(wǎng)友

http://topmanopensource.iteye.com/blog/1738178

@NotThreadSafe  

class BadListHelper <E> {  

    public List<E> list = Collections.synchronizedList(new ArrayList<E>());  

  

    public synchronized boolean putIfAbsent(E x) {  

        boolean absent = !list.contains(x);  

        if (absent)  

            list.add(x);  

        return absent;  

    }  

}  

 

  這個示例希望實現(xiàn)的功能是為List提供一個原子操作:若沒有則添加。因為ArrayList本身不是線程安全的,所以通過集合Collections.synchronizedList將其轉換為一個線程安全的類,然后通過一個輔助的方法來為List實現(xiàn)這么個功能。初看起來這個方法沒問題,因為也添加了synchronized關鍵字實現(xiàn)加鎖了。

 

但是仔細分析,你會發(fā)現(xiàn)問題。首先對于synchronized關鍵字,需要說明的是,它是基于當前的對象來加鎖的,上面的方法也可以這樣寫:

 

java代碼  收藏代碼

public boolean putIfAbsent(E x) {  

    synchronized(this) {  

        boolean absent = !list.contains(x);  

        if (absent)  

            list.add(x);  

        return absent;  

    }  

}  

 

  所以這里的鎖其實是BadListHelper對象, 而可以肯定的是Collections.synchronizedList返回的線程安全的List內部使用的鎖絕對不是BadListHelper的對象,應為你在聲明和初始化這個集合的過程之中,你尚且都不知道這個對象的存在。所以BadListHelper中的putIfAbsent方法和線程安全的List使用的不是同一個鎖,因此上面的這個加了synchronized關鍵字的方法依然不能實現(xiàn)線程安全性。

 

下面給出書中的另一種正確的實現(xiàn):

 

 

Java代碼  收藏代碼

@ThreadSafe  

class GoodListHelper <E> {  

    public List<E> list = Collections.synchronizedList(new ArrayList<E>());  

  

    public boolean putIfAbsent(E x) {  

        synchronized (list) {  

            boolean absent = !list.contains(x);  

            if (absent)  

                list.add(x);  

            return absent;  

        }  

    }  

}  

 

  如果你要分析這個實現(xiàn)是否正確,你需要搞清楚Collections.synchronizedList返回的線程安全的List內部使用的鎖是哪個對象,所以你得看看Collections.synchronizedList這個方法的源碼了。該方法源碼如下:

 

Java代碼  收藏代碼

public static <T> List<T> synchronizedList(List<T> list) {  

    return (list instanceof Randomaccess ?  

                new SynchronizedRandomAccessList<T>(list) :  

                new SynchronizedList<T>(list));  

    }  

 

 

通過源碼,我們還需要知道ArrayList是否實現(xiàn)了RandomAccess接口:

 

Java代碼  收藏代碼

public class ArrayList<E> extends AbstractList<E>  

        implements List<E>, RandomAccess, Cloneable, java.io.Serializable  

 

  查看ArrayList的源碼,可以看到它實現(xiàn)了RandomAccess,所以上面的synchronizedList放回的應該是SynchronizedRandomAccessList的實例。接下來看看SynchronizedRandomAccessList這個類的實現(xiàn):

 

 

Java代碼  收藏代碼

static class SynchronizedRandomAccessList<E>  

    extends SynchronizedList<E>  

    implements RandomAccess {  

  

        SynchronizedRandomAccessList(List<E> list) {  

            super(list);  

        }  

  

    SynchronizedRandomAccessList(List<E> list, Object mutex) {  

            super(list, mutex);  

        }  

  

    public List<E> subList(int fromIndex, int toIndex) {  

        synchronized(mutex) {  

                return new SynchronizedRandomAccessList<E>(  

                    list.subList(fromIndex, toIndex), mutex);  

            }  

        }  

  

        static final long serialVersionUID = 1530674583602358482L;  

  

        /** 

         * Allows instances to be deserialized in PRe-1.4 JREs (which do 

         * not have SynchronizedRandomAccessList).  SynchronizedList has 

         * a readResolve method that inverts this transformation upon 

         * deserialization. 

         */  

        private Object writeReplace() {  

            return new SynchronizedList<E>(list);  

        }  

    }  

 

因為SynchronizedRandomAccessList這個類繼承自SynchronizedList,而大部分方法都在SynchronizedList中實現(xiàn)了,所以源碼中只包含了很少的方法,但是通過subList方法,我們可以看到這里使用的鎖對象為mutex對象,而mutex是在SynchronizedCollection類中定義的,所以再看看SynchronizedCollection這個類中關于mutex的定義部分源碼:

Java代碼  收藏代碼

static class SynchronizedCollection<E> implements Collection<E>, Serializable {  

    // use serialVersionUID from JDK 1.2.2 for interOperability  

    private static final long serialVersionUID = 3053995032091335093L;  

  

    final Collection<E> c;  // Backing Collection  

    final Object mutex;     // Object on which to synchronize  

  

    SynchronizedCollection(Collection<E> c) {  

            if (c==null)  

                throw new NullPointerException();  

        this.c = c;  

            mutex = this;  

        }  

    SynchronizedCollection(Collection<E> c, Object mutex) {  

        this.c = c;  

            this.mutex = mutex;  

        }  

}  

  可以看到mutex就是當前的SynchronizedCollection對象,而SynchronizedRandomAccessList繼承自SynchronizedList,SynchronizedList又繼承自SynchronizedCollection,所以SynchronizedRandomAccessList中的mutex也就是SynchronizedRandomAccessList的this對象。所以在GoodListHelper中使用的鎖list對象,和SynchronizedRandomAccessList內部的鎖是一致的,所以它可以實現(xiàn)線程安全性。


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 芒康县| 丘北县| 丰原市| 天气| 铜山县| 昌都县| 犍为县| 桐柏县| 靖西县| 井研县| 弋阳县| 铁岭县| 石林| 甘洛县| 石阡县| 元氏县| 南华县| 博兴县| 阿拉善盟| 枣强县| 桑日县| 和顺县| 遵义县| 南召县| 明光市| 鹰潭市| 邓州市| 克山县| 鸡西市| 商都县| 娄烦县| 炉霍县| 额济纳旗| 镇宁| 泌阳县| 耒阳市| 科尔| 秦安县| 云林县| 德阳市| 蕉岭县|