{"id":4011,"date":"2024-05-08T07:58:50","date_gmt":"2024-05-07T23:58:50","guid":{"rendered":"https:\/\/xinchewhd.com.cn\/index.php\/%e9%9d%a2%e8%af%95\/%e9%9d%a2%e8%af%95%e5%ae%98%ef%bc%9acopyonwritearraylist%e6%98%af%e5%a6%82%e4%bd%95%e7%a1%ae%e4%bf%9d%e7%ba%bf%e7%a8%8b%e5%ae%89%e5%85%a8%e7%9a%84%ef%bc%9f%e4%b8%ba%e4%bb%80%e4%b9%88%e5%ae%83%e4%b8%8d\/"},"modified":"2024-05-24T15:04:36","modified_gmt":"2024-05-24T07:04:36","slug":"%e9%9d%a2%e8%af%95%e5%ae%98%ef%bc%9acopyonwritearraylist%e6%98%af%e5%a6%82%e4%bd%95%e7%a1%ae%e4%bf%9d%e7%ba%bf%e7%a8%8b%e5%ae%89%e5%85%a8%e7%9a%84%ef%bc%9f%e4%b8%ba%e4%bb%80%e4%b9%88%e5%ae%83%e4%b8%8d","status":"publish","type":"post","link":"https:\/\/xinchewhd.com.cn\/index.php\/%e9%9d%a2%e8%af%95\/%e9%9d%a2%e8%af%95%e5%ae%98%ef%bc%9acopyonwritearraylist%e6%98%af%e5%a6%82%e4%bd%95%e7%a1%ae%e4%bf%9d%e7%ba%bf%e7%a8%8b%e5%ae%89%e5%85%a8%e7%9a%84%ef%bc%9f%e4%b8%ba%e4%bb%80%e4%b9%88%e5%ae%83%e4%b8%8d\/","title":{"rendered":"\u9762\u8bd5\u5b98\uff1aCopyOnWriteArrayList\u662f\u5982\u4f55\u786e\u4fdd\u7ebf\u7a0b\u5b89\u5168\u7684\uff1f\u4e3a\u4ec0\u4e48\u5b83\u4e0d\u9002\u5408\u5904\u7406\u5927\u91cf\u5199\u64cd\u4f5c\u7684\u573a\u666f\uff1f"},"content":{"rendered":"<div class=\"wxsyncmain\">\n<p data-v-md-line=\"1\"><span style=\"font-size: 16px;\">CopyOnWriteArrayList \u662f\u4e00\u4e2a\u7ebf\u7a0b\u5b89\u5168\u7684 ArrayList \u53d8\u4f53\uff0c\u5b83\u901a\u8fc7\u4ee5\u4e0b\u673a\u5236\u786e\u4fdd\u7ebf\u7a0b\u5b89\u5168\uff1a<\/span><\/p>\n<ol class=\"list-paddingleft-1\" style=\"list-style-type: decimal;\">\n<li style=\"letter-spacing: normal;\">\n<p style=\"text-align: justify;\" data-v-md-line=\"3\"><span style=\"letter-spacing: normal;\"><strong><span style=\"font-size: 16px;\">\u5199\u65f6\u590d\u5236\uff08Copy-on-Write\uff09\u673a\u5236<\/span><\/strong><span style=\"font-size: 16px;\">\uff1a\u5f53\u5217\u8868\u88ab\u4fee\u6539\u65f6\uff08\u4f8b\u5982\u6dfb\u52a0\u3001\u5220\u9664\u3001\u8bbe\u7f6e\u5143\u7d20\u503c\u7b49\u64cd\u4f5c\uff09\uff0cCopyOnWriteArrayList \u9996\u5148\u4f1a\u5c06\u5f53\u524d\u5e95\u5c42\u6570\u7ec4\u590d\u5236\u4e00\u4efd\uff0c\u7136\u540e\u5728\u8fd9\u4e2a\u526f\u672c\u4e0a\u8fdb\u884c\u4fee\u6539\u3002\u5b8c\u6210\u4fee\u6539\u540e\uff0c\u5b83\u518d\u5c06\u539f\u6765\u7684\u6570\u7ec4\u5f15\u7528\u6307\u5411\u65b0\u4fee\u6539\u8fc7\u7684\u526f\u672c\u3002\u8fd9\u6837\uff0c\u8bfb\u64cd\u4f5c\u603b\u662f\u5728\u4e0d\u53d8\u7684\u6570\u7ec4\u7248\u672c\u4e0a\u8fdb\u884c\uff0c\u4ece\u800c\u907f\u514d\u4e86\u5e76\u53d1\u8bfb\u5199\u51b2\u7a81\u3002<\/span><\/span><\/p>\n<\/li>\n<li style=\"letter-spacing: normal;\">\n<section style=\"margin-top: 16px; text-align: justify;\"><span style=\"letter-spacing: normal;\"><strong><span style=\"font-size: 16px;\">\u5185\u90e8\u9501\u5b9a<\/span><\/strong><span style=\"font-size: 16px;\">\uff1a\u6240\u6709\u7684\u5199\u5165\u64cd\u4f5c\uff08\u589e\u52a0\u3001\u79fb\u9664\u3001\u66f4\u65b0\u7b49\u64cd\u4f5c\uff09\u90fd\u662f\u901a\u8fc7\u4e00\u4e2a\u5185\u90e8\u7684\u91cd\u5165\u9501\uff08ReentrantLock\uff09\u6765\u540c\u6b65\u7684\uff0c\u4ee5\u6b64\u6765\u4fdd\u8bc1\u540c\u65f6\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u5bf9\u5217\u8868\u5185\u5bb9\u8fdb\u884c\u4fee\u6539\u3002<\/span><\/span><\/section>\n<\/li>\n<li style=\"letter-spacing: normal;\">\n<p style=\"margin-top: 16px;\"><span style=\"letter-spacing: normal;\"><strong><span style=\"letter-spacing: normal; font-size: 16px;\">\u8fed\u4ee3\u5668\u7684\u5f31\u4e00\u81f4\u6027<\/span><\/strong><span style=\"letter-spacing: normal; font-size: 16px;\">\uff1a<\/span><span style=\"font-size: 16px; letter-spacing: normal;\">CopyOnWriteArrayList \u7684\u8fed\u4ee3\u5668\u652f\u6301\u5f31\u4e00\u81f4\u6027\uff0c\u610f\u5473\u7740\u8fed\u4ee3\u5668\u904d\u5386\u5217\u8868\u65f6\u57fa\u4e8e\u6570\u7ec4\u7684\u4e00\u4e2a\u5feb\u7167\uff0c\u56e0\u6b64\u5373\u4f7f\u540e\u7eed\u6709\u4fee\u6539\uff0c\u4e5f\u4e0d\u4f1a\u629b\u51faConcurrentModificationException\u5f02\u5e38\u3002\u8fed\u4ee3\u5668\u770b\u5230\u7684\u5185\u5bb9\u662f\u521b\u5efa\u8fed\u4ee3\u5668\u65f6\u5217\u8868\u7684\u72b6\u6001\uff0c\u5e76\u4e0d\u53cd\u6620\u4e4b\u540e\u7684\u4fee\u6539\u3002<\/span><\/span><\/p>\n<\/li>\n<\/ol>\n<p style=\"margin-top: 16px; margin-bottom: 16px;\"><span style=\"font-size: 16px;\">\u8be6\u7ec6\u5199\u5165\u5b9e\u73b0\u903b\u8f91\u8bf7\u53c2\u8003\u5982\u4e0b\u6e90\u4ee3\u7801\uff1a<\/span><\/p>\n<section class=\"code-snippet__fix code-snippet__js\">\n<pre class=\"code-snippet__js\" data-lang=\"java\"><code><span class=\"code-snippet_outer\">    <span class=\"code-snippet__function\"><span class=\"code-snippet__keyword\">public<\/span> <span class=\"code-snippet__keyword\">boolean<\/span> <span class=\"code-snippet__title\">add<\/span><span class=\"code-snippet__params\">(E e)<\/span> <\/span>{<\/span><\/code><code><span class=\"code-snippet_outer\">        <span class=\"code-snippet__keyword\">synchronized<\/span> (lock) {<\/span><\/code><code><span class=\"code-snippet_outer\">            Object[] elements = getArray();<\/span><\/code><code><span class=\"code-snippet_outer\">            <span class=\"code-snippet__keyword\">int<\/span> len = elements.length;<\/span><\/code><code><span class=\"code-snippet_outer\">            Object[] newElements = Arrays.copyOf(elements, len + <span class=\"code-snippet__number\">1<\/span>);<\/span><\/code><code><span class=\"code-snippet_outer\">            newElements[len] = e;<\/span><\/code><code><span class=\"code-snippet_outer\">            setArray(newElements);<\/span><\/code><code><span class=\"code-snippet_outer\">            <span class=\"code-snippet__keyword\">return<\/span> <span class=\"code-snippet__keyword\">true<\/span>;<\/span><\/code><code><span class=\"code-snippet_outer\">        }<\/span><\/code><code><span class=\"code-snippet_outer\">    }<\/span><\/code><\/pre>\n<\/section>\n<section style=\"margin-top: 24px; margin-bottom: 16px;\"><span style=\"font-size: 16px;\">\u4ece\u6e90\u7801\u5f88\u5bb9\u6613\u770b\u51faCopyOnWriteArrayList \u5f88\u4e0d\u9002\u5408\u5904\u7406\u5927\u91cf\u5199\u64cd\u4f5c\u7684\u573a\u666f\uff0c\u4e3b\u8981\u539f\u56e0\u5305\u62ec\uff1a<\/span><\/section>\n<ol class=\"list-paddingleft-1\" data-v-md-line=\"11\">\n<li>\n<p data-v-md-line=\"11\"><strong><span style=\"font-size: 16px;\">\u9ad8\u6210\u672c\u7684\u5199\u64cd\u4f5c<\/span><\/strong><span style=\"font-size: 16px;\">\uff1a\u6bcf\u6b21\u4fee\u6539\u90fd\u9700\u8981\u590d\u5236\u6574\u4e2a\u5e95\u5c42\u6570\u7ec4\uff0c\u5982\u679c\u6570\u7ec4\u5927\u5c0f\u5f88\u5927\u6216\u8005\u5199\u64cd\u4f5c\u5f88\u9891\u7e41\uff0c\u8fd9\u5c06\u5bfc\u81f4\u9ad8\u989d\u7684\u5185\u5b58\u5360\u7528\u548cCPU\u8d44\u6e90\u6d88\u8017\uff0c\u5e76\u4e14\u6bcf\u6b21\u5199\u64cd\u4f5c\u90fd\u9700\u8981\u52a0\u9501\u3002<\/span><\/p>\n<\/li>\n<li>\n<section style=\"margin-top: 16px;\"><strong><span style=\"font-size: 16px;\">\u5185\u5b58\u5360\u7528<\/span><\/strong><span style=\"font-size: 16px;\">\uff1a\u4e3a\u4e86\u521b\u5efa\u6570\u7ec4\u7684\u526f\u672c\uff0c\u7cfb\u7edf\u9700\u8981\u989d\u5916\u7684\u5185\u5b58\u7a7a\u95f4\uff1b\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u8fd9\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u6ea2\u51fa\u9519\u8bef\uff08\u5982OutOfMemoryError\uff09\u3002<\/span><\/section>\n<\/li>\n<li>\n<section style=\"margin-top: 16px;\"><strong><span style=\"font-size: 16px;\">\u5783\u573e\u56de\u6536\u538b\u529b<\/span><\/strong><span style=\"font-size: 16px;\">\uff1a\u7531\u4e8e\u9891\u7e41\u590d\u5236\u6570\u7ec4\u4f1a\u4ea7\u751f\u5927\u91cf\u4e0d\u518d\u4f7f\u7528\u7684\u6570\u7ec4\u5bf9\u8c61\uff0c\u8fd9\u589e\u52a0\u4e86\u5783\u573e\u6536\u96c6\u5668\u7684\u5de5\u4f5c\u8d1f\u62c5\uff0c\u53ef\u80fd\u5bfc\u81f4\u66f4\u9891\u7e41\u7684\u5783\u573e\u6536\u96c6\u4e8b\u4ef6\u3002<\/span><\/section>\n<\/li>\n<\/ol>\n<section style=\"margin-top: 16px;\"><span style=\"font-size: 16px;\"><span style=\"font-size: 16px; letter-spacing: 0.578px; text-wrap: wrap;\">\u56e0\u4e3a<span style=\"font-size: 16px; letter-spacing: 0.578px; text-wrap: wrap;\">CopyOnWriteArrayList<\/span><\/span><span style=\"font-size: 16px; letter-spacing: 0.578px; text-wrap: wrap;\">\u8bfb\u53d6\u4e0d\u9700\u8981\u52a0\u9501<\/span><span style=\"font-size: 16px; letter-spacing: 0.578px; text-wrap: wrap;\">\u6240\u4ee5\u5728\u9ad8\u5e76\u53d1\u73af\u5883\u4e2d\u5b83<\/span><span style=\"font-size: 16px; letter-spacing: 0.578px; text-wrap: wrap;\">\u8bfb\u53d6\u64cd\u4f5c\u7684\u6027\u80fd<\/span><span style=\"font-size: 16px; letter-spacing: 0.578px; text-wrap: wrap;\">\u8fdc\u8fdc\u8d85<\/span><span style=\"font-size: 16px; letter-spacing: 0.578px; text-wrap: wrap;\">\u8fc7\u4fee\u6539\u64cd\u4f5c\u6240\u4ee5\u5b83<\/span>\u9002\u7528\u4e8e\u201c<\/span><span style=\"font-size: 20px;\"><strong>\u8bfb\u591a\u5199\u5c11<\/strong><\/span><span style=\"font-size: 16px;\">\u201d\u7684\u573a\u666f\uff0c\u4f8b\u5982\uff1a<\/span><\/section>\n<ul class=\"list-paddingleft-1\" data-v-md-line=\"19\">\n<li><strong><span style=\"font-size: 16px;\">\u4e8b\u4ef6\u76d1\u542c\u5668\u5217\u8868<\/span><\/strong><span style=\"font-size: 16px;\">\uff1a\u901a\u5e38\u6ce8\u518c\u76d1\u542c\u5668\u7684\u64cd\u4f5c\u4e0d\u5982\u89e6\u53d1\u4e8b\u4ef6\u9891\u7e41\u3002<\/span><\/li>\n<li>\n<section style=\"margin-top: 16px;\"><strong><span style=\"font-size: 16px;\">\u5171\u4eab\u914d\u7f6e\u6570\u636e<\/span><\/strong><span style=\"font-size: 16px;\">\uff1a\u914d\u7f6e\u6570\u636e\u5728\u521d\u59cb\u5316\u65f6\u88ab\u52a0\u8f7d\uff0c\u4e4b\u540e\u4e3b\u8981\u8fdb\u884c\u8bfb\u53d6\u800c\u5f88\u5c11\u4fee\u6539\u3002<\/span><\/section>\n<\/li>\n<li style=\"letter-spacing: normal;\">\n<section style=\"margin-top: 16px;\"><strong><span style=\"font-size: 16px; letter-spacing: normal;\">\u53d1\u5e03\/\u8ba2\u9605\u6a21\u5f0f\u4e2d\u7684\u8ba2\u9605\u8005\u5217\u8868<\/span><\/strong><span style=\"font-size: 16px; letter-spacing: normal;\">\uff1a\u5f53\u6d88\u606f\u76f8\u5bf9\u5c11\u91cf\u66f4\u65b0\u65f6\uff0c\u8bfb\u53d6\u64cd\u4f5c\uff08\u5206\u53d1\u6d88\u606f\u7ed9\u8ba2\u9605\u8005\uff09\u5c06\u6bd4\u66f4\u65b0\u8ba2\u9605\u8005\u5217\u8868\u66f4\u9891\u7e41\u3002<\/span><\/section>\n<\/li>\n<\/ul>\n<section style=\"text-align: justify; margin-top: 16px;\"><strong>\u603b\u7ed3<\/strong>\uff1a<span style=\"font-size: 16px;\">\u9009\u62e9\u9002\u5f53\u7684\u6570\u636e\u7ed3\u6784\u5e94\u57fa\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u6027\u80fd\u9700\u6c42\u3002\u5728\u9700\u8981\u9891\u7e41\u5199\u64cd\u4f5c\u7684\u573a\u5408\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u5176\u4ed6\u5e76\u53d1\u96c6\u5408\uff0c\u5982 ConcurrentHashMap \u6216 ConcurrentLinkedQueue\uff0c\u8fd9\u4e9b\u6570\u636e\u7ed3\u6784\u8bbe\u8ba1\u6709\u66f4\u597d\u7684\u5e76\u53d1\u4fee\u6539\u6027\u80fd\u3002<\/span><\/section>\n<p style=\"display: none;\">\n<blockquote><p>\u672c\u7bc7\u6587\u7ae0\u6765\u6e90\u4e8e\u5fae\u4fe1\u516c\u4f17\u53f7: \u4e92\u8054\u7f51\u9762\u8bd5\u5c0f\u5e2e\u624b<\/p><\/blockquote>\n<\/div>\n","raw":"","protected":false},"excerpt":{"rendered":"<p>CopyOnWriteArrayList \u662f\u4e00\u4e2a\u7ebf\u7a0b\u5b89\u5168\u7684 ArrayList \u53d8\u4f53\uff0c\u5b83\u901a\u8fc7\u4ee5\u4e0b\u673a\u5236\u786e\u4fdd\u7ebf\u7a0b\u5b89\u5168\uff1a \u5199\u65f6\u590d\u5236\uff08Co &#8230;<\/p>\n","protected":false},"author":1,"featured_media":4008,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[30],"tags":[],"class_list":["post-4011","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-30"],"post_thumbnail_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524144505373-150x150.jpeg","content_first_image":null,"post_medium_image_300":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524144505373-300x127.jpeg","post_thumbnail_image_624":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524144505373.jpeg","post_frist_image":null,"post_medium_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524144505373-300x127.jpeg","post_large_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524144505373.jpeg","post_full_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524144505373.jpeg","post_all_images":[],"videoAdId":"","listAd":"0","listAdId":"","listAdEvery":6,"total_comments":0,"category_name":"\u9762\u8bd5","post_date":"2024-05-07","like_count":"0","praiseWord":"\u9f13\u52b1","copyright_state":"","excitationAd":"0","rewardedVideoAdId":"","detailAdId":"","detailAd":"0","enterpriseMinapp":"0","audios":[],"postImageUrl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2023\/05\/20230519082947553.jpg","avatarurls":[],"related_posts":null,"pageviews":281,"next_post_id":4007,"next_post_title":"\u9762\u8bd5\u5b98\uff1aArrayList\u548cLinkedList\u5e94\u8be5\u5982\u4f55\u9009\u62e9\uff1f","previous_post_id":4517,"previous_post_title":"\u9762\u8bd5\u5b98\uff1a\u5982\u4f55\u4fdd\u8bc1Redis\u7f13\u5b58\u548c\u6570\u636e\u5e93\u6570\u636e\u7684\u4e00\u81f4\u6027\uff1f\u76f4\u51fb\u7075\u9b42\u7684\u8fde\u73af\u4e03\u95ee\u4f60\u625b\u5f97\u4f4f\u4e48\uff1f\uff08\u4e8c\uff09","_links":{"self":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/4011","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/comments?post=4011"}],"version-history":[{"count":1,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/4011\/revisions"}],"predecessor-version":[{"id":4177,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/4011\/revisions\/4177"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/media\/4008"}],"wp:attachment":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/media?parent=4011"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/categories?post=4011"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/tags?post=4011"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}