Java的ConcurrentHashMap如何使用

這篇文章主要介紹了Java的ConcurrentHashMap如何使用的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇Java的ConcurrentHashMap如何使用文章都會有所收獲,下面我們一起來看看吧。

成都創(chuàng)新互聯(lián)公司2013年成立,是專業(yè)互聯(lián)網技術服務公司,擁有項目成都網站建設、網站制作網站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元龍?zhí)蹲鼍W站,已為上家服務,為龍?zhí)陡鞯仄髽I(yè)和個人服務,聯(lián)系電話:028-86922220

說明

1、ConcurentHashMap結合了HashMap和Hashtable的優(yōu)點。HashMap不考慮同步,Hashtable考慮同步。但是Hashtable每次同步都要鎖定整個結構。

2、ConcurentHashMap鎖的方式是稍微細粒度的。ConcurentHashMap將hash表分成16桶(默認值),如get、put、remove等常用操作只鎖定目前需要的桶。

實例

/**
     * Creates a new, empty map with the default initial table size (16).
     */
    public ConcurrentHashMap() {
    }
 
    /**
     * Creates a new, empty map with an initial table size
     * accommodating the specified number of elements without the need
     * to dynamically resize.
     *
     * @param initialCapacity The implementation performs internal
     * sizing to accommodate this many elements.
     * @throws IllegalArgumentException if the initial capacity of
     * elements is negative
     */
    public ConcurrentHashMap(int initialCapacity) {
        if (initialCapacity < 0)
            throw new IllegalArgumentException();
        int cap = ((initialCapacity >= (MAXIMUM_CAPACITY >>> 1)) ?
                   MAXIMUM_CAPACITY :
                   tableSizeFor(initialCapacity + (initialCapacity >>> 1) + 1));
        this.sizeCtl = cap;
    }
 
    /**
     * Creates a new map with the same mappings as the given map.
     *
     * @param m the map
     */
    public ConcurrentHashMap(Map<? extends K, ? extends V> m) {
        this.sizeCtl = DEFAULT_CAPACITY;
        putAll(m);
    }

關于“Java的ConcurrentHashMap如何使用”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“Java的ConcurrentHashMap如何使用”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

網站名稱:Java的ConcurrentHashMap如何使用
網頁鏈接:http://muchs.cn/article10/ihsodo.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供網站設計、靜態(tài)網站、網站內鏈、動態(tài)網站、定制開發(fā)網站改版

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

綿陽服務器托管