{"id":978,"date":"2021-12-19T18:45:19","date_gmt":"2021-12-19T10:45:19","guid":{"rendered":"https:\/\/blog.cauchyschwarz.com\/?p=978"},"modified":"2021-12-19T18:45:24","modified_gmt":"2021-12-19T10:45:24","slug":"java-concurrency-in-practice%e8%af%bb%e4%b9%a6%e7%ac%94%e8%ae%b0","status":"publish","type":"post","link":"https:\/\/blog.cauchyschwarz.com\/?p=978","title":{"rendered":"Java Concurrency in Practice\u8bfb\u4e66\u7b14\u8bb0"},"content":{"rendered":"\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 ez-toc-wrap-right counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-69e0a7a2ea073\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-69e0a7a2ea073\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=978\/#%E4%B8%80%E3%80%81Introduction\" >\u4e00\u3001Introduction<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=978\/#%E4%BA%8C%E3%80%81Thread_Safety\" >\u4e8c\u3001Thread Safety<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=978\/#%E4%B8%89%E3%80%81Sharing_Objects\" >\u4e09\u3001Sharing Objects<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=978\/#1_Visibility\" >1. Visibility<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=978\/#2_Publication_and_escape%E5%8F%91%E5%B8%83%E4%B8%8E%E9%80%B8%E5%87%BA\" >2. Publication and escape(\u53d1\u5e03\u4e0e\u9038\u51fa)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=978\/#3_Thread_confinement%E7%BA%BF%E7%A8%8B%E5%B0%81%E9%97%AD\" >3. Thread confinement(\u7ebf\u7a0b\u5c01\u95ed)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=978\/#4_Immutability\" >4. Immutability<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=978\/#5_Safe_Publication\" >5. Safe Publication<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=978\/#%E5%9B%9B%E3%80%81Composing_Objects\" >\u56db\u3001Composing Objects<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=978\/#41_Designing_a_thread-safe_class\" >4.1 Designing a thread-safe class<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=978\/#42_Instance_confinement\" >4.2 Instance confinement<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=978\/#43_Delegating_thread_safety\" >4.3 Delegating thread safety<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=978\/#44_Adding_functionality_to_existing_thread-safe_classes\" >4.4 Adding functionality to existing thread-safe classes<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=978\/#45_Documenting_synchronization_policies\" >4.5 Documenting synchronization policies<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=978\/#%E4%BA%94%E3%80%81Building_Blocks\" >\u4e94\u3001Building Blocks<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=978\/#51_Synchronized_collections\" >5.1 Synchronized collections<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=978\/#52_Concurrent_collections\" >5.2 Concurrent collections<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=978\/#ConcurrentHashMap\" >ConcurrentHashMap<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-19\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=978\/#CopyOnWriteArrayList\" >CopyOnWriteArrayList<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-20\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=978\/#Blocking_queues_and_the_producer-consumer_pattern\" >Blocking queues and the producer-consumer pattern<\/a><\/li><\/ul><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h1 class=\"wp-block-heading\" id=\"%E4%B8%80introduction\"><span class=\"ez-toc-section\" id=\"%E4%B8%80%E3%80%81Introduction\"><\/span>\u4e00\u3001Introduction<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<p>Java\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\u9700\u8981\u5173\u6ce8\u7684\u51e0\u4e2a\u95ee\u9898\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Safety<br>\u5728\u591a\u4e2a\u7ebf\u7a0b\u4e2d\u5171\u4eab\u7684\u6570\u636e\u8981\u6b63\u786e\u5730\u540c\u6b65<\/li><li>Liveness<br>\u907f\u514d\u6b7b\u9501\u3001\u6d3b\u9501\u3001\u997f\u6b7b\u8fd9\u4e9b\u95ee\u9898<\/li><li>Performance<br>\u8bbe\u8ba1\u5408\u7406\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u4e0e\u8bbe\u8ba1\u4e0d\u5408\u7406\u7684\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u95f4\u6027\u80fd\u53ef\u80fd\u5b58\u5728\u5de8\u5927\u5dee\u5f02<\/li><\/ul>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"%E4%BA%8Cthread-safety\"><span class=\"ez-toc-section\" id=\"%E4%BA%8C%E3%80%81Thread_Safety\"><\/span>\u4e8c\u3001Thread Safety<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"%E4%B8%89sharing-objects\"><span class=\"ez-toc-section\" id=\"%E4%B8%89%E3%80%81Sharing_Objects\"><\/span>\u4e09\u3001Sharing Objects<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1-visibility\"><span class=\"ez-toc-section\" id=\"1_Visibility\"><\/span>1. Visibility<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u51e0\u4e2a\u503c\u5f97\u6ce8\u610f\u7684\u5c0f\u70b9\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Locking is not just about mutual exclusion; it is also about memory visibility. To ensure that all threads see the most up-to-date values of shared mutable variables, the reading and writing threads must synchronize on a common lock.<\/li><li>\u5bf9\u4e8esetter\u548cgetter\u65b9\u6cd5\u5e94\u8be5\u90fd\u8981\u52a0\u540c\u6b65\u673a\u5236\u4fdd\u62a4\u624d\u80fd\u4fdd\u8bc1getter\u8bfb\u51fa\u6765\u7684\u4e0d\u662f\u65e7\u503c(stale)\u3002\u56e0\u4e3a\u53ea\u6709\u540c\u6b65\u673a\u5236\u624d\u4fdd\u8bc1\u4e86getter\u548csetter\u4e4b\u95f4\u7684happens-before\u5173\u7cfb\u3002<\/li><li><em>out-of-thin-air safety<\/em><br>\u5bf9\u4e8e\u4e0d\u52a0\u540c\u6b65\u673a\u5236\u4fdd\u62a4\u7684\u53d8\u91cf\uff0c\u8bfb\u53d6\u53d8\u91cf\u5f97\u5230\u7684\u503c\u53ef\u80fd\u662f\u65e7\u503c(stale)\uff0c\u4f46\u4e0d\u4f1a\u662f\u4e00\u4e2a\u5176\u4ed6\u7684\u4e0d\u76f8\u5173\u7684\u503c\u3002\u4f46\u662f\u5bf9\u4e8e64\u4f4d\u7684\u6570\u503c\u53d8\u91cf(double\u548clong)\u8fd9\u4e2a\u89c4\u5219\u4e0d\u9002\u7528\uff0c\u56e0\u4e3aJVM\u5bf9\u4e8e\u4e00\u4e2a64\u6570\u636e\u7684\u8bfb\u5199\u64cd\u4f5c\u53ef\u4ee5\u7528\u4e24\u4e2a32\u4f4d\u7684\u64cd\u7eb5\u66ff\u6362\uff0c\u5728\u4f60\u8bfb\u4e86\u4e00\u4e2adouble\u7684\u524d32\u4f4d\u65f6\uff0c\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u5199\u4e86\u8fd9\u4e2adouble\u7684\u540e32\u4f4d\uff0c\u7136\u540e\u4f60\u518d\u8bfb\u53d6\u8fd9\u540e32\u4f4d\uff0c\u7ec4\u5408\u51fa\u6765\u7684\u8fd9\u4e2adouble\u5c31\u4e0d\u662f\u4e00\u4e2a\u65e7\u503c\uff0c\u800c\u662f\u4e00\u4e2a\u5b8c\u5168\u9519\u8bef\u7684\u503c\u3002<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"2-publication-and-escape%E5%8F%91%E5%B8%83%E4%B8%8E%E9%80%B8%E5%87%BA\"><span class=\"ez-toc-section\" id=\"2_Publication_and_escape%E5%8F%91%E5%B8%83%E4%B8%8E%E9%80%B8%E5%87%BA\"><\/span>2. Publication and escape(\u53d1\u5e03\u4e0e\u9038\u51fa)<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong>Publishing<\/strong>&nbsp;an object means making it available to code outside of its current scope, such as by storing a reference to it where other code can find it, returning it from a nonprivate method, or passing it to a method in another class.<br>\u4e00\u4e2a\u5bf9\u8c61\u5728\u5176\u6240\u5904\u4f5c\u7528\u57df\u4e4b\u5916\u53ef\u4ee5\u88ab\u8bbf\u95ee\u5c31\u79f0\u4e3a\u53d1\u5e03(\u8fd9\u4e2a\u5bf9\u8c61)\u3002\u6bd4\u5982\u4fdd\u5b58\u5bf9\u8c61\u7684\u5f15\u7528\uff0c\u4f7f\u5f97\u5176\u4ed6\u5904\u7684\u4ee3\u7801\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e2a\u5f15\u7528\u8bbf\u95ee\u8fd9\u4e2a\u5bf9\u8c61\uff1b\u901a\u8fc7\u975e\u79c1\u6709\u65b9\u6cd5\u8fd4\u56de\u7684\u5bf9\u8c61\uff1b\u5c06\u5bf9\u8c61\u4f20\u5165\u5176\u4ed6\u7c7b\u7684\u65b9\u6cd5\u3002<\/p><\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>An object that is published when it should not have been is said to have&nbsp;<strong>escaped<\/strong>.<br>\u4e0d\u8be5\u53d1\u5e03\u7684\u5bf9\u8c61\u88ab\u53d1\u5e03\u4e86\u5c31\u79f0\u4e3a\u9038\u51fa\u3002<\/p><\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Publishing an object also publishes any objects referred to by its nonprivate fields. More generally, any object that is&nbsp;<em>reachable<\/em>&nbsp;from a published object by following some chain of nonprivate field references and method calls has also been published.<br>\u53d1\u5e03\u4e00\u4e2a\u5bf9\u8c61\u7684\u540c\u65f6\u4e5f\u53d1\u5e03\u4e86\u53ef\u4ee5\u901a\u8fc7\u8be5\u5bf9\u8c61\u7684\u975e\u79c1\u6709\u65b9\u6cd5\u3001\u57df\u53ef\u4ee5\u8bbf\u95ee\u7684\u5bf9\u8c61\u3002\u4e00\u822c\u5730\uff0c\u5982\u679c\u4e00\u4e2a\u5bf9\u8c61\u53ef\u4ee5\u4ece\u67d0\u4e2a\u88ab\u53d1\u5e03\u7684\u5bf9\u8c61\u5f00\u59cb\u901a\u8fc7\u67d0\u6761\u975e\u79c1\u6709\u57df\u3001\u975e\u79c1\u6709\u5bf9\u8c61\u7684\u94fe\u8def\u8bbf\u95ee\u5230\uff0c\u90a3\u4e48\u8be5\u5bf9\u8c61\u4e5f\u88ab\u53d1\u5e03\u3002<\/p><\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>From the perspective of a class C, an&nbsp;<strong>alien<\/strong>&nbsp;method is one whose behavior is not fully specified by C. This includes methods in other classes as well as overrideable methods (neither private nor final) in C itself. Passing an object to an alien method must also be considered publishing that object.<br>\u4ece\u4e00\u4e2a\u7c7bC\u7684\u89d2\u5ea6\u6765\u770b\uff0c\u4e00\u4e2a<strong>alien<\/strong>&nbsp;\u65b9\u6cd5\u6307\u7684\u662f\u4e0d\u662f\u5b8c\u5168\u7531\u8be5\u7c7b\u81ea\u5df1\u5b9a\u4e49\u7684\u65b9\u6cd5\uff0c<strong>alien<\/strong>\u65b9\u6cd5\u5305\u62ec\u4e86\u5176\u4ed6\u7c7b\u7684\u65b9\u6cd5\u3001\u53ef\u4ee5\u91cd\u8f7d\u7684\u65b9\u6cd5(\u53ea\u8981\u4e0d\u662fprivate\u548cfinal\u7684\u65b9\u6cd5\u90fd\u53ef\u4ee5\u91cd\u8f7d)\u3002\u5c06\u5bf9\u8c61\u4f20\u7ed9<strong>alien<\/strong>\u65b9\u6cd5\u4e5f\u662f\u5728\u53d1\u5e03\u8be5\u5bf9\u8c61\u3002<\/p><\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Once an object escapes, you have to assume that another class or thread may, maliciously or carelessly, misuse it.<br>\u5c06\u5bf9\u8c61\u4f20\u7ed9alien\u65b9\u6cd5\u4e4b\u540e\uff0c\u65e0\u6cd5\u63a7\u5236alien\u65b9\u6cd5\u4f1a\u5bf9\u8be5\u5bf9\u8c61\u505a\u51fa\u4ec0\u4e48\u3002<\/p><\/blockquote>\n\n\n\n<p>\u5982\u679c\u4e00\u4e2a\u533f\u540d\u5185\u90e8\u7c7b\u7684\u5b9e\u4f8b\u88abpublish\uff0c\u7531\u4e8e\u8be5\u5b9e\u4f8b\u4f1a\u9690\u5f0f\u5f15\u7528\u4e00\u4e2a\u5916\u90e8\u7c7b\u7684<em>this<\/em>\u6307\u9488\uff0c\u56e0\u6b64\u76f8\u5e94\u7684\u5916\u90e8\u7c7b\u5bf9\u8c61\u4e5f\u88abpublish\u3002<\/p>\n\n\n\n<p>\u4e00\u4e2a\u5bf9\u8c61\u53ea\u6709\u5728\u5b83\u7684\u6784\u9020\u51fd\u6570\u8fd4\u56de\u4e4b\u540e\u624d\u4f1a\u5904\u4e8e\u4e00\u4e2apredictable\u3001consistent\u7684\u72b6\u6001\uff0c\u6240\u4ee5\u4e00\u4e2a\u5bf9\u8c61\u5728\u81ea\u5df1\u7684\u6784\u9020\u51fd\u6570\u4e2d\u88abpublish\u65f6\uff0c\u4f1apublish\u4e00\u4e2a\u672a\u5b8c\u6574\u6784\u5efa\u7684\u5bf9\u8c61(\u81ea\u5df1)\uff0c\u5373\u4f7f\u8fd9\u4e2apublish\u53d1\u751f\u5728\u6784\u9020\u51fd\u6570\u7684\u6700\u540e\u4e00\u6761\u8bed\u53e5\u3002\u5bf9\u4e8e\u8fd9\u79cd<em>this<\/em>\u5f15\u7528\u5728\u6784\u9020\u51fd\u6570\u91ccescape\u7684\u60c5\u5f62\uff0c\u8be5\u5bf9\u8c61\u88ab\u8ba4\u4e3a\u662f<em>not properly constructed<\/em>\u3002\u4e0b\u9762\u7684ThisEscape\u5c31\u662f\u8fd9\u6837\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">public class ThisEscape {\n\tpublic ThisEscape(EventSource source) {\n\t\tsource.registerListener(\n\t\t\tnew EventListener() {\n\t\t\t\tpublic void onEvent(Event e) {\n\t\t\t\t\tdoSomething(e);\n\t\t\t\t}\n\t\t\t});\n\t}\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Do not allow&nbsp;<em>this<\/em>&nbsp;reference to escape during construction.<\/strong><\/p>\n\n\n\n<p>\u4e00\u4e2a\u5e38\u89c1\u7684\u8fd9\u79cd\u7c7b\u578b\u7684\u9519\u8bef\u662f\u5728\u6784\u9020\u51fd\u6570\u4e2d\u5f00\u59cb\u4e00\u4e2a\u65b0\u7684\u7ebf\u7a0b\u3002\u5728\u6784\u9020\u51fd\u6570\u4e2d\u521b\u5efa\u65b0\u7684\u7ebf\u7a0b\u672c\u8eab\u4e5f\u6ca1\u6709\u9519\uff0c\u4f46\u662f\u6700\u597d\u4e0d\u8981\u4e5f\u5728\u6784\u9020\u51fd\u6570\u4e2d\u5f00\u59cb\u8fd0\u884c\u8fd9\u4e2a\u7ebf\u7a0b\uff0c\u56e0\u4e3a\u5982\u679c\u4f60\u5728\u6784\u9020\u51fd\u6570\u4e2d\u5f00\u59cb\u8fd0\u884c\u8fd9\u4e2a\u7ebf\u7a0b\uff0c\u800c\u7531\u4e8e\u6b64\u65f6\u8be5\u5bf9\u8c61\u8fd8\u5904\u4e8e\u672a\u5b8c\u6210\u6784\u9020\u7684\u72b6\u6001\uff0c\u90a3\u4e48\u5728\u8be5\u7ebf\u7a0b\u8fd0\u884c\u7684\u8fc7\u7a0b\u4e2d\u5c31\u53ef\u80fd\u4f1a\u51fa\u73b0\u4e9b\u95ee\u9898\u3002<\/p>\n\n\n\n<p>\u50cf\u5b9e\u73b0\u548c\u4e0a\u9762\u4ee3\u7801\u540c\u6837\u7684\u529f\u80fd\u4f46\u662f\u540c\u65f6\u53c8\u4e0d\u8ba9<em>this<\/em>\u5f15\u7528escape\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">public class SafeListener {\n\tprivate final EventListener listener;\n\n\tprivate SafeListener() {\n\t\tlistener = new EventListener() {\n\t\t\tpublic void onEvent(Event e) {\n\t\t\t\tdoSomething(e);\n\t\t\t}\n\t\t};\n\t}\n\n\tpublic static SafeListener newInstance(EventSource source) {\n\t\tSafeListener safe = new SafeListener();\n\t\tsource.registerListener(safe.listener);\n\t\treturn safe;\n\t}\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"3-thread-confinement%E7%BA%BF%E7%A8%8B%E5%B0%81%E9%97%AD\"><span class=\"ez-toc-section\" id=\"3_Thread_confinement%E7%BA%BF%E7%A8%8B%E5%B0%81%E9%97%AD\"><\/span>3. Thread confinement(\u7ebf\u7a0b\u5c01\u95ed)<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u9650\u5236\u4e00\u4e2a\u5bf9\u8c61\u53ea\u80fd\u7531\u4e00\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u3002\u8fd9\u7531\u5f00\u53d1\u8005\u8d1f\u8d23\u5b9e\u73b0\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Ad-hoc thread confinement<br>\u5b8c\u5168\u7531\u7a0b\u5e8f\u5b9e\u73b0\u4fdd\u8bc1\u7684thread confinement\uff0c\u4e0d\u662f\u7531\u7f16\u7a0b\u8bed\u8a00\u7279\u6027\u8f85\u52a9\u5b9e\u73b0\u7684\u3002<\/li><li>Stack confinement<br>\u53ea\u8ba9\u5c40\u90e8\u53d8\u91cf\u8bbf\u95ee\u7684\u5bf9\u8c61\u5bf9\u5c40\u90e8\u53d8\u91cf\u6240\u5c5e\u7684\u7ebf\u7a0b\u5c01\u95ed\u3002<\/li><li>\u7528<strong>ThreadLocal<\/strong>\u7c7bThread-local variables are often used to prevent sharing in designs based on mutable Singletons or global variables.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"4-immutability\"><span class=\"ez-toc-section\" id=\"4_Immutability\"><\/span>4. Immutability<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Immutable objects are always thread-safe.<\/p><\/blockquote>\n\n\n\n<p>immutability\u4e0d\u7b49\u4e8e\u5c06\u5bf9\u8c61\u7684\u6240\u6709\u57df\u90fd\u58f0\u660e\u4e3afinal\uff0c\u56e0\u4e3afinal\u57df\u53ef\u80fd\u6301\u6709mutable\u5bf9\u8c61\u7684\u5f15\u7528\uff0c\u5982\u679c\u4fee\u6539\u4e86\u8be5final\u57df\u6307\u5411\u7684mutable\u5bf9\u8c61\uff0c\u90a3\u4e48\u8fd9\u4e2a\u5bf9\u8c61\u7684\u72b6\u6001\u4e5f\u5c31\u6539\u53d8\u4e86\u3002<\/p>\n\n\n\n<p>\u4e00\u4e2a\u5bf9\u8c61\u662fimmutable\u5982\u679c\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u6784\u9020\u5b8c\u6210\u540e\u8be5\u5bf9\u8c61\u72b6\u6001\u4e0d\u80fd\u88ab\u4fee\u6539\uff1b<\/li><li>\u6240\u6709\u7684\u57df\u662ffinal(\u6709\u4e00\u4e2a\u7279\u4f8b\u662fString)\uff1b<\/li><li>properly constructed(the this reference does not escape during construction)<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"5-safe-publication\"><span class=\"ez-toc-section\" id=\"5_Safe_Publication\"><\/span>5. Safe Publication<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>\u5bf9\u4e8e\u9700\u8981\u5728\u591a\u7ebf\u7a0b\u4e2d\u8fd0\u884c\u7684\u4ee3\u7801\uff0c\u5373\u4f7f\u7b80\u5355\u7684\u5bf9\u8c61\u53d1\u5e03\uff0c\u4e5f\u9700\u8981\u8003\u8651\u6dfb\u52a0\u540c\u6b65\u673a\u5236<\/li><li>\u4f46\u662fimmutable\u5bf9\u8c61\u53d7\u5230Java Memory Model\u7684\u4f18\u5f85<br>Java Memory Model\u4e3aimmutable\u5bf9\u8c61\u63d0\u4f9b\u7279\u4f7f\u7684<em>initialization safety<\/em>\u4fdd\u969c\u673a\u5236\u3002\u5373\u4f7f<em>not properly constructed<\/em>\uff0cimmutable\u5bf9\u8c61\u4e5f\u53ef\u4ee5\u88ab\u5b89\u5168\u5730\u8bbf\u95ee\u3002<strong>Immutable objects can be used safely by any thread without additional synchronization, even when synchronization is not used to pubilsh them.<\/strong><br>This guarantee extends to the values of all final fields of properly constructed objects; final fields can be safely accessed without additional synchronization. However, if final fields refer to mutable objects, synchronization is still required to access the state of the objects they refer to.<\/li><li>Safe publicatioin idiomsTo publish an object safely, both the reference to the object and the object&#8217;s state must be made visible to other threads at the same time. A properly constructed object can be safely published by:<ul><li>Initializing an object reference from a static initializer;<\/li><li>Storing a reference to it into a volatile field or AtomicReference;<\/li><li>Storing a reference to it into a final field of a properly constructed object; or<\/li><li>Storing a reference to it into a field that is properly guarded by a lock.<\/li><\/ul><\/li><li>Effectively immutable objects<br>\u5982\u679c\u4e00\u4e2a\u5bf9\u8c61\u5728\u6784\u9020\u597d\u540e\uff0c\u5176\u72b6\u6001\u5c31\u4e0d\u518d\u6539\u53d8\uff0c\u90a3\u4e48\u8be5\u5bf9\u8c61\u5c31\u662feffectively immutable\uff0c\u8fd9\u79cd\u5bf9\u8c61\u53ea\u9700\u8981\u505a\u5230safe publication(\u5728\u5176\u5f15\u7528\u5bf9\u5176\u4ed6\u7ebf\u7a0b\u53ef\u89c1\u65f6\u8be5\u5bf9\u8c61\u72b6\u6001\u5df2\u7ecf\u8fbe\u5230\u7a33\u5b9a)\uff0c\u4e0d\u9700\u8981\u5176\u4ed6\u7684\u540c\u6b65\u673a\u5236\u6765\u4fdd\u62a4\u5bf9\u8be5\u5bf9\u8c61\u7684\u8bbf\u95ee\u3002<\/li><li>Mutable objects<br>\u5bf9\u8c61\u7684mutability\u4e0d\u540c\uff0c\u53d1\u5e03\u7684\u8981\u6c42\u4e5f\u4e0d\u540c\uff1a<ul><li>Immutable\u5bf9\u8c61\u53ef\u4ee5\u968f\u610f\u53d1\u5e03<\/li><li>Effectively immutable\u7684\u5bf9\u8c61\u9700\u8981\u5b89\u5168\u5730\u53d1\u5e03<\/li><li>Mutable\u5bf9\u8c61\u5fc5\u987b\u5b89\u5168\u53d1\u5e03\uff0c\u800c\u4e14\u5fc5\u987b\u662f\u7ebf\u7a0b\u5b89\u5168\u6216\u8005\u7528\u9501\u4fdd\u62a4<\/li><\/ul><\/li><li>\u5b89\u5168\u5730\u5171\u4eab\u53d8\u91cf<br>\u5728\u5e76\u884c\u7a0b\u5e8f\u4e2d\u6700\u6709\u6548\u7684\u5171\u4eab\u53d8\u91cf\u7684\u65b9\u6cd5\u6709\uff1a<ul><li><strong>Thread-confined<\/strong>A thread-confined object is owned exclusively by and confined to one thread, and can be modified by its owning thread.<\/li><li><strong>Shared read-only<\/strong>A shared read-only object can be accessed concurrently by multiple threads without additional synchronization, but cannot be modified by any thread. Shared read-only objects include immutable and effectively immutable objects.<\/li><li><strong>Shared thread-safe<\/strong>A thread-safe object performs synchronization internally, so multiple threads can freely access it through its public interface without further synchronization.<\/li><li><strong>Guarded<\/strong>A guarded object can be accessed only with a specific lock held. Guarded objects include those that are encapsulated within other thread-safe objects and published objects that are known to be guarded by a specific lock.<\/li><\/ul><\/li><\/ul>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"%E5%9B%9Bcomposing-objects\"><span class=\"ez-toc-section\" id=\"%E5%9B%9B%E3%80%81Composing_Objects\"><\/span>\u56db\u3001Composing Objects<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"41-designing-a-thread-safe-class\"><span class=\"ez-toc-section\" id=\"41_Designing_a_thread-safe_class\"><\/span>4.1 Designing a thread-safe class<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The design process for a thread-safe class should include these three basic elements:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Identify the variables that form the object&#8217;s state(\u627e\u5230\u6784\u6210\u8be5\u5bf9\u8c61\u72b6\u6001\u7684\u53d8\u91cf);<\/li><li>Identify the invariants that constrain the state variables;<\/li><li>Establish a policy for managing concurrent access to the object&#8217;s state(<em>synchronization policy<\/em>\u9610\u8ff0\u4e86immutability,thread confinement,locking\u662f\u5982\u4f55\u88ab\u7528\u6765\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u4e3a\u4e86\u65b9\u4fbf\u7a0b\u5e8f\u7684\u5206\u6790\u548c\u7ef4\u62a4\uff0c\u5e94\u8be5\u4e3a<em>synchronization policy<\/em>\u5199\u6587\u6863).<\/li><\/ul>\n\n\n\n<p>\u5bf9\u4e8e\u4e0a\u9762\u7684\u7b2c\u4e8c\u70b9invariant\u7684\u89e3\u91ca\uff1a<br>\u5176\u5b9e\u5c31\u662f\u8981\u4fdd\u8bc1\u5bf9\u8c61\u603b\u662f\u6709\u4e00\u4e2a\u5408\u7406\u7684\u6b63\u786e\u7684\u503c\uff0c\u4e0d\u8981\u88ab\u8d4b\u503c\u4e3a\u65e0\u6548\u7684\u503c\u3002<br>\u5206\u4e3a\u4e24\u70b9\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>\u5982\u679c\u5bf9\u8c61\u5728\u67d0\u4e2a\u64cd\u4f5c\u7684\u4e2d\u95f4\u8fc7\u7a0b\u5176\u503c\u4f1a\u5904\u4e8einvalid(\u65e0\u6548)\u72b6\u6001(\u6bd4\u5982\u8be5\u5bf9\u8c61\u6709\u5f88\u591a\u4e2a\u57df\uff0c\u67d0\u4e2a\u64cd\u4f5c\u4fee\u6539\u4e86\u5176\u4e2d\u7684\u67d0\u4e9b\u57df\uff0c\u5728\u8fd9\u4e2a\u64cd\u4f5c\u5df2\u7ecf\u4fee\u6539\u4e86\u4e00\u4e9b\u57df\u4f46\u662f\u8fd8\u6ca1\u6709\u5b8c\u6210\u4fee\u6539\u65f6\u8fd9\u4e2a\u5bf9\u8c61\u5c31\u5904\u4e8e\u65e0\u6548\u72b6\u6001)\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u5e94\u8be5\u5c06\u8fd9\u4e2a\u64cd\u4f5c\u8bbe\u4e3a\u539f\u5b50\u64cd\u4f5c\u4ee5\u907f\u514d\u5bf9\u8c61\u53d6\u8fd9\u4e9b\u4e2d\u95f4\u72b6\u6001\u503c\u7684\u65f6\u5019\u88ab\u8bbf\u95ee\u5230\u3002<\/li><li>\u5982\u679c\u5bf9\u8c61\u4e0d\u80fd\u53d6\u67d0\u4e9b\u503c\u6216\u8005\u4e00\u5957\u903b\u8f91\u6765\u51b3\u5b9a\u5bf9\u8c61\u5e94\u8be5\u53d6\u4ec0\u4e48\u6837\u7684\u503c\uff0c\u90a3\u4e48\u5c31\u5e94\u8be5\u5bf9\u4fee\u6539\u5bf9\u8c61\u503c\u7684\u884c\u4e3a\u8fdb\u884c\u5c01\u88c5\uff0c\u9632\u6b62\u6709\u4eba\u7ed9\u8be5\u5bf9\u8c61\u8bbe\u7f6e\u4e00\u4e9b\u4e71\u4e03\u516b\u7cdf\u4e0d\u8be5\u53d6\u7684\u503c\u3002<\/li><\/ol>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong>You cannot ensure thread safety without understanding an object&#8217;s invariants and postconditions. Constraints on the valid values or state transitions for state variables can create atomicity and encapsulation requirements.<\/strong><\/p><\/blockquote>\n\n\n\n<p>postconditions: \u5bf9\u5bf9\u8c61\u7684\u72b6\u6001\u4f5c\u4fee\u6539\u540e\u5e94\u8be5\u6ee1\u8db3\u7684\u8981\u6c42\uff1b<br>preconditions: \u5bf9\u5bf9\u8c61\u7684\u72b6\u6001\u4f5c\u4fee\u6539\u524d\u5e94\u8be5\u6ee1\u8db3\u7684\u8981\u6c42\uff1b<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"42-instance-confinement\"><span class=\"ez-toc-section\" id=\"42_Instance_confinement\"><\/span>4.2 Instance confinement<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"43-delegating-thread-safety\"><span class=\"ez-toc-section\" id=\"43_Delegating_thread_safety\"><\/span>4.3 Delegating thread safety<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>If a class is composed of multiple&nbsp;<em>independent<\/em>&nbsp;thread-safe state variables and has no operations that have any invalid state transitions, then it can delegate thread safety to the underlying state variables.<\/p><\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>If a state variable is thread-safe, does not participate in any invariants that constrain its value, and has no prohibited state transitions for any of its operations, then it can safely be published.<\/p><\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"44-adding-functionality-to-existing-thread-safe-classes\"><span class=\"ez-toc-section\" id=\"44_Adding_functionality_to_existing_thread-safe_classes\"><\/span>4.4 Adding functionality to existing thread-safe classes<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u7528\u7ee7\u627f\u7684\u65b9\u5f0f\u6765\u4e3a\u5df2\u6709\u7684\u7ebf\u7a0b\u5b89\u5168\u7c7b\u6dfb\u52a0\u65b0\u7684\u63a5\u53e3\u5e76\u4e0d\u592a\u597d\uff0c\u56e0\u4e3a\u4f60\u5fc5\u987b\u6e05\u695a\u5730\u7406\u89e3\u539f\u6765\u7684\u7c7b\u662f\u5982\u4f55\u5b9e\u73b0\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u539f\u6765\u7684\u7c7b\u4e0d\u4e00\u5b9a\u63d0\u4f9b\u6e90\u4ee3\u7801\u6216\u8005\u76f8\u5173\u6587\u6863\u6765\u8bf4\u660e\u5176\u7ebf\u7a0b\u5b89\u5168\u7279\u6027\u5b9e\u73b0\u539f\u7406\uff0c\u800c\u4e14\u539f\u6765\u7684\u7c7b\u53ef\u80fd\u5728\u65b0\u7684\u7248\u672c\u4e2d\u6539\u53d8\u5176\u7ebf\u7a0b\u5b89\u5168\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002\u66f4\u597d\u7684\u65b9\u6cd5\u662f\u7528\u7ec4\u5408\uff0c\u5c06\u539f\u6765\u7684\u7ebf\u7a0b\u5b89\u5168\u7684\u7c7b\u4f5c\u4e3a\u6210\u5458\u53d8\u91cf\uff0c\u5b9e\u73b0\u4e00\u4e2a\u8f85\u52a9\u7684\u7ebf\u7a0b\u5b89\u5168\u7684\u7c7b(wrapper\u3001helper)\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"45-documenting-synchronization-policies\"><span class=\"ez-toc-section\" id=\"45_Documenting_synchronization_policies\"><\/span>4.5 Documenting synchronization policies<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Document a class&#8217;s thread safety guarantees for its clients; document its synchronization policy for its maintainers.<\/p><\/blockquote>\n\n\n\n<p>\u4e00\u4e2a\u6bd4\u8f83\u6709\u7528\u7684\u6280\u5de7\u662f\u4f7f\u7528<code>Guardedby<\/code>\u6ce8\u89e3\uff0c\u65b9\u4fbf\u8ba9\u4eba\u77e5\u9053\u8be5\u53d8\u91cf\u9700\u8981\u7528\u67d0\u4e2a\u9501\u540c\u6b65\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"%E4%BA%94building-blocks\"><span class=\"ez-toc-section\" id=\"%E4%BA%94%E3%80%81Building_Blocks\"><\/span>\u4e94\u3001Building Blocks<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"51-synchronized-collections\"><span class=\"ez-toc-section\" id=\"51_Synchronized_collections\"><\/span>5.1 Synchronized collections<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>\u7528\u5de5\u5382\u65b9\u6cd5Collections.synchronizedXxx\u521b\u9020\u7684\u96c6\u5408\u5bf9\u8c61(\u5305\u62ecVector,Hashtable\u7b49)\u3002\u8fd9\u4e9b\u96c6\u5408\u5bf9\u8c61\u81ea\u8eab\u63d0\u4f9b\u7684\u63a5\u53e3\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u4f46\u662f\u5982\u679c\u6211\u4eec\u60f3\u5bf9\u5176\u505a\u4e00\u4e9b\u7ec4\u5408\u6027\u8d28\u7684\u64cd\u4f5c(\u542b\u6709\u591a\u4e2a\u6b65\u9aa4\u7684\u6539\u52a8\uff0c\u6bd4\u5982\u904d\u5386\u4e00\u4e2a\u96c6\u5408\u4e2d\u7684\u5bf9\u8c61\u8fd9\u79cd\u64cd\u4f5c)\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u5e94\u8be5\u624b\u52a8\u540c\u6b65(client-side locking)\u3002<\/li><li>\u8fed\u4ee3\u5668\u4e0eConcurrentModificationException<br>\u5bf9\u4e8e\u8fd9\u79cdsynchronized collections\uff0c\u5728\u53d6\u5f97\u5176iterator\u4e4b\u540e\uff0c\u5728\u6bcf\u6b21\u8c03\u7528iterator.next()\u6216\u8005iterator.hasNext()\u65b9\u6cd5\u7684\u65f6\u5019\uff0c\u90fd\u4f1a\u68c0\u67e5\u8be5\u96c6\u5408\u662f\u5426\u88ab\u4fee\u6539(\u8fd9\u79cd\u68c0\u67e5\u662f\u4e0d\u4fdd\u8bc1\u51c6\u786e\u7684\uff0c\u5373\u6709\u53ef\u80fd\u8be5\u96c6\u5408\u88ab\u4fee\u6539\u4e86\u4f46\u662f\u672a\u68c0\u6d4b\u51fa\u6765)\u3002\u5982\u679c\u8be5\u96c6\u5408\u88ab\u68c0\u6d4b\u51fa\u4fee\u6539\uff0c\u5219\u4f1a\u629b\u51faConcurrentModificationException\u5f02\u5e38\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u95ee\u9898\u7684\u51fa\u73b0\uff0c\u8981\u4e48\u5bf9synchronized collections\u8fed\u4ee3\u904d\u5386\u65f6\u52a0\u9501\uff0c\u8981\u4e48clone\u8be5\u96c6\u5408\u5f97\u5230\u4e00\u4e2a\u7ebf\u7a0b\u5c01\u95ed\u7684\u96c6\u5408\uff0c\u7136\u540e\u5bf9clone\u7684\u96c6\u5408\u8fdb\u884c\u904d\u5386\u3002<\/li><li>Hidden Iterators<br>\u6ce8\u610f\u6709\u4e9b\u64cd\u4f5c\u662f\u9690\u5f0f\u5730\u4f7f\u7528\u4e86\u8fed\u4ee3\u5668\u904d\u5386\u96c6\u5408\u7684\uff0c\u6bd4\u5982\u96c6\u5408\u7684toString\u64cd\u4f5c\u3002\u56e0\u6b64\u8fd9\u4e9b\u5730\u65b9\u4e5f\u53ef\u80fd\u629b\u51faConcurrentModificationException\u5f02\u5e38\uff0c\u8981\u5f02\u5e38\u5c0f\u5fc3\u3002<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"52-concurrent-collections\"><span class=\"ez-toc-section\" id=\"52_Concurrent_collections\"><\/span>5.2 Concurrent collections<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Java5.0\u5f15\u5165\u4e86concurrent\u96c6\u5408\uff0c\u76f8\u6bd4\u4e8e\u4e4b\u524d\u7684synchronized\u96c6\u5408\u589e\u52a0\u4e86\u5bf9\u96c6\u5408\u64cd\u4f5c\u7684throughput\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"concurrenthashmap\"><span class=\"ez-toc-section\" id=\"ConcurrentHashMap\"><\/span>ConcurrentHashMap<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<ol class=\"wp-block-list\"><li>\u8fd9\u5f97\u76ca\u4e8e\u6539\u8fdb\u7684locking strategy\uff0c\u4e4b\u524d\u7684synchronized collections\u5728\u6240\u6709\u7684\u65b9\u6cd5\u4e0a\u4f7f\u7528\u540c\u4e00\u4e2a\u9501\u8fdb\u884c\u540c\u6b65\uff0c\u800cconcurrent collections\u4f7f\u7528\u66f4\u7ec6\u7c92\u5ea6\u7684<strong>lock stripping<\/strong>\u3002<\/li><li>concurrent collections\u7684iterator\u662f<em>weakly consistent<\/em>\u800c\u4e0d\u662f\u5982synchronized collections\u7684iteraotr\u90a3\u6837<em>fail-fast<\/em>\u3002\u4e5f\u5c31\u662f\u8bf4\u5728\u7528\u8fed\u4ee3\u5668\u904d\u5386\u96c6\u5408\u65f6\u8be5\u96c6\u5408\u53ef\u4ee5\u88ab\u4fee\u6539\uff0c\u53ea\u662f\u7528\u8be5\u8fed\u4ee3\u5668\u904d\u5386\u7684\u7ed3\u679c\u57fa\u672c\u4e0a\u662f\u83b7\u5f97\u8be5\u8fed\u4ee3\u5668\u65f6\u96c6\u5408\u7684\u6837\u5b50(&#8220;\u57fa\u672c\u4e0a&#8221;\u7684\u610f\u601d\u662f\u904d\u5386\u7684\u7ed3\u679c\u4e5f\u53ef\u80fd\u53cd\u6620\u8be5\u96c6\u5408\u88ab\u4fee\u6539\u540e\u7684\u6837\u5b50)\u3002<\/li><li>\u50cfsize\u3001isEmpty\u7b49\u64cd\u4f5c\u88ab\u5f31\u5316\u4e3a\u5bf9\u96c6\u5408\u5bf9\u8c61\u7684estimate\u3002\u56e0\u4e3a\u8bbe\u8ba1\u8005\u8ba4\u4e3aconcurrent collections\u7684\u66f4\u91cd\u8981\u7684\u64cd\u4f5c\u662fget\u3001put\u3001containtsKey\u548cremove\u7b49\u3002<\/li><\/ol>\n\n\n\n<p>\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b(\u53ea\u8981\u4f60\u4e0d\u662f\u4e00\u5b9a\u9700\u8981\u5bf9\u96c6\u5408\u7684\u4e92\u65a5\u8bbf\u95ee)\uff0c\u7528ConcurrentHashMap\u66ff\u6362synchronizedMap\u3001Hashtable\u4f1a\u53d6\u5f97\u66f4\u597d\u7684\u6027\u80fd\u3001scalability\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"copyonwritearraylist\"><span class=\"ez-toc-section\" id=\"CopyOnWriteArrayList\"><\/span>CopyOnWriteArrayList<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"blocking-queues-and-the-producer-consumer-pattern\"><span class=\"ez-toc-section\" id=\"Blocking_queues_and_the_producer-consumer_pattern\"><\/span>Blocking queues and the producer-consumer pattern<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Executor\u4f7f\u7528\u4e86blocking queue(\u5b58\u7591)\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Bounded queues are a powerful resource management tool for building reliable appplications: they make your program more robust to overload by throttling activities that threaten to produce more work than can be handled.<\/p><\/blockquote>\n\n\n\n<p><em>Serial thread confinement<\/em>\uff1a\u5373\u4fdd\u8bc1\u4e00\u4e2a\u5bf9\u8c61\u540c\u65f6\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u4f1a\u4fee\u6539\u5b83\u7684\u7ebf\u7a0b\u5c01\u95ed\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001Introduction Java\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\u9700\u8981\u5173\u6ce8\u7684\u51e0\u4e2a\u95ee\u9898\uff1a Safety\u5728\u591a\u4e2a\u7ebf\u7a0b\u4e2d\u5171\u4eab\u7684\u6570\u636e\u8981\u6b63\u786e\u5730\u540c\u6b65 Liveness\u907f\u514d\u6b7b\u9501\u3001\u6d3b\u9501\u3001\u997f\u6b7b\u8fd9\u4e9b\u95ee\u9898 Performance\u8bbe\u8ba1\u5408\u7406\u591a\u7ebf&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,6],"tags":[87],"class_list":["post-978","post","type-post","status-publish","format-standard","hentry","category-10","category-6","tag-java"],"_links":{"self":[{"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=\/wp\/v2\/posts\/978","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=978"}],"version-history":[{"count":1,"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=\/wp\/v2\/posts\/978\/revisions"}],"predecessor-version":[{"id":979,"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=\/wp\/v2\/posts\/978\/revisions\/979"}],"wp:attachment":[{"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=978"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=978"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=978"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}