{"id":4376,"date":"2019-07-02T11:01:14","date_gmt":"2019-07-02T03:01:14","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=4376"},"modified":"2019-07-02T11:01:14","modified_gmt":"2019-07-02T03:01:14","slug":"%e9%9d%a2%e8%af%95%e5%ae%98%e8%ae%a9%e6%88%91%e5%86%99%e4%b8%80%e4%b8%aa-arraylist-%e7%9a%84%e7%ba%bf%e7%a8%8b%e4%b8%8d%e5%ae%89%e5%85%a8%e7%9a%84bug%ef%bc%8c%e5%b9%b6%e4%bf%ae","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=4376","title":{"rendered":"\u9762\u8bd5\u5b98\u8ba9\u6211\u5199\u4e00\u4e2a ArrayList \u7684\u7ebf\u7a0b\u4e0d\u5b89\u5168\u7684\u201cbug\u201d\uff0c\u5e76\u4fee\u590d\u5b83\uff01"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u90fd\u77e5\u9053\uff0cArrayList \u5929\u751f\u5c31\u4e0d\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u4f46\u662f\u5f88\u591a\u4eba\u4e5f\u5c31\u662f\u9053\u542c\u9014\u8bf4\u800c\u5df2\u3002\u5e76\u6ca1\u6709\u5b9e\u9645\u7684\u8fdb\u884c\u6d4b\u8bd5\uff0c\u4e5f\u5e76\u4e0d\u6e05\u695a ArrayList \u4e3a\u4ec0\u4e48\u4e0d\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff01<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e0d\uff0c\u6628\u5929\u8fd8\u6709\u4eba\u95ee\u6211\u4e3a\u4ec0\u4e48\uff1f\u8bf4\u9762\u8bd5\u5b98\u8ba9\u4ed6\u5199\u4e00\u4e2a bug\u3002\u7b54\u6848\u5176\u5b9e\u5f88\u7b80\u5355\uff0c\u4eca\u5929\u6211\u4eec\u5c31\u4e00\u8d77\u6765\u52a8\u624b\u5199\u4e00\u4e2a\u201cbug\u201d\uff01<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/wx1.sinaimg.cn\/large\/8fa5dcfcgy1g4laxp6iilj20ap07jwfj.jpg\" alt=\"\" \/><figcaption>\u5199\u4e00\u4e2a ArrayList \u7684\u7ebf\u7a0b\u4e0d\u5b89\u5168\u7684\u201cbug\u201d<\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u770b\u4e0b\u9762\u8fd9\u4e2a\u6d4b\u8bd5\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class XttblogUnsafeList {\n    public static void main(String[] args) {\n        \/\/ \u8fdb\u884c 10\u6b21\u6d4b\u8bd5\n        for (int i = 0; i &lt; 10; i++) {\n            test();\n        }\n    }\n    public static void test() {\n        \/\/ \u7528\u6765\u6d4b\u8bd5\u7684List\n        List&lt;Object&gt; list = new ArrayList&lt;Object&gt;();\n        \/\/ \u7ebf\u7a0b\u6570\u91cf(100)\n        int threadCount = 100;\n        \/\/ \u7528\u6765\u8ba9\u4e3b\u7ebf\u7a0b\u7b49\u5f85threadCount\u4e2a\u5b50\u7ebf\u7a0b\u6267\u884c\u5b8c\u6bd5\n        CountDownLatch countDownLatch = new CountDownLatch(threadCount);\n        \/\/ \u542f\u52a8threadCount\u4e2a\u5b50\u7ebf\u7a0b\n        for (int i = 0; i &lt; threadCount; i++) {\n            Thread thread = new Thread(new MyThread(list, countDownLatch));\n            thread.start();\n        }\n        try {\n            \/\/ \u4e3b\u7ebf\u7a0b\u7b49\u5f85\u6240\u6709\u5b50\u7ebf\u7a0b\u6267\u884c\u5b8c\u6210\uff0c\u518d\u5411\u4e0b\u6267\u884c\n            countDownLatch.await();\n        } catch (InterruptedException e) {\n            e.printStackTrace();\n        }\n        \/\/ List \u7684size\n        System.out.println(list.size());\n    }\n}\nclass MyThread implements Runnable {\n    private List&lt;Object&gt; list;\n    private CountDownLatch countDownLatch;\n    public MyThread(List&lt;Object&gt; list, CountDownLatch countDownLatch) {\n        this.list = list;\n        this.countDownLatch = countDownLatch;\n    }\n    public void run() {\n        \/\/ \u6bcf\u4e2a\u7ebf\u7a0b\u5411List\u4e2d\u6dfb\u52a0100\u4e2a\u5143\u7d20\n        for (int i = 0; i &lt; 1000; i++) {\n            list.add(new Object());\n        }\n        \/\/ \u5b8c\u6210\u4e00\u4e2a\u5b50\u7ebf\u7a0b\uff08\u4e3b\u7ebf\u7a0b\u7b49\u5f85\u5b50\u7ebf\u7a0b\u6267\u884c\u5b8c\u4e86\u518d\u6267\u884c\uff09\n        countDownLatch.countDown();\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u591a\u6b21\u6267\u884c\u540e\uff0c\u8fd0\u884c\u7ed3\u679c\u6709\u4e24\u79cd\u3002\u4e00\u79cd\u5c31\u662f\u5e38\u89c1\u7684 java.lang.ArrayIndexOutOfBoundsException \u5f02\u5e38\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Exception in thread \"Thread-100\" java.lang.ArrayIndexOutOfBoundsException: 109\n    at java.util.ArrayList.add(ArrayList.java:463)\n    at com.xttblog.MyThread.run(XttblogUnsafeList.java:52)\n    at java.lang.Thread.run(Thread.java:748)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd8\u6709\u4e00\u79cd\u5c31\u662f\u8dd1\u6210\u529f\u4e86\uff0c\u4f46\u662f\u6570\u636e\u4e0d\u5bf9\u3002\u751a\u81f3\u65f6\u4e0d\u65f6\u4f1a\u629b\u51fa\u4e2a IndexOutOfBoundsException \u5f02\u5e38\uff0c\u8fd9\u79cd\u60c5\u51b5\u5f88\u5c11\u89c1\uff0c\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u90fd\u662f\u76f4\u63a5\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>100000<br> 100000<br> 99847<br> 100000<br> 99416<br> 99442<br> 99998<br> 100000<br> 99271<br> 99926<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">\u89c2\u5bdf\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u5176\u5b9e\u5f88\u7b80\u5355\uff0c\u5c31\u662f\u591a\u7ebf\u7a0b\uff0c\u5411 list \u4e2d add \u5143\u7d20\u3002\u4f46\u662f\uff0c\u6700\u7ec8\u7ed3\u679c\u5374\u548c\u9884\u671f\u4e0d\u4e00\u81f4\u3002\u4e3a\u4ec0\u4e48\u4f1a\u6f0f\u6389\u6570\u636e\u5462\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u662f\u56e0\u4e3a List \u5bf9\u8c61\uff0c\u5728\u505a add \u65f6\uff0c\u6267\u884c Arrays.copyOf \u7684\u65f6\u5019\uff0c\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\u5bf9\u8c61\u3002\u5f53\u6709\u7ebf\u7a0b t1\u3001t2\u3001\u2026 \u540c\u65f6\u8fdb\u5165 grow\u65b9\u6cd5\uff0c\u591a\u4e2a\u7ebf\u7a0b\u90fd\u4f1a\u6267\u884c Arrays.copyOf \u65b9\u6cd5\uff0c\u8fd4\u56de\u591a\u4e2a\u4e0d\u540c\u7684 elementData \u5bf9\u8c61\uff0c\u5047\u5982\uff0ct1 \u5148\u8fd4\u56de\uff0ct2 \u540e\u8fd4\u56de\uff0c\u90a3\u4e48 List.elementData == t1.elementData\uff0c\u7136\u540e t2 \u4e5f\u8fd4\u56de\u540e\uff0c\u8fd9\u65f6 List.elementData == t2.elementData\uff0c\u6240\u4ee5\uff0ct2.elementData \u5c31\u628a t1.elementData \u6570\u636e\u7ed9\u8986\u76d6\u4e86\u3002\u5bfc\u81f4 t1.elementData \u88ab\u4e22\u5931\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u90a3\u4e48\u8fd9\u79cd\u95ee\u9898\uff0c\u600e\u4e48\u89e3\u51b3\u5462\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e00\u79cd\u662f\u4f7f\u7528 Vector\uff0c\u8fd8\u6709\u4e00\u79cd\u662f\u4f7f\u7528 Collections.synchronizedList(new ArrayList()) \u3002\u4f46\u662f\u4f60\u4e5f\u8981\u6ce8\u610f\u7b26\u5408\u64cd\u4f5c\uff01<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u5c31\u662f\uff0c\u9762\u8bd5\u4e2d\uff0c\u8ba9\u6211\u5199\u4e00\u4e2a\u201cbug\u201d\uff0c\u5e76\u4fee\u590d\u4e00\u4e2a\u201cbug\u201d\u3002<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=4376\">\u9762\u8bd5\u5b98\u8ba9\u6211\u5199\u4e00\u4e2a ArrayList \u7684\u7ebf\u7a0b\u4e0d\u5b89\u5168\u7684\u201cbug\u201d\uff0c\u5e76\u4fee\u590d\u5b83\uff01<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u6211\u4eec\u90fd\u77e5\u9053\uff0cArrayList \u5929\u751f\u5c31\u4e0d\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u4f46\u662f\u5f88\u591a\u4eba\u4e5f\u5c31\u662f\u9053\u542c\u9014\u8bf4\u800c\u5df2\u3002\u5e76\u6ca1\u6709\u5b9e\u9645\u7684\u8fdb\u884c\u6d4b\u8bd5\uff0c\u4e5f\u5e76 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[1741,567,202,531,1931],"class_list":["post-4376","post","type-post","status-publish","format-standard","hentry","category-java","tag-arraylist","tag-java-","tag-locked","tag-531","tag-1931"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4376","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4376"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4376\/revisions"}],"predecessor-version":[{"id":4377,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4376\/revisions\/4377"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4376"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4376"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4376"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}