{"id":4063,"date":"2022-12-05T13:44:54","date_gmt":"2022-12-05T05:44:54","guid":{"rendered":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=4063"},"modified":"2022-12-05T14:39:37","modified_gmt":"2022-12-05T06:39:37","slug":"copyonwritearraylist%e7%9c%9f%e7%9a%84%e7%ba%bf%e7%a8%8b%e5%ae%89%e5%85%a8%e5%90%97","status":"publish","type":"post","link":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=4063","title":{"rendered":"CopyOnWriteArrayList\u771f\u7684\u7ebf\u7a0b\u5b89\u5168\u5417?"},"content":{"rendered":"\n<p>\u539f\u6587\uff1a<a href=\"https:\/\/juejin.cn\/post\/7172885337738313758\">CopyOnWriteArrayList\u771f\u7684\u7ebf\u7a0b\u5b89\u5168\u5417? &#8211; \u6398\u91d1 (juejin.cn)<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/p9-juejin.byteimg.com\/tos-cn-i-k3u1fbpfcp\/202f0607c9c5477a91271d7616232c6c~tplv-k3u1fbpfcp-zoom-crop-mark:3024:3024:3024:1702.awebp?\" alt=\"CopyOnWriteArrayList\u771f\u7684\u7ebf\u7a0b\u5b89\u5168\u5417?\"\/><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote\">\n<p>\u524d\u51e0\u5929\u5237\u535a\u5ba2\u65f6\uff0c\u65e0\u610f\u4e2d\u770b\u5230\u4e00\u7bc7\u540d\u4e3a\u300aCopyOnWriteArrayList\u771f\u7684\u5b8c\u5168\u7ebf\u7a0b\u5b89\u5168\u5417\u300b\u535a\u5ba2\u3002\u5fc3\u4e2d\u4e0d\u7981\u6cdb\u8d77\u7591\u95ee\uff0c\u5b83\u5c31\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u554a\uff0c\u96be\u9053\u8fd8\u6709\u5565\u7279\u6b8a\u60c5\u51b5\uff1f<\/p>\n<\/blockquote>\n\n\n\n<p>\u6211\u4eec\u77e5\u9053<code>CopyOnWrite<\/code>\u7684\u6838\u5fc3\u601d\u60f3\u6b63\u5982\u5176\u540d\uff1a<code>\u5199\u65f6\u590d\u5236<\/code>\u3002\u5728\u5bf9\u6570\u636e\u6709\u4fee\u6539\u64cd\u4f5c\u65f6\uff0c\u5148\u590d\u5236\u518d\u64cd\u4f5c\uff0c\u6700\u540e\u66ff\u6362\u539f\u6570\u7ec4\u3002\u5728\u8fd9\u4e9b\u64cd\u4f5c\u65f6\uff0c\u662f\u6709\u52a0\u9501\u7684\u4e86\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1 \u95ee\u9898\u590d\u73b0<\/h3>\n\n\n\n<p>\u8fd9\u7bc7\u535a\u6587\u4e2d\u4e3b\u8981\u63d0\u5230<code>\u6570\u7ec4\u8d8a\u754c<\/code>\u5f02\u5e38\u3002\u573a\u666f\u4e3a\uff1a\u5047\u8bbe\u73b0\u5728\u6709\u4e00\u4e2a\u5df2\u5b58\u5728\u7684\u5217\u8868\uff0c\u7ebf\u7a0b1\u5c1d\u8bd5\u53bb\u67e5\u8be2\u5217\u8868\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff0c\u800c\u6b64\u65f6\u7ebf\u7a0b2\u8981\u53bb\u5220\u9664\u5217\u8868\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u3002\u6b64\u65f6\u7ebf\u7a0b1\u7531\u4e8e\u6700\u5f00\u59cb\u8bfb\u53d6\u7684size()=n\uff0c\u5728\u7ebf\u7a0b2\u5220\u9664\u540esize()=n-1\uff0c\u518d\u62ff\u539fIndex\u65b9\u5f0f\u65f6\uff0c\u4fbf\u89e6\u53d1<code>ArrayIndexOutOfBoundsException<\/code>\u5f02\u5e38\u3002<\/p>\n\n\n\n<p>\u5176\u5b9e\u8bfb\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u5c31\u5df2\u7ecf\u77e5\u9053\u4e86\u95ee\u9898\u6240\u5728\u3002\u5728\u8bfb\u53d6<code>\u5217\u8868\u5927\u5c0f<\/code>\u548c<code>\u6839\u636e\u7d22\u5f15\u8bbf\u95ee<\/code>\u4e24\u4e2a\u65f6\u95f4\u70b9\uff0c\u5217\u8868\u6570\u636e\u5df2\u7ecf\u53d1\u751f\u4e86\u6539\u53d8\u3002\u8fd9\u79cd\u5f02\u5e38\u7406\u8bba\u4e0a\u5c5e\u4e8e\u53ef\u9884\u77e5\u7684\u5f02\u5e38\u3002<\/p>\n\n\n\n<p>\u8bf7\u770b\u4e0b\u9762\u7684\u4ee3\u7801\uff0c\u5e76\u601d\u8003\u4e0b\u5e76\u53d1\u6267\u884c\u4f1a\u6709\u95ee\u9898\u5417<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CopyOnWriteArrayList&lt;String> cowList = <strong>new<\/strong> <strong>CopyOnWriteArrayList<\/strong>&lt;>(l);\n\n<strong>while<\/strong> (true) {\n    <strong>if<\/strong> (!cowList.isEmpty()) {\n        cowList.remove(0);\n    } <strong>else<\/strong> {\n        <strong>return<\/strong>;\n    }\n}\n <\/code><\/pre>\n\n\n\n<p>\u6211\u4eec\u4e0d\u59a8\u6765\u8bd5\u4e0b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em>\/**\n * @author lpe234\n * @date 2022\/12\/03\n *\/<\/em>\n<strong>@Slf4j<\/strong>\n<strong>public<\/strong> <strong>class<\/strong> <strong>CowalTest<\/strong> {\n    <strong>public<\/strong> <strong>static<\/strong> void <strong>main<\/strong>(String&#91;] args) {\n        <strong>List<\/strong>&lt;<strong>String<\/strong>> l = <strong>new<\/strong> <strong>ArrayList<\/strong>&lt;>();\n        <strong>for<\/strong> (int i = 0; i &lt; 100; i++) {\n            l.<strong>add<\/strong>(<strong>String<\/strong>.<strong>valueOf<\/strong>(i));\n        }\n\n        <strong>CopyOnWriteArrayList<\/strong>&lt;<strong>String<\/strong>> cowList = <strong>new<\/strong> <strong>CopyOnWriteArrayList<\/strong>&lt;>(l);\n\n        final <strong>Runnable<\/strong> rab = () -> {\n            <strong>while<\/strong> (true) {\n                <strong>if<\/strong> (!cowList.<strong>isEmpty<\/strong>()) {\n                    cowList.<strong>remove<\/strong>(0);\n                } <strong>else<\/strong> {\n                    <strong>return<\/strong>;\n                }\n            }\n        };\n\n        <strong>new<\/strong> <strong>Thread<\/strong>(rab).<strong>start<\/strong>();\n        <strong>new<\/strong> <strong>Thread<\/strong>(rab).<strong>start<\/strong>();\n    }\n}\n <\/code><\/pre>\n\n\n\n<p>\u7a0b\u5e8f\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Exception <strong>in<\/strong> thread \"Thread-1\" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds <strong>for<\/strong> length 0\n\tat java.base\/java.util.concurrent.CopyOnWriteArrayList.elementAt(CopyOnWriteArrayList.java:386)\n\tat java.base\/java.util.concurrent.CopyOnWriteArrayList.remove(CopyOnWriteArrayList.java:478)\n\tat com.example.other.CowalTest.lambda$main$0(CowalTest.java:25)\n\tat java.base\/java.lang.Thread.run(Thread.java:834)\n <\/code><\/pre>\n\n\n\n<p>\u539f\u56e0\u5c31\u5728\u4e8e<code>cowList.isEmpty()<\/code>\u548c<code>cowList.remove(0)<\/code>\u4e3a\u4e24\u4e2a\u64cd\u4f5c\u3002\u5728\u8fd9\u4e24\u4e2a\u64cd\u4f5c\u4e4b\u95f4\uff0c\u5e76\u6ca1\u6709\u4ec0\u4e48\u673a\u5236\u6765\u4fdd\u8bc1<code>cowList<\/code>\u4e0d\u4f1a\u6539\u53d8\u3002\u6240\u4ee5\u51fa\u73b0\u5f02\u5e38\uff0c\u662f\u53ef\u9884\u89c1\u7684\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2 \u6e90\u7801\u5206\u6790<\/h3>\n\n\n\n<p>\u6838\u5fc3\u5c5e\u6027\u53caget\/set\u65b9\u6cd5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>public<\/strong> <strong>class<\/strong> <strong>CopyOnWriteArrayList<\/strong>&lt;E>\n    <strong>implements<\/strong> <strong>List<\/strong>&lt;E>, RandomAccess, Cloneable, java.io.Serializable {\n    <strong>private<\/strong> <strong>static<\/strong> <strong>final<\/strong> <strong>long<\/strong> serialVersionUID = 8673264195747942595L;\n\n    <em>\/** \u6240\u6709\u6d89\u53ca\u5230array\u53d8\u66f4\u64cd\u4f5c\u7684\u9501\u3002(\u5728\u5185\u7f6e\u9501\u548cReentrantLock\u90fd\u53ef\u4f7f\u7528\u65f6\uff0c\u6211\u4eec\u66f4\u503e\u5411\u4e8e\u5185\u7f6e\u9501) *\/<\/em>\n    <strong>final<\/strong> <strong>transient<\/strong> <strong>Object<\/strong> lock = <strong>new<\/strong> <strong>Object<\/strong>();\n\n    <em>\/** \u8fd9\u4e2a\u6570\u7ec4\u7684\u6240\u6709\u8bbf\u95ee\uff0c\u53ea\u4f1a\u901a\u8fc7getArray\/setArray\u6765\u8fdb\u884c\u3002 *\/<\/em>\n    <strong>private<\/strong> <strong>transient<\/strong> <strong>volatile<\/strong> Object&#91;] array;\n\n    <em>\/**\n     * Gets the array.  Non-private so as to also be accessible\n     * from CopyOnWriteArraySet class.\n     *\/<\/em>\n    <strong>final<\/strong> Object&#91;] getArray() {\n        <strong>return<\/strong> array;\n    }\n\n    <em>\/**\n     * Sets the array.\n     *\/<\/em>\n    <strong>final<\/strong> <strong>void<\/strong> <strong>setArray<\/strong>(Object&#91;] a) {\n        array = a;\n    } <\/code><\/pre>\n\n\n\n<p>\u53ef\u89c1\u5b9e\u73b0\u5176\u5b9e\u5f88\u7b80\u5355\u3002\u5185\u90e8\u4f7f\u7528<code>Object[] array<\/code>\u6765\u627f\u8f7d\u6570\u636e\u3002\u4f7f\u7528<code>volatile<\/code>\u6765\u4fdd\u8bc1\u591a\u7ebf\u7a0b\u4e0b\u6570\u7ec4\u7684\u53ef\u89c1\u6027\u3002<\/p>\n\n\n\n<p>\u518d\u770b\u4e0b<code>isEmpty<\/code>\u548c<code>remove<\/code>\u65b9\u6cd5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public int size() {\n    return getArray().length<em>;<\/em>\n}\n\npublic boolean isEmpty() {\n    return size() == 0<em>;<\/em>\n}\n\npublic E remove(int index) {\n    synchronized (lock) {\n        Object<strong>&#91;]<\/strong> es = getArray()<em>;<\/em>\n        int len = es.length<em>;<\/em>\n        E oldValue = elementAt(es, index)<em>;<\/em>\n        int numMoved = len - index - 1<em>;<\/em>\n        Object<strong>&#91;]<\/strong> newElements<em>;<\/em>\n        if (numMoved == 0)\n            newElements = Arrays.copyOf(es, len - 1)<em>;<\/em>\n        else {\n            newElements = new Object&#91;len - 1]<em>;<\/em>\n            System.arraycopy(es, 0, newElements, 0, index)<em>;<\/em>\n            System.arraycopy(es, index + 1, newElements, index,\n                             numMoved)<em>;<\/em>\n        }\n        setArray(newElements)<em>;<\/em>\n        return oldValue<em>;<\/em>\n    }\n}\n\u590d\u5236\u4ee3\u7801<\/code><\/pre>\n\n\n\n<p>\u53ef\u4ee5\u5f88\u6e05\u6670\u7684\u770b\u5230\uff0c\u5728\u8fd9\u4fe9\u65b9\u6cd5\u4e2d\uff0c\u5747\u6709<code>getArray()<\/code>\u8c03\u7528\u3002\u5982\u679c\u4e2d\u95f4\u51fa\u73b0\u5176\u4ed6\u7ebf\u7a0b\u4fee\u6539\u6570\u636e\uff0c\u8fd9\u4fe9\u6570\u636e\u5fc5\u7136\u4e0d\u4e00\u81f4\u3002\u5728\u770b\u4e00\u4e2a<code>add(E e)<\/code>\u65b9\u6cd5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>public<\/strong> <strong>boolean<\/strong> <strong>add<\/strong>(E e) {\n    <strong>synchronized<\/strong> (lock) {\n        Object&#91;] es = getArray();\n        <strong>int<\/strong> len = es.length;\n        es = Arrays.copyOf(es, len + 1);\n        es&#91;len] = e;\n        setArray(es);\n        <strong>return<\/strong> true;\n    }\n} <\/code><\/pre>\n\n\n\n<p>\u6b64\u65f6\u6211\u4eec\u53ef\u4ee5\u5f88\u6e05\u6670\u7684\u770b\u6e05\u4ed6\u7684\u7f16\u7a0b\u903b\u8f91\u3002<\/p>\n\n\n\n<ul>\n<li>\u51e1\u662f\u5bf9\u6570\u7ec4\u6709\u4fee\u6539\u7684\u64cd\u4f5c\uff0c\u5148\u83b7\u53d6\u9501\u3002<\/li>\n\n\n\n<li>\u901a\u8fc7<code>getArray()<\/code>\u83b7\u53d6\u6570\u636e\u3002\u524d\u9762\u5df2\u52a0\u9501\uff0c\u4e3a\u6700\u65b0\u6570\u636e\uff0c\u5728\u91ca\u653e\u9501\u524d\u4e0d\u4f1a\u6709\u5176\u4ed6\u7ebf\u7a0b\u4fee\u6539\u3002<\/li>\n\n\n\n<li>\u5bf9\u6570\u636e\u8fdb\u884c\u76f8\u5173\u4fee\u6539\u64cd\u4f5c\uff0c<code>Arrays.copyOf<\/code>\u662f\u91cd\u70b9\u3002<\/li>\n\n\n\n<li>\u901a\u8fc7<code>setArray(es)<\/code>\u5c06\u4fee\u6539\u540e\u7684\u6570\u636e\u8d4b\u503c\u7ed9\u539f\u6570\u7ec4\u3002<\/li>\n\n\n\n<li>\u91ca\u653e\u9501\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3 \u601d\u8003<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">3.1 \u901a\u8fc7\u672c\u4f8b\u6211\u4eec\u80fd\u5b66\u5230\u4ec0\u4e48<\/h4>\n\n\n\n<ul>\n<li>\u7c7b\u4f3c<code>CopyOnWriteArrayList<\/code>\u8fd9\u79cd\u5e76\u53d1\u5b89\u5168\u7684\u7c7b\uff0c\u5982\u679c\u4e0d\u5408\u7406(\u4e0d\u89c4\u8303\u7684\u3001\u9519\u8bef\u7684)\u7684\u4f7f\u7528\uff0c\u4e5f\u4f1a\u5bfc\u81f4\u5e76\u53d1\u5b89\u5168\u95ee\u9898<\/li>\n\n\n\n<li>\u9762\u5bf9\u4e8b\u7269\uff0c\u8981\u77e5\u5176\u7136\u77e5\u5176\u6240\u4ee5\u7136\u3002\u53ea\u6709\u4e86\u89e3\u5185\u90e8\u539f\u7406\uff0c\u624d\u80fd\u66f4\u597d\u7684\u53bb\u4f7f\u7528\u5b83\u3002<\/li>\n\n\n\n<li>\u5728<code>CopyOnWriteArrayList<\/code>\u4ee3\u7801\u4e2d\u53ef\u4ee5\u770b\u5230\uff0c\u5f53\u9047\u5230\u4fee\u6539\u64cd\u4f5c\u65f6\uff0c\u57fa\u672c\u90fd\u79bb\u4e0d\u5f00<code>Arrays.copyOf<\/code>\uff0c\u8fd9\u79cd\u62f7\u8d1d\u4f1a\u5360\u7528\u989d\u5916\u4e00\u500d\u7684\u5185\u5b58\u7a7a\u95f4\u3002\u5982\u679c\u6709\u5927\u91cf\u9891\u7e41\u7684\u4fee\u6539\u64cd\u4f5c\uff0c\u663e\u7136\u662f\u4e0d\u592a\u5408\u9002\u7684\u3002<\/li>\n\n\n\n<li>\u5728\u4fee\u6539\u76f8\u5173\u64cd\u4f5c\u4ee3\u7801\u903b\u8f91\u4e2d\uff0c\u53ef\u4ee5\u4f53\u4f1a\u5230\uff0c\u6574\u4f53\u662f\u6709\u90a3\u4e48\u4e00\u70b9\u70b9\u7684\u5ef6\u8fdf\u7684\u3002\u5373\u4e00\u4e2a\u7ebf\u7a0b\u4fee\u6539\u5b8c\u5e76setArray\u540e\uff0c\u53e6\u5916\u7684\u7ebf\u7a0b\u624d\u80fd\u83b7\u53d6\u5230\u6700\u65b0\u503c\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">3.2 \u5176\u4ed6\u7684\u5462<\/h4>\n\n\n\n<ul>\n<li><code>CopyOnWrite<\/code>\u662f\u4e00\u79cd\u5f88\u597d\u7684\u601d\u60f3\uff0c\u5b83\u80fd\u591f\u4f7f<code>\u8bfb\u3001\u5199<\/code>\u64cd\u4f5c\u5e76\u53d1\u6267\u884c\u3002\u5728Redis\u7684RDB\u5feb\u7167\u751f\u6210\u65f6\uff0c\u4e5f\u4f7f\u7528\u4e86\u8be5\u601d\u60f3\u3002<\/li>\n\n\n\n<li>\u4e3a\u4ec0\u4e48\u4f1a\u6709<code>final transient Object lock = new Object()<\/code>\u8fd9\u4e2a\u9501\uff1f\u5982\u679c\u7ec6\u5fc3\u770b\u8fc7\u6e90\u7801\u5c31\u80fd\u660e\u767d\uff0c\u5176\u5b9e\u5c31\u662f\u6700\u5927\u7a0b\u5ea6\u7684\u51cf\u5c11\u9501\u7684\u8303\u56f4(\u7c92\u5ea6)\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>public<\/strong> <strong>boolean<\/strong> <strong>addAll<\/strong>(Collection&lt;? extends E> c) {\n    Object&#91;] cs = (c.getClass() == CopyOnWriteArrayList.class) ?\n        ((CopyOnWriteArrayList&lt;?>)c).getArray() : c.toArray();\n    <strong>if<\/strong> (cs.length == 0)\n        <strong>return<\/strong> false;\n    <strong>synchronized<\/strong> (lock) {\n\t\t<em>\/\/ \u7565...<\/em>\n\t}\n} <\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\uff1aCopyOnWriteArrayList\u771f\u7684\u7ebf\u7a0b\u5b89\u5168\u5417? &#8211; \u6398\u91d1 (juejin.cn)  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"_links":{"self":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4063"}],"collection":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4063"}],"version-history":[{"count":4,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4063\/revisions"}],"predecessor-version":[{"id":4067,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4063\/revisions\/4067"}],"wp:attachment":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4063"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4063"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4063"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}