
{"id":1252,"date":"2021-02-01T12:37:31","date_gmt":"2021-02-01T04:37:31","guid":{"rendered":"https:\/\/www.changxuan.top\/?p=1252"},"modified":"2021-04-15T17:02:37","modified_gmt":"2021-04-15T09:02:37","slug":"%e5%89%96%e6%9e%90-copyonwritearraylist","status":"publish","type":"post","link":"https:\/\/www.changxuan.top\/?p=1252","title":{"rendered":"\u5256\u6790 CopyOnWriteArrayList"},"content":{"rendered":"\n<p><code>CopyOnWriteArrayList<\/code> \u662f JUC \u4e2d\u552f\u4e00\u4e00\u4e2a\u652f\u6301\u5e76\u53d1\u7684 List\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>CopyOnWriteArrayList \u7684\u4fee\u6539\u64cd\u4f5c\u90fd\u662f\u5728\u5e95\u5c42\u7684\u4e00\u4e2a\u590d\u5236\u7684\u6570\u7ec4\u4e0a\u8fdb\u884c\uff0c\u5373\u5199\u65f6\u590d\u5236\u7b56\u7565\uff0c\u4ece\u800c\u5b9e\u73b0\u4e86\u7ebf\u7a0b\u5b89\u5168\u3002\u5176\u5b9e\u539f\u7406\u548c\u6570\u636e\u5e93\u7684\u8bfb\u5199\u5206\u79bb\u5341\u5206\u76f8\u4f3c\u3002<\/p><\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">\u57fa\u672c\u6784\u6210<\/h3>\n\n\n\n<p>\u5e95\u5c42\u4f7f\u7528\u6570\u7ec4 <code>private transient volatile Object[] array;<\/code> \u6765\u5b58\u50a8\u5143\u7d20\uff0c\u4f7f\u7528 <code>ReentrantLock<\/code> \u72ec\u5360\u9501\u4fdd\u8bc1\u76f8\u5173\u64cd\u4f5c\u7684\u5b89\u5168\u6027\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u6784\u9020\u51fd\u6570<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public CopyOnWriteArrayList() {\n        setArray(new Object[0]);\n}\n\/\/ \u5c06\u96c6\u5408 c \u5185\u7684\u5143\u7d20\u590d\u5236\u5230 list \u4e2d\npublic CopyOnWriteArrayList(Collection&amp;lt;? extends E&amp;gt; c) {\n        Object[] elements;\n        if (c.getClass() == CopyOnWriteArrayList.class)\n            elements = ((CopyOnWriteArrayList&amp;lt;?&amp;gt;)c).getArray();\n        else {\n            elements = c.toArray();\n            \/\/ c.toArray might (incorrectly) not return Object[] (see 6260652)\n            if (elements.getClass() != Object[].class)\n                elements = Arrays.copyOf(elements, elements.length, Object[].class);\n        }\n        setArray(elements);\n}\n\/\/ \u521b\u5efa\u4e00\u4e2a\u5185\u90e8\u5143\u7d20\u662f toCopyIn \u526f\u672c\u7684 list\npublic CopyOnWriteArrayList(E[] toCopyIn) {\n        setArray(Arrays.copyOf(toCopyIn, toCopyIn.length, Object[].class));\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u6dfb\u52a0\u5143\u7d20<\/h3>\n\n\n\n<p><code>CopyOnWriteArrayList<\/code> \u4e2d\u4e0e\u6dfb\u52a0\u5143\u7d20\u76f8\u5173\u7684\u65b9\u6cd5\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>add(E e)<\/li><li>add(int index, E element)<\/li><li>addAll(Collection c)<\/li><li>addAll(int index, Collection c)<\/li><li>addIfAbsent(E e)<\/li><li>addAllAbsent(Collection c)<\/li><\/ul>\n\n\n\n<p>\u9274\u4e8e\u539f\u7406\u57fa\u672c\u76f8\u4f3c\uff0c\u4e0b\u9762\u53ea\u5206\u6790 <code>add(E e)<\/code> \u548c <code>addIfAbsent(E e)<\/code> \u65b9\u6cd5\u505a\u4e3a\u4f8b\u5b50\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">add(E e)<\/h4>\n\n\n\n<p><strong>\u6e90\u7801<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public boolean add(E e) {\n    \/\/ \u83b7\u53d6\u975e\u516c\u5e73\u7684\u72ec\u5360\u9501\n    final ReentrantLock lock = this.lock;\n    lock.lock();\n    try {\n        \/\/ \u83b7\u53d6\u5f53\u524d\u6570\u7ec4\n        Object[] elements = getArray();\n        int len = elements.length;\n        \/\/ \u5c06\u5f53\u524d\u6570\u7ec4\u5143\u7d20\u62f7\u8d1d\u81f3\u65b0\u6570\u7ec4\n        Object[] newElements = Arrays.copyOf(elements, len + 1);\n        newElements[len] = e;\n        \/\/ \u4f7f\u7528\u65b0\u6570\u7ec4\u66ff\u6362\u539f\u6709\u6570\u7ec4\n        setArray(newElements);\n        return true;\n    } finally {\n        \/\/ \u91ca\u653e\u72ec\u5360\u9501\n        lock.unlock();\n    }\n}<\/pre>\n\n\n\n<p>\u8fdb\u5165 <code>add<\/code> \u65b9\u6cd5\u7684\u7ebf\u7a0b\u9996\u5148\u4f1a\u53bb\u5c1d\u8bd5\u83b7\u53d6\u72ec\u5360\u9501\uff0c\u6210\u529f\u83b7\u53d6\u7684\u7ebf\u7a0b\u4f1a\u7ee7\u7eed\u6267\u884c\u540e\u7eed\u6dfb\u52a0\u5143\u7d20\u903b\u8f91\uff0c\u800c\u672a\u83b7\u53d6\u72ec\u5360\u9501\u7684\u7ebf\u7a0b\u5728\u6ca1\u6709\u5f02\u5e38\u7684\u60c5\u51b5\u4e0b\u5219\u4f1a\u963b\u585e\u6302\u8d77\u3002\u7b49\u5f85\u72ec\u5360\u9501\u88ab\u91ca\u653e\u540e\uff0c\u518d\u6b21\u5c1d\u8bd5\u83b7\u53d6\u3002\uff08ps. \u5728 CopyOnWriteArrayList \u4e2d\u4f7f\u7528\u7684\u662f ReentrantLock \u7684\u975e\u516c\u5e73\u9501\u6a21\u5f0f\uff09<\/p>\n\n\n\n<p>\u8fd9\u6837\u5c31\u80fd\u4fdd\u8bc1\uff0c\u540c\u4e00\u65f6\u95f4\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u8fdb\u884c\u6dfb\u52a0\u5143\u7d20\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">addIfAbsent(E e)<\/h4>\n\n\n\n<p><strong>\u6e90\u7801<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public boolean addIfAbsent(E e) {\n    \/\/ \u83b7\u53d6\u5f53\u524d\u6570\u7ec4\n    Object[] snapshot = getArray();\n    \/\/ \u8c03\u7528 indexOf \u5224\u65ad\u5143\u7d20\u662f\u5426\u5df2\u5b58\u5728 \uff08\u904d\u5386\uff09\n    return indexOf(e, snapshot, 0, snapshot.length) &amp;gt;= 0 ? false :\n        addIfAbsent(e, snapshot);\n}\n\nprivate boolean addIfAbsent(E e, Object[] snapshot) {\n    \/\/ \u83b7\u53d6\u72ec\u5360\u9501\n    final ReentrantLock lock = this.lock;\n    lock.lock();\n    try {\n        \/\/ \u518d\u6b21\u83b7\u53d6\u5f53\u524d\u6570\u7ec4\n        Object[] current = getArray();\n        int len = current.length;\n        \/\/ \u6b64\u5904\u5224\u65ad\u662f\u68c0\u67e5\u5728\u5f53\u524d\u7ebf\u7a0b\u5224\u65ad\u5143\u7d20\u4e0d\u5b58\u5728\u548c\u83b7\u53d6\u72ec\u5360\u9501\u4e4b\u95f4\u7684\u8fd9\u6bb5\u65f6\u95f4\u5185\u662f\u5426\u6709\u5176\u5b83\u7ebf\u7a0b\u5bf9\u6570\u7ec4\u8fdb\u884c\u4e86\u66f4\u6539\u64cd\u4f5c\n        if (snapshot != current) {\n            \/\/ Optimize for lost race to another addXXX operation\n            int common = Math.min(snapshot.length, len);\n            for (int i = 0; i &amp;lt; common; i++)\n                if (current[i] != snapshot[i] &amp;amp;&amp;amp; eq(e, current[i]))\n                    return false;\n            if (indexOf(e, current, common, len) &amp;gt;= 0)\n                    return false;\n        }\n        \/\/ \u4e0e add \u65b9\u6cd5\u7684\u903b\u8f91\u76f8\u540c\n        Object[] newElements = Arrays.copyOf(current, len + 1);\n        newElements[len] = e;\n        setArray(newElements);\n        return true;\n    } finally {\n        lock.unlock();\n    }\n}<\/pre>\n\n\n\n<p>\u5982\u679c\u5f53\u524d\u6570\u7ec4\u4e2d\u4e0d\u5b58\u5728\u5143\u7d20 <code>e<\/code>\uff0c<code>addIfAbsent<\/code> \u5219\u4f1a\u5c06 <code>e<\/code> \u6dfb\u52a0\u81f3\u6570\u7ec4\u4e2d\u8fd4\u56de <code>true<\/code>\uff1b\u5982\u679c\u5f53\u524d\u6570\u7ec4\u4e2d\u5b58\u5728\u5f85\u6dfb\u52a0\u5143\u7d20\uff0c\u5219\u4f1a\u8fd4\u56de <code>false<\/code>\u3002<\/p>\n\n\n\n<p><strong>\u5728 <code>addIfAbsent<\/code> \u65b9\u6cd5\u4e2d\u4e3a\u4e86\u63d0\u9ad8\u6027\u80fd\uff0c\u8bbe\u8ba1\u8005\u628a\u5224\u65ad\u201c\u5f53\u524d\u6570\u7ec4\u662f\u5426\u5b58\u5728\u5f85\u6dfb\u52a0\u5143\u7d20\u201d\u548c\u201c\u6dfb\u52a0\u5143\u7d20\u201d\u7684\u64cd\u4f5c\u5206\u5f00\u4e86<\/strong>\u3002\u7531\u4e8e\u524d\u4e00\u4e2a\u64cd\u4f5c\u4e0d\u5fc5\u8981\u83b7\u53d6\u72ec\u5360\u9501\uff0c\u5728\u9047\u5230\u6bcf\u6b21\u5f85\u6dfb\u52a0\u7684\u5143\u7d20\u90fd\u5df2\u7ecf\u5b58\u5728\u4e8e\u6570\u7ec4\u7684\u60c5\u51b5\u65f6\u53ef\u4ee5\u9ad8\u6548\u7684\u8fd4\u56de <code>false<\/code> \u3002<\/p>\n\n\n\n<p>\u56e0\u4e3a\u4e0a\u9762\u63d0\u5230\u7684\u4e24\u6b65\u64cd\u4f5c\u662f\u975e\u539f\u5b50\u6027\u7684\uff0c\u6240\u4ee5\u518d\u7b2c\u4e8c\u6b65\u64cd\u4f5c\u4e2d\u8fd8\u9700\u8981\u518d\u6b21\u8fdb\u884c\u786e\u8ba4\u4e4b\u524d\u7528\u6765\u5224\u65ad\u4e0d\u5b58\u5728\u5143\u7d20 <code>e<\/code> \u7684\u6570\u7ec4\u662f\u5426\u88ab\u201c\u6389\u5305\u201d\u4e86\u3002\u5982\u679c\u88ab\u201c\u6389\u5305\u201d\uff0c\u90a3\u4e48\u4e5f\u4e0d\u8981\u201c\u5acc\u5f03\u201d\u3002\u5c31\u9700\u8981\u518d\u5224\u65ad\u4e00\u4e0b\u201c\u6389\u5305\u201d\u540e\u7684\u6570\u7ec4\u8fd8\u80fd\u4e0d\u80fd\u63a5\u7740\u7528\u3002\u5982\u679c\u4e0d\u80fd\u7528\u76f4\u63a5\u8fd4\u56de <code>false<\/code>\uff0c\u5982\u679c\u53d1\u73b0\u80fd\u7528\u5c31\u7ee7\u7eed\u5411\u4e0b\u6267\u884c\uff0c\u6210\u529f\u540e\u8fd4\u56de <code>true<\/code> \u3002<\/p>\n\n\n\n<p>\u8fd9\u79cd\u8bbe\u8ba1\u601d\u8def\uff0c\u5728\u81ea\u5df1\u7684\u4e1a\u52a1\u7cfb\u7edf\u4e2d\u8fd8\u662f\u6bd4\u8f83\u503c\u7684\u501f\u9274\u7684\u3002\u5f53\u7136\u4e0a\u8ff0\u573a\u666f\u4e0b\u201c\u574f\u201d\u7684\u8bbe\u8ba1\uff0c\u5c31\u662f\u4f1a\u5148\u5c1d\u8bd5\u83b7\u53d6\u72ec\u5360\u9501\uff0c\u5728\u83b7\u53d6\u72ec\u5360\u9501\u540e\u518d\u8fdb\u884c\u201c\u5224\u65ad\u5143\u7d20\u662f\u5426\u5b58\u5728\u548c\u51b3\u5b9a\u662f\u5426\u6dfb\u52a0\u5143\u7d20\u7684\u64cd\u4f5c\u201d\u3002\u8fd9\u6837\u5219\u4f1a\u5bfc\u81f4\u5927\u5927\u589e\u52a0\u7ebf\u7a0b\u963b\u585e\u6302\u8d77\u51e0\u7387\u3002\u76f8\u4fe1\u5927\u591a\u6570\u540c\u5b66\u8fd8\u662f\u80fd\u5199\u51fa\u6f02\u4eae\u7684\u4ee3\u7801\u7684\uff0c\u4e0d\u81f3\u4e8e\u72af\u8fd9\u79cd\u5c0f\u9519\u8bef\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u83b7\u53d6\u5143\u7d20<\/h3>\n\n\n\n<p>\u83b7\u53d6\u5143\u7d20\u4e00\u5171\u6d89\u53ca\u5230\u4e09\u4e2a\u65b9\u6cd5\uff0c\u6e90\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public E get(int index) {\n    return get(getArray(), index);\n}\n\/\/ \u6b65\u9aa4\u4e00\nfinal Object[] getArray() {\n    return array;\n}\n\/\/ \u6b65\u9aa4\u4e8c\nprivate E get(Object[] a, int index) {\n    return (E) a[index];\n}<\/pre>\n\n\n\n<p>\u6211\u4eec\u770b\u5230\u83b7\u53d6\u5143\u7d20\u7684\u64cd\u4f5c\uff0c\u5168\u7a0b\u6ca1\u6709\u52a0\u9501\u3002\u5e76\u4e14\u83b7\u53d6\u5143\u7d20\u662f\u7531\u4e24\u6b65\u64cd\u4f5c\u7ec4\u5408\u800c\u6210\u7684\uff0c\u4e00\u83b7\u53d6\u5f53\u524d\u6570\u7ec4\uff0c\u4e8c\u4ece\u5f53\u524d\u6570\u636e\u4e2d\u53d6\u51fa\u6240\u6307\u5b9a\u7684\u4e0b\u6807\u4f4d\u7f6e\u7684\u5143\u7d20\u3002\u4e00\u65e6\u5728\u8fd9\u4e24\u6b65\u64cd\u4f5c\u4e4b\u95f4\uff0c\u6709\u5176\u5b83\u7ebf\u7a0b\u66f4\u6539\u4e86 <code>index<\/code> \u4e0b\u6807\u4f4d\u7f6e\u7684\u5143\u7d20\u3002\u6b64\u65f6\uff0c\u83b7\u53d6\u5143\u7d20\u7684\u7ebf\u7a0b\u6240\u4f7f\u7528\u7684\u6570\u7ec4\u5219\u662f\u88ab\u5e9f\u5f03\u6389\u7684\uff0c\u5b83\u63a5\u6536\u5230\u7684\u503c\u4e5f\u4e0d\u662f\u6700\u65b0\u7684\u503c\u3002\u8fd9\u662f<strong>\u5199\u65f6\u590d\u5236\u7b56\u7565\u4ea7\u751f\u7684\u5f31\u4e00\u81f4\u6027\u95ee\u9898<\/strong>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4fee\u6539\u5143\u7d20<\/h3>\n\n\n\n<p>\u53ef\u4ee5\u4f7f\u7528 <code>set(int index, E element)<\/code> \u65b9\u6cd5\u4fee\u6539\u6307\u5b9a\u4f4d\u7f6e\u7684\u5143\u7d20\u3002<\/p>\n\n\n\n<p><strong>\u6e90\u7801<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public E set(int index, E element) {\n    \/\/ \u83b7\u53d6\u72ec\u5360\u9501\n    final ReentrantLock lock = this.lock;\n    lock.lock();\n    try {\n        \/\/ \u83b7\u53d6\u5f53\u524d\u6570\u7ec4\n        Object[] elements = getArray();\n        \/\/ \u83b7\u53d6\u8981\u4fee\u6539\u4f4d\u7f6e\u7684\u5143\u7d20\n        E oldValue = get(elements, index);\n        \/\/ \u65b0\u503c\u4e0e\u8001\u503c\u662f\u5426\u4e00\u6837\n        if (oldValue != element) {\n            int len = elements.length;\n            Object[] newElements = Arrays.copyOf(elements, len);\n            newElements[index] = element;\n            setArray(newElements);\n        } else {\n            \/\/ Not quite a no-op; ensures volatile write semantics\n            setArray(elements);\n        }\n        return oldValue;\n    } finally {\n        lock.unlock();\n    }\n}<\/pre>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u5728\u4ee3\u7801\u4e2d\u5e76\u6ca1\u6709\u663e\u793a\u7684\u5224\u65ad <code>index<\/code> \u662f\u5426\u5408\u6cd5\uff0c\u5982\u679c\u4e0d\u5408\u6cd5\u5219\u4f1a\u629b\u51fa <code>IndexOutOfBoundsException<\/code> \u5f02\u5e38\u3002<\/p>\n\n\n\n<p>\u4e3b\u8981\u903b\u8f91\u4e5f\u662f\u5148\u5c1d\u8bd5\u83b7\u53d6\u72ec\u5360\u9501\uff0c\u7b26\u5408\u6761\u4ef6\u5219\u8fdb\u884c\u4fee\u6539\u3002\u9700\u8981\u6ce8\u610f\u7684\u4e00\u70b9\u662f\uff0c\u5982\u679c\u6307\u5b9a\u7d22\u5f15\u5904\u7684\u5143\u7d20\u503c\u4e0e\u65b0\u503c\u76f8\u7b49\uff0c\u4e5f\u4f1a\u8c03\u7528 <code>setArray(Object[] a)<\/code> \u4e00\u6b21\u65b9\u6cd5\uff0c\u8fd9\u4e3b\u8981\u662f\u4e3a\u4e86\u4fdd\u8bc1 <code>volatile<\/code> \u8bed\u4e49\u3002\uff08\u7ebf\u7a0b\u5728\u5199\u5165 <code>volatile<\/code> \u53d8\u91cf\u65f6\uff0c\u4e0d\u4f1a\u628a\u503c\u7f13\u5b58\u5728\u5bc4\u5b58\u5668\u6216\u8005\u5176\u5b83\u5730\u65b9\uff0c\u800c\u662f\u4f1a\u628a\u503c\u5237\u56de\u5230\u4e3b\u5185\u5b58\uff0c\u786e\u4fdd\u5185\u5b58\u53ef\u89c1\u6027\uff09<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u3000\u5220\u9664\u5143\u7d20<\/h3>\n\n\n\n<p>\u5220\u9664\u5143\u7d20\u7684\u65b9\u6cd5\u5305\u62ec\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>E remove(int index)<\/li><li>boolean remove(Object o)<\/li><li>boolean removeAll(Collection c)<\/li><\/ul>\n\n\n\n<p>\u6211\u4eec\u6765\u770b\u4e0b <code>remove(int index)<\/code> \u7684\u5b9e\u73b0\uff0c<br>\n<strong>\u6e90\u7801<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public E remove(int index) {\n    \/\/ \u83b7\u53d6\u72ec\u5360\u9501\n    final ReentrantLock lock = this.lock;\n    lock.lock();\n    try {\n        \/\/ \u83b7\u53d6\u5f53\u524d\u6570\u636e\n        Object[] elements = getArray();\n        int len = elements.length;\n        \/\/ \u83b7\u53d6\u8981\u88ab\u5220\u9664\u7684\u5143\u7d20\n        E oldValue = get(elements, index);\n        \/\/ \u8ba1\u7b97\u8981\u79fb\u52a8\u7684\u4f4d\u7f6e\n        int numMoved = len - index - 1;\n        if (numMoved == 0)\n            \/\/ \u5220\u9664\u6700\u540e\u4e00\u4e2a\u5143\u7d20\n            setArray(Arrays.copyOf(elements, len - 1));\n        else {\n            Object[] newElements = new Object[len - 1];\n            \/\/ \u590d\u5236\u88ab\u5220\u9664\u5143\u7d20\u4e4b\u524d\u7684\u6240\u6709\u5143\u7d20\u5230\u65b0\u6570\u7ec4\n            System.arraycopy(elements, 0, newElements, 0, index);\n            \/\/ \u590d\u5236\u88ab\u5220\u9664\u5143\u7d20\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5230\u65b0\u6570\u7ec4\n            System.arraycopy(elements, index + 1, newElements, index,\n                             numMoved);\n            \/\/ \u8bbe\u7f6e\u65b0\u6570\u7ec4\n            setArray(newElements);\n        }\n        return oldValue;\n    } finally {\n        lock.unlock();\n    }\n}<\/pre>\n\n\n\n<p>\u5176\u5b9e\u4ee3\u7801\u903b\u8f91\u5f88\u6e05\u695a\uff0c\u83b7\u53d6\u9501\u540e\u6839\u636e\u60c5\u51b5\u590d\u5236\u8001\u6570\u7ec4\u4e2d\u7684\u672a\u5220\u9664\u6570\u636e\u5230\u65b0\u6570\u7ec4\u5373\u53ef\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u8fed\u4ee3\u5668<\/h3>\n\n\n\n<p>\u4e0d\u77e5\u9053\u5927\u5bb6\u6709\u6ca1\u6709\u5728\u904d\u5386 <code>ArrayList<\/code> \u53d8\u91cf\u7684\u8fc7\u7a0b\u4e2d\u60f3\u6ca1\u60f3\u8fc7\u5220\u9664\u5176\u4e2d\u7684\u67d0\u4e2a\u5143\u7d20\uff1f\u53cd\u6b63\u6211\u66fe\u7ecf\u8fd9\u4e48\u5199\u8fc7\uff0c\u7136\u540e\u5c31\u51fa\u73b0\u4e86\u95ee\u9898 \u2026 \u540e\u6765\u4f7f\u7528\u4e86 <code>ArrayList<\/code> \u7684\u8fed\u4ee3\u5668\u4e4b\u540e\u5c31\u6ca1\u6709\u9519\u8bef\u4e86\u3002<\/p>\n\n\n\n<p>\u5728 <code>CopyOnWriteArrayList<\/code> \u4e2d\u4e5f\u6709\u8fed\u4ee3\u5668\uff0c\u4f46\u662f\u4e5f\u5b58\u5728\u7740\u5f31\u4e00\u81f4\u6027\u95ee\u9898<br>\n<strong>\u6e90\u7801<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public Iterator&amp;lt;E&amp;gt; iterator() {\n    return new COWIterator&amp;lt;E&amp;gt;(getArray(), 0);\n}\n\nstatic final class COWIterator&amp;lt;E&amp;gt; implements ListIterator&amp;lt;E&amp;gt; {\n    \/** Snapshot of the array \u6570\u7ec4\u7684\u5feb\u7167\u7248\u672c *\/\n    private final Object[] snapshot;\n    \/** Index of element to be returned by subsequent call to next.  *\/\n    private int cursor;\n    \/\/ \u6784\u9020\u51fd\u6570\n    private COWIterator(Object[] elements, int initialCursor) {\n        cursor = initialCursor;\n        snapshot = elements;\n    }\n    \/\/ \u662f\u5426\u7ed3\u675f\n    public boolean hasNext() {\n        return cursor &amp;lt; snapshot.length;\n    }\n\n    public boolean hasPrevious() {\n        return cursor &amp;gt; 0;\n    }\n    \/\/ \u83b7\u53d6\u5143\u7d20\n    public E next() {\n        if (! hasNext())\n            throw new NoSuchElementException();\n        return (E) snapshot[cursor++];\n    }\n\n   ... ...\n\n    public int nextIndex() {\n        return cursor;\n    }\n\n    public int previousIndex() {\n        return cursor-1;\n    }\n\n    public void remove() {\n        throw new UnsupportedOperationException();\n    }\n\n    public void set(E e) {\n        throw new UnsupportedOperationException();\n    }\n\n    public void add(E e) {\n        throw new UnsupportedOperationException();\n    }\n\n    ... ...\n}<\/pre>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\uff0c<code>CopyOnWriteArrayList<\/code> \u7684\u8fed\u4ee3\u5668\u5e76\u4e0d\u652f\u6301 <code>remove<\/code> \u64cd\u4f5c\u3002\u5728\u8c03\u7528 <code>iterator()<\/code> \u65b9\u6cd5\u65f6\u83b7\u53d6\u4e86\u4e00\u4efd\u5f53\u524d\u6570\u7ec4\u7684\u5feb\u7167\uff0c\u5982\u679c\u5728\u904d\u5386\u671f\u95f4\u5e76\u6ca1\u6709\u5176\u5b83\u7ebf\u7a0b\u5bf9\u6570\u636e\u505a\u66f4\u6539\u64cd\u4f5c\u5c31\u4e0d\u4f1a\u51fa\u73b0\u4e00\u81f4\u6027\u7684\u95ee\u9898\u3002\u4e00\u65e6\u6709\u5176\u5b83\u7ebf\u7a0b\u5bf9\u6570\u636e\u66f4\u6539\u540e\uff0c\u5c06 <code>CopyOnWriteArrayList<\/code> \u4e2d\u7684\u6570\u7ec4\u66f4\u6539\u4e3a\u4e86\u65b0\u6570\u7ec4\uff0c\u6b64\u65f6\u8fed\u4ee3\u5668\u6240\u6301\u6709\u7684\u6570\u636e\u5c31\u76f8\u5f53\u4e8e\u5feb\u7167\u4e86\uff0c\u540c\u65f6\u4e5f\u51fa\u73b0\u4e86\u5f31\u4e00\u81f4\u6027\u95ee\u9898\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u62d3\u5c55\u5ef6\u7533<\/h3>\n\n\n\n<p>\u8fd8\u8bb0\u5f97\u521a\u521a\u63d0\u5230\u7684 <code>addIfAbsent<\/code> \u65b9\u6cd5\u5417\uff1f\u770b\u5230\u5b83\u4f60\u6709\u6ca1\u6709\u8054\u60f3\u5230\u4ec0\u4e48\u4e1c\u897f\u5462\uff1f\u96c6\u5408 <code>set<\/code>?<\/p>\n\n\n\n<p>\u5bf9\u7684\uff0c\u901a\u8fc7 <code>addIfAbsent<\/code> \u65b9\u6cd5\u4e5f\u80fd\u5b9e\u73b0\u96c6\u5408\u7684\u529f\u80fd\uff0c<code>CopyOnWriteArraySet<\/code> \u7684\u5e95\u5c42\u5c31\u662f\u4f7f\u7528 <code>CopyOnWriteArrayList<\/code> \u5b9e\u73b0\u7684\u3002(PS.  <code>HasSet<\/code> \u7684\u5e95\u5c42\u4f9d\u8d56 <code>HashMap<\/code> \u3002)<\/p>\n\n\n<ul class=\"wp-block-latest-posts__list wp-block-latest-posts\"><li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/www.changxuan.top\/?p=1865\">\u3010\u7a0b\u5e8f\u4eba\u751f\u3011\u5de5\u4f5c\u516d\u5e74<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/www.changxuan.top\/?p=1854\">\u8ba1\u7b97\u673a\u4f53\u7cfb\u7ed3\u6784\u4e2d\u7684 7 \u4e2a\u5e38\u7528\u601d\u60f3<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/www.changxuan.top\/?p=1808\">k3s\u3001ctr\u3001contained\u3001images \u95ee\u9898\u5c0f\u8bb0<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/www.changxuan.top\/?p=1798\">\u3010\u7a0b\u5e8f\u4eba\u751f\u3011\u5de5\u4f5c\u4e94\u5e74<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/www.changxuan.top\/?p=1609\">\u8dd1\u6b65<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>CopyOnWriteArrayList \u662f JUC \u4e2d\u552f\u4e00\u4e00\u4e2a\u652f\u6301\u5e76\u53d1\u7684 List\u3002 CopyOnWrite &hellip; <a href=\"https:\/\/www.changxuan.top\/?p=1252\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u5256\u6790 CopyOnWriteArrayList<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37],"tags":[98],"class_list":["post-1252","post","type-post","status-publish","format-standard","hentry","category-tech","tag-juc"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>\u5256\u6790 CopyOnWriteArrayList - \u5e38\u8f69<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.changxuan.top\/?p=1252\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u5256\u6790 CopyOnWriteArrayList - \u5e38\u8f69\" \/>\n<meta property=\"og:description\" content=\"CopyOnWriteArrayList \u662f JUC \u4e2d\u552f\u4e00\u4e00\u4e2a\u652f\u6301\u5e76\u53d1\u7684 List\u3002 CopyOnWrite &hellip; \u7ee7\u7eed\u9605\u8bfb\u5256\u6790 CopyOnWriteArrayList\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.changxuan.top\/?p=1252\" \/>\n<meta property=\"og:site_name\" content=\"\u5e38\u8f69\" \/>\n<meta property=\"article:published_time\" content=\"2021-02-01T04:37:31+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-04-15T09:02:37+00:00\" \/>\n<meta name=\"author\" content=\"\u5e38\u8f69\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"\u5e38\u8f69\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.changxuan.top\\\/?p=1252#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.changxuan.top\\\/?p=1252\"},\"author\":{\"name\":\"\u5e38\u8f69\",\"@id\":\"https:\\\/\\\/www.changxuan.top\\\/#\\\/schema\\\/person\\\/08c8f0af44536928094dc1b4f88da3bd\"},\"headline\":\"\u5256\u6790 CopyOnWriteArrayList\",\"datePublished\":\"2021-02-01T04:37:31+00:00\",\"dateModified\":\"2021-04-15T09:02:37+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.changxuan.top\\\/?p=1252\"},\"wordCount\":48,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.changxuan.top\\\/#\\\/schema\\\/person\\\/08c8f0af44536928094dc1b4f88da3bd\"},\"keywords\":[\"JUC\"],\"articleSection\":[\"\u6280\u672f\"],\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.changxuan.top\\\/?p=1252#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.changxuan.top\\\/?p=1252\",\"url\":\"https:\\\/\\\/www.changxuan.top\\\/?p=1252\",\"name\":\"\u5256\u6790 CopyOnWriteArrayList - \u5e38\u8f69\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.changxuan.top\\\/#website\"},\"datePublished\":\"2021-02-01T04:37:31+00:00\",\"dateModified\":\"2021-04-15T09:02:37+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.changxuan.top\\\/?p=1252#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.changxuan.top\\\/?p=1252\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.changxuan.top\\\/?p=1252#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\\\/\\\/www.changxuan.top\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u5256\u6790 CopyOnWriteArrayList\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.changxuan.top\\\/#website\",\"url\":\"https:\\\/\\\/www.changxuan.top\\\/\",\"name\":\"\u5e38\u8f69\",\"description\":\"\u95fb\u9053\u6709\u5148\u540e\uff0c\u672f\u4e1a\u6709\u4e13\u653b-\u4e00\u4e8c\u4e09\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.changxuan.top\\\/#\\\/schema\\\/person\\\/08c8f0af44536928094dc1b4f88da3bd\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.changxuan.top\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"zh-Hans\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/www.changxuan.top\\\/#\\\/schema\\\/person\\\/08c8f0af44536928094dc1b4f88da3bd\",\"name\":\"\u5e38\u8f69\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\\\/\\\/www.changxuan.top\\\/wp-content\\\/uploads\\\/2021\\\/04\\\/IMG_008520200606-095631.jpg\",\"url\":\"https:\\\/\\\/www.changxuan.top\\\/wp-content\\\/uploads\\\/2021\\\/04\\\/IMG_008520200606-095631.jpg\",\"contentUrl\":\"https:\\\/\\\/www.changxuan.top\\\/wp-content\\\/uploads\\\/2021\\\/04\\\/IMG_008520200606-095631.jpg\",\"width\":960,\"height\":960,\"caption\":\"\u5e38\u8f69\"},\"logo\":{\"@id\":\"https:\\\/\\\/www.changxuan.top\\\/wp-content\\\/uploads\\\/2021\\\/04\\\/IMG_008520200606-095631.jpg\"},\"description\":\"\u603b\u8981\u505a\u70b9\u4ec0\u4e48\u5427\uff01\",\"sameAs\":[\"https:\\\/\\\/www.changxuan.top\"],\"url\":\"https:\\\/\\\/www.changxuan.top\\\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u5256\u6790 CopyOnWriteArrayList - \u5e38\u8f69","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.changxuan.top\/?p=1252","og_locale":"zh_CN","og_type":"article","og_title":"\u5256\u6790 CopyOnWriteArrayList - \u5e38\u8f69","og_description":"CopyOnWriteArrayList \u662f JUC \u4e2d\u552f\u4e00\u4e00\u4e2a\u652f\u6301\u5e76\u53d1\u7684 List\u3002 CopyOnWrite &hellip; \u7ee7\u7eed\u9605\u8bfb\u5256\u6790 CopyOnWriteArrayList","og_url":"https:\/\/www.changxuan.top\/?p=1252","og_site_name":"\u5e38\u8f69","article_published_time":"2021-02-01T04:37:31+00:00","article_modified_time":"2021-04-15T09:02:37+00:00","author":"\u5e38\u8f69","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"\u5e38\u8f69","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.changxuan.top\/?p=1252#article","isPartOf":{"@id":"https:\/\/www.changxuan.top\/?p=1252"},"author":{"name":"\u5e38\u8f69","@id":"https:\/\/www.changxuan.top\/#\/schema\/person\/08c8f0af44536928094dc1b4f88da3bd"},"headline":"\u5256\u6790 CopyOnWriteArrayList","datePublished":"2021-02-01T04:37:31+00:00","dateModified":"2021-04-15T09:02:37+00:00","mainEntityOfPage":{"@id":"https:\/\/www.changxuan.top\/?p=1252"},"wordCount":48,"commentCount":0,"publisher":{"@id":"https:\/\/www.changxuan.top\/#\/schema\/person\/08c8f0af44536928094dc1b4f88da3bd"},"keywords":["JUC"],"articleSection":["\u6280\u672f"],"inLanguage":"zh-Hans","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.changxuan.top\/?p=1252#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.changxuan.top\/?p=1252","url":"https:\/\/www.changxuan.top\/?p=1252","name":"\u5256\u6790 CopyOnWriteArrayList - \u5e38\u8f69","isPartOf":{"@id":"https:\/\/www.changxuan.top\/#website"},"datePublished":"2021-02-01T04:37:31+00:00","dateModified":"2021-04-15T09:02:37+00:00","breadcrumb":{"@id":"https:\/\/www.changxuan.top\/?p=1252#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.changxuan.top\/?p=1252"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.changxuan.top\/?p=1252#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.changxuan.top\/"},{"@type":"ListItem","position":2,"name":"\u5256\u6790 CopyOnWriteArrayList"}]},{"@type":"WebSite","@id":"https:\/\/www.changxuan.top\/#website","url":"https:\/\/www.changxuan.top\/","name":"\u5e38\u8f69","description":"\u95fb\u9053\u6709\u5148\u540e\uff0c\u672f\u4e1a\u6709\u4e13\u653b-\u4e00\u4e8c\u4e09","publisher":{"@id":"https:\/\/www.changxuan.top\/#\/schema\/person\/08c8f0af44536928094dc1b4f88da3bd"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.changxuan.top\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"zh-Hans"},{"@type":["Person","Organization"],"@id":"https:\/\/www.changxuan.top\/#\/schema\/person\/08c8f0af44536928094dc1b4f88da3bd","name":"\u5e38\u8f69","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.changxuan.top\/wp-content\/uploads\/2021\/04\/IMG_008520200606-095631.jpg","url":"https:\/\/www.changxuan.top\/wp-content\/uploads\/2021\/04\/IMG_008520200606-095631.jpg","contentUrl":"https:\/\/www.changxuan.top\/wp-content\/uploads\/2021\/04\/IMG_008520200606-095631.jpg","width":960,"height":960,"caption":"\u5e38\u8f69"},"logo":{"@id":"https:\/\/www.changxuan.top\/wp-content\/uploads\/2021\/04\/IMG_008520200606-095631.jpg"},"description":"\u603b\u8981\u505a\u70b9\u4ec0\u4e48\u5427\uff01","sameAs":["https:\/\/www.changxuan.top"],"url":"https:\/\/www.changxuan.top\/?author=1"}]}},"views":1911,"_links":{"self":[{"href":"https:\/\/www.changxuan.top\/index.php?rest_route=\/wp\/v2\/posts\/1252","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.changxuan.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.changxuan.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.changxuan.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.changxuan.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1252"}],"version-history":[{"count":3,"href":"https:\/\/www.changxuan.top\/index.php?rest_route=\/wp\/v2\/posts\/1252\/revisions"}],"predecessor-version":[{"id":1339,"href":"https:\/\/www.changxuan.top\/index.php?rest_route=\/wp\/v2\/posts\/1252\/revisions\/1339"}],"wp:attachment":[{"href":"https:\/\/www.changxuan.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.changxuan.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.changxuan.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}