{"id":70567,"date":"2026-02-02T03:17:29","date_gmt":"2026-02-01T19:17:29","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/70567.html"},"modified":"2026-02-02T03:17:29","modified_gmt":"2026-02-01T19:17:29","slug":"java-%e5%b9%b6%e5%8f%91%e5%b7%a5%e5%85%b7%e7%b1%bb%e8%af%a6%e8%a7%a3%ef%bc%9a4-%e5%a4%a7%e6%a0%b8%e5%bf%83%e5%b7%a5%e5%85%b7-%e5%ae%9e%e6%88%98%e5%9c%ba%e6%99%af%ef%bc%8c%e5%91%8a%e5%88%ab-synchroni","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/70567.html","title":{"rendered":"Java \u5e76\u53d1\u5de5\u5177\u7c7b\u8be6\u89e3\uff1a4 \u5927\u6838\u5fc3\u5de5\u5177 + \u5b9e\u6218\u573a\u666f\uff0c\u544a\u522b synchronized"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"836\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260201191728-697fa6c80495f.png\" width=\"1498\" \/><\/p>\n<p>\u5728 Java \u5e76\u53d1\u7f16\u7a0b\u4e2d&#xff0c;synchronized\u867d\u7136\u80fd\u89e3\u51b3\u7ebf\u7a0b\u540c\u6b65\u95ee\u9898&#xff0c;\u4f46\u529f\u80fd\u5355\u4e00\u3001\u7075\u6d3b\u6027\u5dee&#xff0c;\u9762\u5bf9\u590d\u6742\u573a\u666f&#xff08;\u5982\u591a\u7ebf\u7a0b\u7b49\u5f85\u3001\u6d41\u91cf\u63a7\u5236\u3001\u6570\u636e\u4ea4\u6362&#xff09;\u65f6\u529b\u4e0d\u4ece\u5fc3\u3002JDK \u63d0\u4f9b\u7684CountDownLatch\u3001CyclicBarrier\u3001Semaphore\u3001Exchanger\u56db\u5927\u5e76\u53d1\u5de5\u5177\u7c7b&#xff0c;\u57fa\u4e8e AQS&#xff08;\u961f\u5217\u5f0f\u540c\u6b65\u5668&#xff09;\u5b9e\u73b0&#xff0c;\u80fd\u66f4\u4f18\u96c5\u5730\u5904\u7406\u5404\u7c7b\u5e76\u53d1\u573a\u666f\u3002\u672c\u6587\u7528 \u201c\u751f\u6d3b\u573a\u666f &#043; \u4ee3\u7801\u793a\u4f8b\u201d \u7684\u65b9\u5f0f&#xff0c;\u5e26\u4f60\u5403\u900f\u8fd9 4 \u4e2a\u5de5\u5177\u7c7b&#xff0c;\u9762\u8bd5\u548c\u5de5\u4f5c\u90fd\u80fd\u7528\u5f97\u4e0a&#xff01;<\/p>\n<h3>\u4e00\u3001\u4e3a\u4ec0\u4e48\u9700\u8981\u5e76\u53d1\u5de5\u5177\u7c7b&#xff1f;\u2014\u2014 \u89e3\u51b3synchronized\u7684\u75db\u70b9<\/h3>\n<p>synchronized\u4f5c\u4e3a Java \u57fa\u7840\u7684\u540c\u6b65\u9501&#xff0c;\u5b58\u5728 3 \u4e2a\u660e\u663e\u5c40\u9650&#xff1a;<\/p>\n<ul>\n<li>\u53ea\u80fd\u5b9e\u73b0 \u201c\u4e92\u65a5\u201d&#xff08;\u540c\u4e00\u65f6\u95f4\u4e00\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u8d44\u6e90&#xff09;&#xff0c;\u65e0\u6cd5\u5b9e\u73b0 \u201c\u534f\u4f5c\u201d&#xff08;\u591a\u4e2a\u7ebf\u7a0b\u6309\u987a\u5e8f\u6267\u884c&#xff09;&#xff1b;<\/li>\n<li>\u6ca1\u6709\u8d85\u65f6\u673a\u5236&#xff0c;\u7ebf\u7a0b\u53ef\u80fd\u6c38\u4e45\u963b\u585e&#xff1b;<\/li>\n<li>\u529f\u80fd\u5355\u4e00&#xff0c;\u65e0\u6cd5\u6ee1\u8db3\u6d41\u91cf\u63a7\u5236\u3001\u6570\u636e\u4ea4\u6362\u7b49\u590d\u6742\u9700\u6c42\u3002<\/li>\n<\/ul>\n<p>\u800c\u5e76\u53d1\u5de5\u5177\u7c7b\u7684\u6838\u5fc3\u4ef7\u503c\u7684\u662f **\u201c\u7ebf\u7a0b\u534f\u4f5c\u4e0e\u7cbe\u7ec6\u5316\u63a7\u5236\u201d**&#xff0c;\u8ba9\u591a\u7ebf\u7a0b\u4e0d\u518d\u662f \u201c\u5404\u81ea\u4e3a\u6218\u201d&#xff0c;\u800c\u662f\u80fd\u6309\u9884\u671f\u6d41\u7a0b\u534f\u540c\u5de5\u4f5c&#xff0c;\u540c\u65f6\u63d0\u4f9b\u8d85\u65f6\u3001\u56de\u8c03\u7b49\u9ad8\u7ea7\u7279\u6027&#xff0c;\u8ba9\u5e76\u53d1\u7f16\u7a0b\u66f4\u5b89\u5168\u3001\u66f4\u7075\u6d3b\u3002<\/p>\n<h3>\u4e8c\u30014 \u5927\u6838\u5fc3\u5e76\u53d1\u5de5\u5177\u7c7b&#xff1a;\u573a\u666f &#043; \u4ee3\u7801 &#043; \u539f\u7406<\/h3>\n<h4>1. CountDownLatch&#xff1a;\u5012\u8ba1\u65f6\u9501 \u2014\u2014\u201c\u7b49\u5f85\u5176\u4ed6\u7ebf\u7a0b\u5b8c\u6210&#xff0c;\u518d\u884c\u52a8\u201d<\/h4>\n<h5>\u6838\u5fc3\u5b9a\u4f4d<\/h5>\n<p>\u5141\u8bb8 1 \u4e2a\u6216\u591a\u4e2a\u7ebf\u7a0b\u7b49\u5f85&#xff0c;\u76f4\u5230\u5176\u4ed6N\u4e2a\u7ebf\u7a0b\u5b8c\u6210\u4efb\u52a1\u540e&#xff0c;\u518d\u7ee7\u7eed\u6267\u884c\u3002\u672c\u8d28\u662f \u201c\u5012\u8ba1\u6570\u5668\u201d&#xff0c;\u8ba1\u6570\u5668\u5f52 0 \u65f6&#xff0c;\u7b49\u5f85\u7ebf\u7a0b\u88ab\u5524\u9192\u3002<\/p>\n<h5>\u751f\u6d3b\u573a\u666f<\/h5>\n<ul>\n<li>\u8003\u8bd5\u7ed3\u675f&#xff1a;\u8001\u5e08&#xff08;\u7b49\u5f85\u7ebf\u7a0b&#xff09;\u8981\u7b49\u6240\u6709\u5b66\u751f&#xff08;\u5de5\u4f5c\u7ebf\u7a0b&#xff09;\u4ea4\u5377\u540e&#xff0c;\u624d\u5f00\u59cb\u6279\u6539\u8bd5\u5377&#xff1b;<\/li>\n<li>\u7530\u5f84\u8d5b\u8dd1&#xff1a;10 \u540d\u8fd0\u52a8\u5458&#xff08;\u5de5\u4f5c\u7ebf\u7a0b&#xff09;\u7b49\u5f85\u88c1\u5224&#xff08;\u63a7\u5236\u7ebf\u7a0b&#xff09;\u53d1\u4ee4\u540e&#xff0c;\u540c\u65f6\u8d77\u8dd1\u3002<\/li>\n<\/ul>\n<h5>\u6838\u5fc3\u65b9\u6cd5<\/h5>\n<ul>\n<li>CountDownLatch(int count)&#xff1a;\u6784\u9020\u65b9\u6cd5&#xff0c;\u521d\u59cb\u5316\u8ba1\u6570\u5668&#xff08;\u9700\u7b49\u5f85\u7684\u7ebf\u7a0b\u6570&#xff09;&#xff1b;<\/li>\n<li>countDown()&#xff1a;\u5de5\u4f5c\u7ebf\u7a0b\u8c03\u7528&#xff0c;\u8ba1\u6570\u5668\u51cf 1&#xff08;\u4efb\u52a1\u5b8c\u6210&#xff09;&#xff1b;<\/li>\n<li>await()&#xff1a;\u7b49\u5f85\u7ebf\u7a0b\u8c03\u7528&#xff0c;\u963b\u585e\u76f4\u5230\u8ba1\u6570\u5668\u4e3a 0&#xff1b;<\/li>\n<li>await(long timeout, TimeUnit unit)&#xff1a;\u5e26\u8d85\u65f6\u7684\u7b49\u5f85&#xff0c;\u907f\u514d\u6c38\u4e45\u963b\u585e\u3002<\/li>\n<\/ul>\n<h5>\u5b9e\u6218\u4ee3\u7801<\/h5>\n<h6>\u573a\u666f 1&#xff1a;\u4e3b\u7ebf\u7a0b\u7b49\u5f85 10 \u4e2a\u5de5\u4f5c\u7ebf\u7a0b\u5b8c\u6210\u540e\u6c47\u603b\u7ed3\u679c<\/h6>\n<p>import java.util.concurrent.CountDownLatch;<\/p>\n<p>public class CountDownLatchDemo1 {<br \/>\n    public static void main(String[] args) throws InterruptedException {<br \/>\n        int threadCount &#061; 10; \/\/ 10\u4e2a\u5de5\u4f5c\u7ebf\u7a0b<br \/>\n        CountDownLatch latch &#061; new CountDownLatch(threadCount);<\/p>\n<p>        for (int i &#061; 0; i &lt; threadCount; i&#043;&#043;) {<br \/>\n            new Thread(() -&gt; {<br \/>\n                try {<br \/>\n                    \/\/ \u6a21\u62df\u5de5\u4f5c\u4efb\u52a1&#xff08;\u5982\u6570\u636e\u67e5\u8be2\u3001\u6587\u4ef6\u5904\u7406&#xff09;<br \/>\n                    Thread.sleep(1000);<br \/>\n                    System.out.println(Thread.currentThread().getName() &#043; &#034;&#xff1a;\u4efb\u52a1\u6267\u884c\u5b8c\u6bd5&#034;);<br \/>\n                } catch (InterruptedException e) {<br \/>\n                    e.printStackTrace();<br \/>\n                } finally {<br \/>\n                    latch.countDown(); \/\/ \u8ba1\u6570\u5668\u51cf1&#xff08;\u5fc5\u987b\u653e\u5728finally&#xff0c;\u786e\u4fdd\u4efb\u52a1\u5b8c\u6210\u540e\u6267\u884c&#xff09;<br \/>\n                }<br \/>\n            }, &#034;\u5de5\u4f5c\u7ebf\u7a0b&#034; &#043; i).start();<br \/>\n        }<\/p>\n<p>        System.out.println(&#034;\u4e3b\u7ebf\u7a0b&#xff1a;\u7b49\u5f85\u6240\u6709\u5de5\u4f5c\u7ebf\u7a0b\u5b8c\u6210&#8230;&#034;);<br \/>\n        latch.await(); \/\/ \u963b\u585e\u7b49\u5f85\u8ba1\u6570\u5668\u4e3a0<br \/>\n        System.out.println(&#034;\u4e3b\u7ebf\u7a0b&#xff1a;\u6240\u6709\u4efb\u52a1\u6267\u884c\u5b8c\u6bd5&#xff0c;\u5f00\u59cb\u6c47\u603b\u7ed3\u679c&#034;);<br \/>\n    }<br \/>\n}<\/p>\n<p>\u8fd0\u884c\u7ed3\u679c&#xff1a;<\/p>\n<p>\u4e3b\u7ebf\u7a0b&#xff1a;\u7b49\u5f85\u6240\u6709\u5de5\u4f5c\u7ebf\u7a0b\u5b8c\u6210&#8230;<br \/>\n\u5de5\u4f5c\u7ebf\u7a0b0&#xff1a;\u4efb\u52a1\u6267\u884c\u5b8c\u6bd5<br \/>\n\u5de5\u4f5c\u7ebf\u7a0b1&#xff1a;\u4efb\u52a1\u6267\u884c\u5b8c\u6bd5<br \/>\n&#8230;&#xff08;\u4e2d\u95f4\u7701\u75658\u4e2a\u7ebf\u7a0b&#xff09;<br \/>\n\u5de5\u4f5c\u7ebf\u7a0b9&#xff1a;\u4efb\u52a1\u6267\u884c\u5b8c\u6bd5<br \/>\n\u4e3b\u7ebf\u7a0b&#xff1a;\u6240\u6709\u4efb\u52a1\u6267\u884c\u5b8c\u6bd5&#xff0c;\u5f00\u59cb\u6c47\u603b\u7ed3\u679c<\/p>\n<h6>\u573a\u666f 2&#xff1a;10 \u4e2a\u7ebf\u7a0b\u7b49\u5f85\u6307\u4ee4&#xff0c;\u540c\u65f6\u6267\u884c<\/h6>\n<p>import java.util.concurrent.CountDownLatch;<\/p>\n<p>public class CountDownLatchDemo2 {<br \/>\n    public static void main(String[] args) throws InterruptedException {<br \/>\n        CountDownLatch latch &#061; new CountDownLatch(1); \/\/ \u8ba1\u6570\u5668\u521d\u59cb\u4e3a1&#xff08;\u7b49\u5f851\u4e2a\u4fe1\u53f7&#xff09;<br \/>\n        int threadCount &#061; 10;<\/p>\n<p>        for (int i &#061; 0; i &lt; threadCount; i&#043;&#043;) {<br \/>\n            new Thread(() -&gt; {<br \/>\n                try {<br \/>\n                    System.out.println(Thread.currentThread().getName() &#043; &#034;&#xff1a;\u51c6\u5907\u5c31\u7eea&#xff0c;\u7b49\u5f85\u6307\u4ee4&#034;);<br \/>\n                    latch.await(); \/\/ \u963b\u585e\u7b49\u5f85\u8ba1\u6570\u5668\u4e3a0<br \/>\n                    System.out.println(Thread.currentThread().getName() &#043; &#034;&#xff1a;\u6536\u5230\u6307\u4ee4&#xff0c;\u5f00\u59cb\u6267\u884c&#034;);<br \/>\n                } catch (InterruptedException e) {<br \/>\n                    e.printStackTrace();<br \/>\n                }<br \/>\n            }, &#034;\u8fd0\u52a8\u5458&#034; &#043; i).start();<br \/>\n        }<\/p>\n<p>        \/\/ \u6a21\u62df\u88c1\u5224\u51c6\u5907\u65f6\u95f4<br \/>\n        Thread.sleep(2000);<br \/>\n        System.out.println(&#034;\u4e3b\u7ebf\u7a0b&#xff08;\u88c1\u5224&#xff09;&#xff1a;\u53d1\u4ee4&#xff01;&#034;);<br \/>\n        latch.countDown(); \/\/ \u8ba1\u6570\u5668\u51cf1&#xff08;\u53d8\u4e3a0&#xff0c;\u5524\u9192\u6240\u6709\u7b49\u5f85\u7ebf\u7a0b&#xff09;<br \/>\n    }<br \/>\n}<\/p>\n<p>\u8fd0\u884c\u7ed3\u679c&#xff1a;<\/p>\n<p>\u8fd0\u52a8\u54580&#xff1a;\u51c6\u5907\u5c31\u7eea&#xff0c;\u7b49\u5f85\u6307\u4ee4<br \/>\n\u8fd0\u52a8\u54581&#xff1a;\u51c6\u5907\u5c31\u7eea&#xff0c;\u7b49\u5f85\u6307\u4ee4<br \/>\n&#8230;&#xff08;\u4e2d\u95f4\u7701\u75658\u4e2a\u7ebf\u7a0b&#xff09;<br \/>\n\u8fd0\u52a8\u54589&#xff1a;\u51c6\u5907\u5c31\u7eea&#xff0c;\u7b49\u5f85\u6307\u4ee4<br \/>\n\u4e3b\u7ebf\u7a0b&#xff08;\u88c1\u5224&#xff09;&#xff1a;\u53d1\u4ee4&#xff01;<br \/>\n\u8fd0\u52a8\u54580&#xff1a;\u6536\u5230\u6307\u4ee4&#xff0c;\u5f00\u59cb\u6267\u884c<br \/>\n\u8fd0\u52a8\u54581&#xff1a;\u6536\u5230\u6307\u4ee4&#xff0c;\u5f00\u59cb\u6267\u884c<br \/>\n&#8230;&#xff08;\u6240\u6709\u7ebf\u7a0b\u540c\u65f6\u6267\u884c&#xff09;<\/p>\n<h5>\u5173\u952e\u6ce8\u610f<\/h5>\n<ul>\n<li>\u8ba1\u6570\u5668\u53ea\u80fd\u4f7f\u7528\u4e00\u6b21&#xff0c;\u5f52 0 \u540e\u518d\u8c03\u7528countDown()\u65e0\u6548&#xff1b;<\/li>\n<li>countDown()\u5fc5\u987b\u653e\u5728finally\u5757\u4e2d&#xff0c;\u786e\u4fdd\u5de5\u4f5c\u7ebf\u7a0b\u65e0\u8bba\u662f\u5426\u5f02\u5e38&#xff0c;\u90fd\u4f1a decrement \u8ba1\u6570\u5668&#xff0c;\u907f\u514d\u7b49\u5f85\u7ebf\u7a0b\u6c38\u4e45\u963b\u585e\u3002<\/li>\n<\/ul>\n<h4>2. CyclicBarrier&#xff1a;\u5faa\u73af\u5c4f\u969c \u2014\u2014\u201c\u5927\u5bb6\u5230\u9f50\u4e86&#xff0c;\u518d\u4e00\u8d77\u51fa\u53d1\u201d<\/h4>\n<h5>\u6838\u5fc3\u5b9a\u4f4d<\/h5>\n<p>\u8ba9N\u4e2a\u7ebf\u7a0b\u5230\u8fbe \u201c\u5c4f\u969c\u201d \u540e\u963b\u585e&#xff0c;\u76f4\u5230\u6240\u6709\u7ebf\u7a0b\u90fd\u5230\u8fbe\u5c4f\u969c&#xff0c;\u5c4f\u969c\u624d\u4f1a\u89e3\u9664&#xff0c;\u6240\u6709\u7ebf\u7a0b\u540c\u65f6\u7ee7\u7eed\u6267\u884c\u3002\u4e0eCountDownLatch\u7684\u6838\u5fc3\u533a\u522b\u662f&#xff1a;\u8ba1\u6570\u5668\u53ef\u5faa\u73af\u4f7f\u7528\u3002<\/p>\n<h5>\u751f\u6d3b\u573a\u666f<\/h5>\n<ul>\n<li>\u56e2\u961f\u65c5\u6e38&#xff1a;\u5bfc\u6e38&#xff08;\u5c4f\u969c&#xff09;\u8981\u7b49\u6240\u6709\u6e38\u5ba2&#xff08;\u7ebf\u7a0b&#xff09;\u5230\u9f50\u540e&#xff0c;\u624d\u51fa\u53d1\u53bb\u4e0b\u4e00\u4e2a\u666f\u70b9&#xff1b;<\/li>\n<li>\u591a\u9636\u6bb5\u4efb\u52a1&#xff1a;\u591a\u4e2a\u7ebf\u7a0b\u5148\u5b8c\u6210\u7b2c\u4e00\u9636\u6bb5\u4efb\u52a1&#xff0c;\u5230\u5c4f\u969c\u5904\u96c6\u5408&#xff0c;\u518d\u540c\u65f6\u5f00\u59cb\u7b2c\u4e8c\u9636\u6bb5\u4efb\u52a1\u3002<\/li>\n<\/ul>\n<h5>\u6838\u5fc3\u65b9\u6cd5<\/h5>\n<ul>\n<li>CyclicBarrier(int parties)&#xff1a;\u6784\u9020\u65b9\u6cd5&#xff0c;\u6307\u5b9a\u53c2\u4e0e\u7684\u7ebf\u7a0b\u6570&#xff08;\u5c4f\u969c\u89e6\u53d1\u6761\u4ef6&#xff09;&#xff1b;<\/li>\n<li>CyclicBarrier(int parties, Runnable barrierAction)&#xff1a;\u5e26\u56de\u8c03\u7684\u6784\u9020\u65b9\u6cd5&#xff0c;\u6240\u6709\u7ebf\u7a0b\u5230\u8fbe\u5c4f\u969c\u540e&#xff0c;\u5148\u6267\u884cbarrierAction&#xff08;\u5982\u65e5\u5fd7\u8bb0\u5f55\u3001\u8d44\u6e90\u521d\u59cb\u5316&#xff09;&#xff1b;<\/li>\n<li>await()&#xff1a;\u7ebf\u7a0b\u5230\u8fbe\u5c4f\u969c\u540e\u8c03\u7528&#xff0c;\u963b\u585e\u76f4\u5230\u6240\u6709\u7ebf\u7a0b\u90fd\u5230\u8fbe&#xff1b;<\/li>\n<li>await(long timeout, TimeUnit unit)&#xff1a;\u5e26\u8d85\u65f6\u7684\u7b49\u5f85\u3002<\/li>\n<\/ul>\n<h5>\u5b9e\u6218\u4ee3\u7801<\/h5>\n<p>import java.util.concurrent.CyclicBarrier;<\/p>\n<p>public class CyclicBarrierDemo {<br \/>\n    public static void main(String[] args) {<br \/>\n        int threadCount &#061; 5; \/\/ 5\u4e2a\u7ebf\u7a0b\u53c2\u4e0e<br \/>\n        \/\/ \u5c4f\u969c\u89e6\u53d1\u65f6&#xff0c;\u6267\u884c\u56de\u8c03&#xff08;\u6240\u6709\u7ebf\u7a0b\u5230\u9f50\u540e\u6267\u884c&#xff09;<br \/>\n        CyclicBarrier barrier &#061; new CyclicBarrier(threadCount, () -&gt; {<br \/>\n            System.out.println(&#034;\\\\n&#061;&#061;&#061;&#061;&#061; \u6240\u6709\u7ebf\u7a0b\u5df2\u5230\u9f50&#xff0c;\u5c4f\u969c\u89e3\u9664&#xff01;&#061;&#061;&#061;&#061;&#061;\\\\n&#034;);<br \/>\n        });<\/p>\n<p>        for (int i &#061; 0; i &lt; threadCount; i&#043;&#043;) {<br \/>\n            int finalI &#061; i;<br \/>\n            new Thread(() -&gt; {<br \/>\n                try {<br \/>\n                    \/\/ \u6a21\u62df\u7ebf\u7a0b\u5230\u8fbe\u5c4f\u969c\u524d\u7684\u4efb\u52a1&#xff08;\u5982\u6570\u636e\u9884\u5904\u7406&#xff09;<br \/>\n                    Thread.sleep((long) (Math.random() * 2000));<br \/>\n                    System.out.println(Thread.currentThread().getName() &#043; &#034;&#xff1a;\u5df2\u5230\u8fbe\u5c4f\u969c&#xff08;\u5b8c\u6210\u7b2c&#034; &#043; finalI &#043; &#034;\u9636\u6bb5\u4efb\u52a1&#xff09;&#034;);<\/p>\n<p>                    barrier.await(); \/\/ \u963b\u585e\u7b49\u5f85\u5176\u4ed6\u7ebf\u7a0b<\/p>\n<p>                    \/\/ \u5c4f\u969c\u89e3\u9664\u540e&#xff0c;\u6267\u884c\u540e\u7eed\u4efb\u52a1<br \/>\n                    Thread.sleep(1000);<br \/>\n                    System.out.println(Thread.currentThread().getName() &#043; &#034;&#xff1a;\u7ee7\u7eed\u6267\u884c\u7b2c&#034; &#043; (finalI &#043; 1) &#043; &#034;\u9636\u6bb5\u4efb\u52a1&#034;);<br \/>\n                } catch (Exception e) {<br \/>\n                    e.printStackTrace();<br \/>\n                }<br \/>\n            }, &#034;\u5de5\u4f5c\u7ebf\u7a0b&#034; &#043; i).start();<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<p>\u8fd0\u884c\u7ed3\u679c&#xff1a;<\/p>\n<p>\u5de5\u4f5c\u7ebf\u7a0b2&#xff1a;\u5df2\u5230\u8fbe\u5c4f\u969c&#xff08;\u5b8c\u6210\u7b2c2\u9636\u6bb5\u4efb\u52a1&#xff09;<br \/>\n\u5de5\u4f5c\u7ebf\u7a0b0&#xff1a;\u5df2\u5230\u8fbe\u5c4f\u969c&#xff08;\u5b8c\u6210\u7b2c0\u9636\u6bb5\u4efb\u52a1&#xff09;<br \/>\n\u5de5\u4f5c\u7ebf\u7a0b1&#xff1a;\u5df2\u5230\u8fbe\u5c4f\u969c&#xff08;\u5b8c\u6210\u7b2c1\u9636\u6bb5\u4efb\u52a1&#xff09;<br \/>\n\u5de5\u4f5c\u7ebf\u7a0b3&#xff1a;\u5df2\u5230\u8fbe\u5c4f\u969c&#xff08;\u5b8c\u6210\u7b2c3\u9636\u6bb5\u4efb\u52a1&#xff09;<br \/>\n\u5de5\u4f5c\u7ebf\u7a0b4&#xff1a;\u5df2\u5230\u8fbe\u5c4f\u969c&#xff08;\u5b8c\u6210\u7b2c4\u9636\u6bb5\u4efb\u52a1&#xff09;<\/p>\n<p>&#061;&#061;&#061;&#061;&#061; \u6240\u6709\u7ebf\u7a0b\u5df2\u5230\u9f50&#xff0c;\u5c4f\u969c\u89e3\u9664&#xff01;&#061;&#061;&#061;&#061;&#061;<\/p>\n<p>\u5de5\u4f5c\u7ebf\u7a0b4&#xff1a;\u7ee7\u7eed\u6267\u884c\u7b2c5\u9636\u6bb5\u4efb\u52a1<br \/>\n\u5de5\u4f5c\u7ebf\u7a0b0&#xff1a;\u7ee7\u7eed\u6267\u884c\u7b2c1\u9636\u6bb5\u4efb\u52a1<br \/>\n\u5de5\u4f5c\u7ebf\u7a0b2&#xff1a;\u7ee7\u7eed\u6267\u884c\u7b2c3\u9636\u6bb5\u4efb\u52a1<br \/>\n\u5de5\u4f5c\u7ebf\u7a0b1&#xff1a;\u7ee7\u7eed\u6267\u884c\u7b2c2\u9636\u6bb5\u4efb\u52a1<br \/>\n\u5de5\u4f5c\u7ebf\u7a0b3&#xff1a;\u7ee7\u7eed\u6267\u884c\u7b2c4\u9636\u6bb5\u4efb\u52a1<\/p>\n<h5>\u5173\u952e\u6ce8\u610f<\/h5>\n<ul>\n<li>\u8ba1\u6570\u5668\u53ef\u5faa\u73af\u4f7f\u7528&#xff1a;\u5c4f\u969c\u89e3\u9664\u540e&#xff0c;\u8ba1\u6570\u5668\u81ea\u52a8\u91cd\u7f6e&#xff0c;\u53ef\u518d\u6b21\u7528\u4e8e\u4e0b\u4e00\u8f6e\u7ebf\u7a0b\u534f\u4f5c&#xff1b;<\/li>\n<li>\u82e5\u67d0\u4e2a\u7ebf\u7a0b\u4e2d\u65ad\u6216\u8d85\u65f6&#xff0c;\u5c4f\u969c\u4f1a\u88ab\u7834\u574f&#xff0c;\u5176\u4ed6\u7ebf\u7a0b\u4f1a\u629b\u51faBrokenBarrierException&#xff0c;\u9700\u6355\u83b7\u5904\u7406\u3002<\/li>\n<\/ul>\n<h4>3. Semaphore&#xff1a;\u4fe1\u53f7\u91cf \u2014\u2014\u201c\u6d41\u91cf\u63a7\u5236&#xff0c;\u6700\u591a N \u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bbf\u95ee\u201d<\/h4>\n<h5>\u6838\u5fc3\u5b9a\u4f4d<\/h5>\n<p>\u63a7\u5236\u540c\u4e00\u65f6\u95f4\u8bbf\u95ee\u67d0\u4e2a\u8d44\u6e90\u7684\u7ebf\u7a0b\u6570&#xff0c;\u672c\u8d28\u662f \u201c\u8bb8\u53ef\u8ba1\u6570\u5668\u201d&#xff0c;\u7ebf\u7a0b\u9700\u5148\u83b7\u53d6\u8bb8\u53ef\u624d\u80fd\u8bbf\u95ee\u8d44\u6e90&#xff0c;\u7528\u5b8c\u540e\u91ca\u653e\u8bb8\u53ef\u3002<\/p>\n<h5>\u751f\u6d3b\u573a\u666f<\/h5>\n<ul>\n<li>\u505c\u8f66\u573a&#xff1a;\u6700\u591a\u5bb9\u7eb3 100 \u8f86\u8f66&#xff08;\u8bb8\u53ef\u6570 100&#xff09;&#xff0c;\u8f66\u8f86&#xff08;\u7ebf\u7a0b&#xff09;\u8fdb\u5165\u524d\u9700\u83b7\u53d6\u8f66\u4f4d&#xff08;\u8bb8\u53ef&#xff09;&#xff0c;\u79bb\u5f00\u65f6\u91ca\u653e\u8f66\u4f4d&#xff1b;<\/li>\n<li>\u63a5\u53e3\u9650\u6d41&#xff1a;\u540c\u4e00\u65f6\u95f4\u6700\u591a\u5141\u8bb8 10 \u4e2a\u8bf7\u6c42&#xff08;\u7ebf\u7a0b&#xff09;\u8bbf\u95ee\u652f\u4ed8\u63a5\u53e3&#xff0c;\u907f\u514d\u670d\u52a1\u8fc7\u8f7d\u3002<\/li>\n<\/ul>\n<h5>\u6838\u5fc3\u65b9\u6cd5<\/h5>\n<ul>\n<li>Semaphore(int permits)&#xff1a;\u6784\u9020\u65b9\u6cd5&#xff0c;\u6307\u5b9a\u6700\u5927\u8bb8\u53ef\u6570&#xff08;\u540c\u65f6\u8bbf\u95ee\u7684\u7ebf\u7a0b\u6570&#xff09;&#xff1b;<\/li>\n<li>acquire()&#xff1a;\u83b7\u53d6 1 \u4e2a\u8bb8\u53ef&#xff0c;\u82e5\u65e0\u8bb8\u53ef\u5219\u963b\u585e&#xff1b;<\/li>\n<li>tryAcquire(long timeout, TimeUnit unit)&#xff1a;\u5c1d\u8bd5\u5728\u6307\u5b9a\u65f6\u95f4\u5185\u83b7\u53d6\u8bb8\u53ef&#xff0c;\u6210\u529f\u8fd4\u56detrue&#xff0c;\u5931\u8d25\u8fd4\u56defalse&#xff1b;<\/li>\n<li>release()&#xff1a;\u91ca\u653e 1 \u4e2a\u8bb8\u53ef&#xff0c;\u5524\u9192\u7b49\u5f85\u7684\u7ebf\u7a0b&#xff1b;<\/li>\n<li>availablePermits()&#xff1a;\u83b7\u53d6\u5f53\u524d\u53ef\u7528\u7684\u8bb8\u53ef\u6570\u3002<\/li>\n<\/ul>\n<h5>\u5b9e\u6218\u4ee3\u7801<\/h5>\n<h6>\u573a\u666f&#xff1a;\u63a5\u53e3\u9650\u6d41&#xff08;\u540c\u4e00\u65f6\u95f4\u6700\u591a 3 \u4e2a\u7ebf\u7a0b\u8bbf\u95ee&#xff09;<\/h6>\n<p>import java.text.SimpleDateFormat;<br \/>\nimport java.util.Date;<br \/>\nimport java.util.concurrent.Semaphore;<\/p>\n<p>public class SemaphoreDemo {<br \/>\n    public static void main(String[] args) {<br \/>\n        int maxPermits &#061; 3; \/\/ \u6700\u591a3\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bbf\u95ee<br \/>\n        Semaphore semaphore &#061; new Semaphore(maxPermits);<br \/>\n        SimpleDateFormat sdf &#061; new SimpleDateFormat(&#034;yyyy-MM-dd HH:mm:ss&#034;);<\/p>\n<p>        \/\/ \u6a21\u62df10\u4e2a\u8bf7\u6c42\u7ebf\u7a0b<br \/>\n        for (int i &#061; 0; i &lt; 10; i&#043;&#043;) {<br \/>\n            new Thread(() -&gt; {<br \/>\n                \/\/ \u5c1d\u8bd53\u79d2\u5185\u83b7\u53d6\u8bb8\u53ef&#xff0c;\u907f\u514d\u6c38\u4e45\u963b\u585e<br \/>\n                try {<br \/>\n                    if (semaphore.tryAcquire(3, java.util.concurrent.TimeUnit.SECONDS)) {<br \/>\n                        \/\/ \u6a21\u62df\u63a5\u53e3\u5904\u7406\u65f6\u95f4&#xff08;1-2\u79d2&#xff09;<br \/>\n                        Thread.sleep((long) (Math.random() * 1000 &#043; 1000));<br \/>\n                        System.out.println(sdf.format(new Date()) &#043; &#034; | &#034; &#043; Thread.currentThread().getName() &#043; &#034;&#xff1a;\u83b7\u53d6\u8bb8\u53ef&#xff0c;\u63a5\u53e3\u8bbf\u95ee\u6210\u529f&#034;);<br \/>\n                    } else {<br \/>\n                        System.out.println(sdf.format(new Date()) &#043; &#034; | &#034; &#043; Thread.currentThread().getName() &#043; &#034;&#xff1a;\u83b7\u53d6\u8bb8\u53ef\u8d85\u65f6&#xff0c;\u63a5\u53e3\u8bbf\u95ee\u5931\u8d25&#034;);<br \/>\n                    }<br \/>\n                } catch (InterruptedException e) {<br \/>\n                    e.printStackTrace();<br \/>\n                } finally {<br \/>\n                    \/\/ \u91ca\u653e\u8bb8\u53ef&#xff08;\u5fc5\u987b\u653e\u5728finally&#xff0c;\u907f\u514d\u8bb8\u53ef\u6cc4\u9732&#xff09;<br \/>\n                    if (semaphore.hasQueuedThreads()) {<br \/>\n                        semaphore.release();<br \/>\n                    }<br \/>\n                }<br \/>\n            }, &#034;\u8bf7\u6c42\u7ebf\u7a0b&#034; &#043; i).start();<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<p>\u8fd0\u884c\u7ed3\u679c&#xff1a;<\/p>\n<p>2024-05-20 15:30:01 | \u8bf7\u6c42\u7ebf\u7a0b0&#xff1a;\u83b7\u53d6\u8bb8\u53ef&#xff0c;\u63a5\u53e3\u8bbf\u95ee\u6210\u529f<br \/>\n2024-05-20 15:30:01 | \u8bf7\u6c42\u7ebf\u7a0b1&#xff1a;\u83b7\u53d6\u8bb8\u53ef&#xff0c;\u63a5\u53e3\u8bbf\u95ee\u6210\u529f<br \/>\n2024-05-20 15:30:01 | \u8bf7\u6c42\u7ebf\u7a0b2&#xff1a;\u83b7\u53d6\u8bb8\u53ef&#xff0c;\u63a5\u53e3\u8bbf\u95ee\u6210\u529f<br \/>\n2024-05-20 15:30:02 | \u8bf7\u6c42\u7ebf\u7a0b3&#xff1a;\u83b7\u53d6\u8bb8\u53ef&#xff0c;\u63a5\u53e3\u8bbf\u95ee\u6210\u529f<br \/>\n2024-05-20 15:30:02 | \u8bf7\u6c42\u7ebf\u7a0b4&#xff1a;\u83b7\u53d6\u8bb8\u53ef&#xff0c;\u63a5\u53e3\u8bbf\u95ee\u6210\u529f<br \/>\n&#8230;&#xff08;\u540e\u7eed\u7ebf\u7a0b\u4f9d\u6b21\u83b7\u53d6\u91ca\u653e\u7684\u8bb8\u53ef&#xff09;<\/p>\n<h5>\u5173\u952e\u6ce8\u610f<\/h5>\n<ul>\n<li>\u8bb8\u53ef\u5fc5\u987b\u624b\u52a8\u91ca\u653e&#xff0c;\u4e14\u8981\u653e\u5728finally\u5757\u4e2d&#xff0c;\u907f\u514d\u56e0\u7ebf\u7a0b\u5f02\u5e38\u5bfc\u81f4\u8bb8\u53ef\u6cc4\u9732&#xff08;\u53ef\u7528\u8bb8\u53ef\u8d8a\u6765\u8d8a\u5c11&#xff09;&#xff1b;<\/li>\n<li>\u5f53\u8bb8\u53ef\u6570\u4e3a 1 \u65f6&#xff0c;Semaphore\u7b49\u4ef7\u4e8e\u4e92\u65a5\u9501&#xff08;synchronized&#xff09;&#xff0c;\u4f46\u66f4\u7075\u6d3b&#xff08;\u652f\u6301\u8d85\u65f6\u3001\u975e\u963b\u585e\u83b7\u53d6&#xff09;\u3002<\/li>\n<\/ul>\n<h4>4. Exchanger&#xff1a;\u6570\u636e\u4ea4\u6362\u5668 \u2014\u2014\u201c\u4e24\u4e2a\u7ebf\u7a0b\u7684\u53cc\u5411\u6570\u636e\u4ea4\u6362\u201d<\/h4>\n<h5>\u6838\u5fc3\u5b9a\u4f4d<\/h5>\n<p>\u7528\u4e8e\u4e24\u4e2a\u7ebf\u7a0b\u4e4b\u95f4\u7684\u53cc\u5411\u6570\u636e\u4ea4\u6362&#xff0c;\u7ebf\u7a0b\u5230\u8fbe\u4ea4\u6362\u70b9\u540e&#xff0c;\u4f1a\u963b\u585e\u7b49\u5f85\u53e6\u4e00\u4e2a\u7ebf\u7a0b&#xff0c;\u76f4\u5230\u4e24\u4e2a\u7ebf\u7a0b\u90fd\u5230\u8fbe&#xff0c;\u624d\u4ea4\u6362\u5f7c\u6b64\u7684\u6570\u636e\u3002<\/p>\n<h5>\u751f\u6d3b\u573a\u666f<\/h5>\n<ul>\n<li>\u5feb\u9012\u4ea4\u6362&#xff1a;\u4e24\u4e2a\u5feb\u9012\u5458&#xff08;\u7ebf\u7a0b&#xff09;\u5728\u6307\u5b9a\u5730\u70b9\u78b0\u9762&#xff0c;\u4ea4\u6362\u5404\u81ea\u7684\u5305\u88f9&#xff08;\u6570\u636e&#xff09;&#xff1b;<\/li>\n<li>\u6570\u636e\u6821\u9a8c&#xff1a;\u7ebf\u7a0b A \u8bfb\u53d6\u6587\u4ef6 A \u7684\u6570\u636e&#xff0c;\u7ebf\u7a0b B \u8bfb\u53d6\u6587\u4ef6 B \u7684\u6570\u636e&#xff0c;\u4ea4\u6362\u540e\u4e92\u76f8\u6821\u9a8c\u3002<\/li>\n<\/ul>\n<h5>\u6838\u5fc3\u65b9\u6cd5<\/h5>\n<ul>\n<li>Exchanger&lt;V&gt;&#xff1a;\u6cdb\u578b\u7c7b&#xff0c;\u6307\u5b9a\u4ea4\u6362\u7684\u6570\u636e\u7c7b\u578b&#xff1b;<\/li>\n<li>exchange(V x)&#xff1a;\u7ebf\u7a0b\u8c03\u7528&#xff0c;\u5c06\u6570\u636ex\u4f20\u9012\u7ed9\u5bf9\u65b9&#xff0c;\u540c\u65f6\u63a5\u6536\u5bf9\u65b9\u7684\u6570\u636e&#xff0c;\u963b\u585e\u76f4\u5230\u5bf9\u65b9\u5230\u8fbe\u4ea4\u6362\u70b9&#xff1b;<\/li>\n<li>exchange(V x, long timeout, TimeUnit unit)&#xff1a;\u5e26\u8d85\u65f6\u7684\u4ea4\u6362&#xff0c;\u8d85\u65f6\u629b\u51fa\u5f02\u5e38\u3002<\/li>\n<\/ul>\n<h5>\u5b9e\u6218\u4ee3\u7801<\/h5>\n<p>import java.util.concurrent.Exchanger;<\/p>\n<p>public class ExchangerDemo {<br \/>\n    public static void main(String[] args) {<br \/>\n        Exchanger&lt;String&gt; exchanger &#061; new Exchanger&lt;&gt;(); \/\/ \u4ea4\u6362String\u7c7b\u578b\u6570\u636e<\/p>\n<p>        \/\/ \u7ebf\u7a0b1&#xff1a;\u53d1\u9001\u6570\u636e&#034;A&#034;&#xff0c;\u63a5\u6536\u7ebf\u7a0b2\u7684\u6570\u636e<br \/>\n        new Thread(() -&gt; {<br \/>\n            try {<br \/>\n                String data &#061; &#034;A&#034;;<br \/>\n                System.out.println(Thread.currentThread().getName() &#043; &#034;&#xff1a;\u51c6\u5907\u4ea4\u6362\u7684\u6570\u636e&#xff1a;&#034; &#043; data);<br \/>\n                \/\/ \u963b\u585e\u7b49\u5f85\u7ebf\u7a0b2&#xff0c;\u4ea4\u6362\u6570\u636e<br \/>\n                String receivedData &#061; exchanger.exchange(data);<br \/>\n                System.out.println(Thread.currentThread().getName() &#043; &#034;&#xff1a;\u4ea4\u6362\u540e\u6536\u5230\u7684\u6570\u636e&#xff1a;&#034; &#043; receivedData);<br \/>\n            } catch (InterruptedException e) {<br \/>\n                e.printStackTrace();<br \/>\n            }<br \/>\n        }, &#034;\u4ea4\u6362\u7ebf\u7a0b1&#034;).start();<\/p>\n<p>        \/\/ \u7ebf\u7a0b2&#xff1a;\u53d1\u9001\u6570\u636e&#034;B&#034;&#xff0c;\u63a5\u6536\u7ebf\u7a0b1\u7684\u6570\u636e<br \/>\n        new Thread(() -&gt; {<br \/>\n            try {<br \/>\n                String data &#061; &#034;B&#034;;<br \/>\n                System.out.println(Thread.currentThread().getName() &#043; &#034;&#xff1a;\u51c6\u5907\u4ea4\u6362\u7684\u6570\u636e&#xff1a;&#034; &#043; data);<br \/>\n                \/\/ \u6a21\u62df\u5ef6\u8fdf\u5230\u8fbe\u4ea4\u6362\u70b9<br \/>\n                Thread.sleep(1000);<br \/>\n                String receivedData &#061; exchanger.exchange(data);<br \/>\n                System.out.println(Thread.currentThread().getName() &#043; &#034;&#xff1a;\u4ea4\u6362\u540e\u6536\u5230\u7684\u6570\u636e&#xff1a;&#034; &#043; receivedData);<br \/>\n            } catch (InterruptedException e) {<br \/>\n                e.printStackTrace();<br \/>\n            }<br \/>\n        }, &#034;\u4ea4\u6362\u7ebf\u7a0b2&#034;).start();<br \/>\n    }<br \/>\n}<\/p>\n<p>\u8fd0\u884c\u7ed3\u679c&#xff1a;<\/p>\n<p>\u4ea4\u6362\u7ebf\u7a0b1&#xff1a;\u51c6\u5907\u4ea4\u6362\u7684\u6570\u636e&#xff1a;A<br \/>\n\u4ea4\u6362\u7ebf\u7a0b2&#xff1a;\u51c6\u5907\u4ea4\u6362\u7684\u6570\u636e&#xff1a;B<br \/>\n\u4ea4\u6362\u7ebf\u7a0b1&#xff1a;\u4ea4\u6362\u540e\u6536\u5230\u7684\u6570\u636e&#xff1a;B<br \/>\n\u4ea4\u6362\u7ebf\u7a0b2&#xff1a;\u4ea4\u6362\u540e\u6536\u5230\u7684\u6570\u636e&#xff1a;A<\/p>\n<h5>\u5173\u952e\u6ce8\u610f<\/h5>\n<ul>\n<li>\u4ec5\u652f\u6301\u4e24\u4e2a\u7ebf\u7a0b\u4ea4\u6362&#xff0c;\u591a\u4e2a\u7ebf\u7a0b\u8c03\u7528exchange()\u4f1a\u5bfc\u81f4\u6570\u636e\u4ea4\u6362\u6df7\u4e71&#xff1b;<\/li>\n<li>\u82e5\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u5230\u8fbe\u4ea4\u6362\u70b9&#xff0c;\u4f1a\u4e00\u76f4\u963b\u585e&#xff08;\u6216\u8d85\u65f6&#xff09;&#xff0c;\u9700\u786e\u4fdd\u4e24\u4e2a\u7ebf\u7a0b\u90fd\u80fd\u6b63\u5e38\u5230\u8fbe\u3002<\/li>\n<\/ul>\n<h3>\u4e09\u3001\u9ad8\u9891\u9762\u8bd5&#xff1a;4 \u5927\u5de5\u5177\u7c7b\u6838\u5fc3\u533a\u522b<\/h3>\n<table>\n<tr>\u5de5\u5177\u7c7b\u6838\u5fc3\u4f5c\u7528\u8ba1\u6570\u5668\u7279\u6027\u9002\u7528\u573a\u666f\u6838\u5fc3\u533a\u522b<\/tr>\n<tbody>\n<tr>\n<td>CountDownLatch<\/td>\n<td>1 \u4e2a \/ \u591a\u4e2a\u7ebf\u7a0b\u7b49\u5f85 N \u4e2a\u7ebf\u7a0b\u5b8c\u6210<\/td>\n<td>\u4e00\u6b21\u6027\u4f7f\u7528<\/td>\n<td>\u4efb\u52a1\u6c47\u603b\u3001\u6307\u4ee4\u4e0b\u53d1<\/td>\n<td>\u7b49\u5f85\u7ebf\u7a0b\u4e0d\u53c2\u4e0e\u4efb\u52a1&#xff0c;\u4ec5\u7b49\u5f85&#xff1b;\u8ba1\u6570\u5668\u4e0d\u53ef\u5faa\u73af<\/td>\n<\/tr>\n<tr>\n<td>CyclicBarrier<\/td>\n<td>N \u4e2a\u7ebf\u7a0b\u4e92\u76f8\u7b49\u5f85&#xff0c;\u5230\u9f50\u540e\u540c\u65f6\u6267\u884c<\/td>\n<td>\u53ef\u5faa\u73af\u4f7f\u7528<\/td>\n<td>\u591a\u9636\u6bb5\u4efb\u52a1\u3001\u56e2\u961f\u534f\u4f5c<\/td>\n<td>\u6240\u6709\u7ebf\u7a0b\u90fd\u53c2\u4e0e\u4efb\u52a1&#xff0c;\u5230\u8fbe\u5c4f\u969c\u540e\u7ee7\u7eed&#xff1b;\u8ba1\u6570\u5668\u53ef\u91cd\u7f6e<\/td>\n<\/tr>\n<tr>\n<td>Semaphore<\/td>\n<td>\u63a7\u5236\u540c\u65f6\u8bbf\u95ee\u8d44\u6e90\u7684\u7ebf\u7a0b\u6570<\/td>\n<td>\u53ef\u91cd\u590d\u83b7\u53d6\u91ca\u653e<\/td>\n<td>\u63a5\u53e3\u9650\u6d41\u3001\u8d44\u6e90\u6c60\u63a7\u5236<\/td>\n<td>\u4e0d\u6d89\u53ca\u7ebf\u7a0b\u7b49\u5f85&#xff0c;\u4ec5\u6d41\u91cf\u63a7\u5236<\/td>\n<\/tr>\n<tr>\n<td>Exchanger<\/td>\n<td>\u4e24\u4e2a\u7ebf\u7a0b\u53cc\u5411\u6570\u636e\u4ea4\u6362<\/td>\n<td>\u65e0\u8ba1\u6570\u5668<\/td>\n<td>\u6570\u636e\u4ea4\u6362\u3001\u53cc\u5411\u6821\u9a8c<\/td>\n<td>\u4ec5\u652f\u6301\u4e24\u4e2a\u7ebf\u7a0b&#xff0c;\u4e13\u6ce8\u6570\u636e\u4ea4\u6362<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u56db\u3001\u5b9e\u6218\u907f\u5751\u6307\u5357<\/h3>\n<li>\u907f\u514d\u6c38\u4e45\u963b\u585e&#xff1a;\u4f18\u5148\u4f7f\u7528\u5e26\u8d85\u65f6\u7684\u65b9\u6cd5&#xff08;\u5982await(10, SECONDS)\u3001tryAcquire(3, SECONDS)&#xff09;&#xff0c;\u9632\u6b62\u7ebf\u7a0b\u56e0\u5f02\u5e38\u6216\u6b7b\u9501\u6c38\u4e45\u963b\u585e&#xff1b;<\/li>\n<li>\u91ca\u653e\u8d44\u6e90 \/ \u8bb8\u53ef&#xff1a;countDown()\u3001release()\u5fc5\u987b\u653e\u5728finally\u5757\u4e2d&#xff0c;\u786e\u4fdd\u5373\u4f7f\u7ebf\u7a0b\u5f02\u5e38&#xff0c;\u4e5f\u80fd\u6b63\u5e38\u91ca\u653e&#xff0c;\u907f\u514d\u8d44\u6e90\u6cc4\u9732&#xff1b;<\/li>\n<li>\u7ebf\u7a0b\u4e2d\u65ad\u5904\u7406&#xff1a;\u5e76\u53d1\u5de5\u5177\u7c7b\u7684\u963b\u585e\u65b9\u6cd5\u4f1a\u54cd\u5e94interrupt()&#xff0c;\u9700\u6355\u83b7InterruptedException&#xff0c;\u6839\u636e\u4e1a\u52a1\u51b3\u5b9a\u662f\u7ee7\u7eed\u6267\u884c\u8fd8\u662f\u9000\u51fa&#xff1b;<\/li>\n<li>** CyclicBarrier \u5f02\u5e38\u5904\u7406 **&#xff1a;\u82e5\u67d0\u4e2a\u7ebf\u7a0b\u4e2d\u65ad&#xff0c;\u4f1a\u5bfc\u81f4\u5c4f\u969c\u7834\u88c2&#xff0c;\u5176\u4ed6\u7ebf\u7a0b\u629b\u51faBrokenBarrierException&#xff0c;\u53ef\u901a\u8fc7isBroken()\u65b9\u6cd5\u5224\u65ad\u5c4f\u969c\u72b6\u6001&#xff0c;\u5fc5\u8981\u65f6\u91cd\u7f6e\u3002<\/li>\n<h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<p>Java \u5e76\u53d1\u5de5\u5177\u7c7b\u662fsynchronized\u7684\u8fdb\u9636\u66ff\u4ee3\u65b9\u6848&#xff0c;\u4e13\u6ce8\u4e8e \u201c\u7ebf\u7a0b\u534f\u4f5c\u201d \u548c \u201c\u7cbe\u7ec6\u5316\u63a7\u5236\u201d&#xff1a;<\/p>\n<ul>\n<li>\u9700 \u201c\u7b49\u5f85\u5176\u4ed6\u7ebf\u7a0b\u5b8c\u6210\u201d \u7528CountDownLatch&#xff1b;<\/li>\n<li>\u9700 \u201c\u7ebf\u7a0b\u4e92\u76f8\u7b49\u5f85\u3001\u5faa\u73af\u534f\u4f5c\u201d \u7528CyclicBarrier&#xff1b;<\/li>\n<li>\u9700 \u201c\u6d41\u91cf\u63a7\u5236\u3001\u9650\u5236\u5e76\u53d1\u6570\u201d \u7528Semaphore&#xff1b;<\/li>\n<li>\u9700 \u201c\u4e24\u4e2a\u7ebf\u7a0b\u6570\u636e\u4ea4\u6362\u201d \u7528Exchanger\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u4e9b\u5de5\u5177\u7c7b\u5e95\u5c42\u90fd\u57fa\u4e8e AQS \u5b9e\u73b0&#xff0c;\u65e0\u9700\u5173\u6ce8\u590d\u6742\u7684\u9501\u673a\u5236&#xff0c;\u53ea\u9700\u6839\u636e\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177&#xff0c;\u5c31\u80fd\u8ba9\u5e76\u53d1\u7f16\u7a0b\u66f4\u7b80\u6d01\u3001\u66f4\u5b89\u5168\u3002\u5efa\u8bae\u7ed3\u5408\u5b9e\u9645\u573a\u666f\u591a\u6572\u4ee3\u7801\u7ec3\u4e60&#xff0c;\u7406\u89e3\u5b83\u4eec\u7684\u9002\u7528\u8fb9\u754c&#xff0c;\u9762\u8bd5\u65f6\u5c31\u80fd\u4ece\u5bb9\u5e94\u5bf9&#xff0c;\u5de5\u4f5c\u4e2d\u4e5f\u80fd\u5feb\u901f\u89e3\u51b3\u5e76\u53d1\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Java \u5e76\u53d1\u7f16\u7a0b\u4e2d&#xff0c;synchronized\u867d\u7136\u80fd\u89e3\u51b3\u7ebf\u7a0b\u540c\u6b65\u95ee\u9898&#xff0c;\u4f46\u529f\u80fd\u5355\u4e00\u3001\u7075\u6d3b\u6027\u5dee&#xff0c;\u9762\u5bf9\u590d\u6742\u573a\u666f&#xff08;\u5982\u591a\u7ebf\u7a0b\u7b49\u5f85\u3001\u6d41\u91cf\u63a7\u5236\u3001\u6570\u636e\u4ea4\u6362&#xff09;\u65f6\u529b\u4e0d\u4ece\u5fc3\u3002JDK \u63d0\u4f9b\u7684CountDownLatch\u3001CyclicBarrier\u3001Semaphore\u3001Exchanger\u56db\u5927\u5e76\u53d1\u5de5\u5177\u7c7b&#xff0c;\u57fa\u4e8e AQS&#xff08;\u961f\u5217\u5f0f\u540c\u6b65\u5668&#xff09;\u5b9e\u73b0&#xff0c;\u80fd\u66f4\u4f18\u96c5\u5730\u5904\u7406\u5404\u7c7b\u5e76\u53d1\u573a\u666f\u3002\u672c\u6587\u7528 \u201c<\/p>\n","protected":false},"author":2,"featured_media":70566,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[7413,99],"topic":[],"class_list":["post-70567","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-7413","tag-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Java \u5e76\u53d1\u5de5\u5177\u7c7b\u8be6\u89e3\uff1a4 \u5927\u6838\u5fc3\u5de5\u5177 + \u5b9e\u6218\u573a\u666f\uff0c\u544a\u522b synchronized - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/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.wsisp.com\/helps\/70567.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java \u5e76\u53d1\u5de5\u5177\u7c7b\u8be6\u89e3\uff1a4 \u5927\u6838\u5fc3\u5de5\u5177 + \u5b9e\u6218\u573a\u666f\uff0c\u544a\u522b synchronized - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u5728 Java \u5e76\u53d1\u7f16\u7a0b\u4e2d&#xff0c;synchronized\u867d\u7136\u80fd\u89e3\u51b3\u7ebf\u7a0b\u540c\u6b65\u95ee\u9898&#xff0c;\u4f46\u529f\u80fd\u5355\u4e00\u3001\u7075\u6d3b\u6027\u5dee&#xff0c;\u9762\u5bf9\u590d\u6742\u573a\u666f&#xff08;\u5982\u591a\u7ebf\u7a0b\u7b49\u5f85\u3001\u6d41\u91cf\u63a7\u5236\u3001\u6570\u636e\u4ea4\u6362&#xff09;\u65f6\u529b\u4e0d\u4ece\u5fc3\u3002JDK \u63d0\u4f9b\u7684CountDownLatch\u3001CyclicBarrier\u3001Semaphore\u3001Exchanger\u56db\u5927\u5e76\u53d1\u5de5\u5177\u7c7b&#xff0c;\u57fa\u4e8e AQS&#xff08;\u961f\u5217\u5f0f\u540c\u6b65\u5668&#xff09;\u5b9e\u73b0&#xff0c;\u80fd\u66f4\u4f18\u96c5\u5730\u5904\u7406\u5404\u7c7b\u5e76\u53d1\u573a\u666f\u3002\u672c\u6587\u7528 \u201c\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/70567.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-02-01T19:17:29+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260201191728-697fa6c80495f.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/70567.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/70567.html\",\"name\":\"Java \u5e76\u53d1\u5de5\u5177\u7c7b\u8be6\u89e3\uff1a4 \u5927\u6838\u5fc3\u5de5\u5177 + \u5b9e\u6218\u573a\u666f\uff0c\u544a\u522b synchronized - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-02-01T19:17:29+00:00\",\"dateModified\":\"2026-02-01T19:17:29+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/70567.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/70567.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/70567.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java \u5e76\u53d1\u5de5\u5177\u7c7b\u8be6\u89e3\uff1a4 \u5927\u6838\u5fc3\u5de5\u5177 + \u5b9e\u6218\u573a\u666f\uff0c\u544a\u522b synchronized\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java \u5e76\u53d1\u5de5\u5177\u7c7b\u8be6\u89e3\uff1a4 \u5927\u6838\u5fc3\u5de5\u5177 + \u5b9e\u6218\u573a\u666f\uff0c\u544a\u522b synchronized - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","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.wsisp.com\/helps\/70567.html","og_locale":"zh_CN","og_type":"article","og_title":"Java \u5e76\u53d1\u5de5\u5177\u7c7b\u8be6\u89e3\uff1a4 \u5927\u6838\u5fc3\u5de5\u5177 + \u5b9e\u6218\u573a\u666f\uff0c\u544a\u522b synchronized - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u5728 Java \u5e76\u53d1\u7f16\u7a0b\u4e2d&#xff0c;synchronized\u867d\u7136\u80fd\u89e3\u51b3\u7ebf\u7a0b\u540c\u6b65\u95ee\u9898&#xff0c;\u4f46\u529f\u80fd\u5355\u4e00\u3001\u7075\u6d3b\u6027\u5dee&#xff0c;\u9762\u5bf9\u590d\u6742\u573a\u666f&#xff08;\u5982\u591a\u7ebf\u7a0b\u7b49\u5f85\u3001\u6d41\u91cf\u63a7\u5236\u3001\u6570\u636e\u4ea4\u6362&#xff09;\u65f6\u529b\u4e0d\u4ece\u5fc3\u3002JDK \u63d0\u4f9b\u7684CountDownLatch\u3001CyclicBarrier\u3001Semaphore\u3001Exchanger\u56db\u5927\u5e76\u53d1\u5de5\u5177\u7c7b&#xff0c;\u57fa\u4e8e AQS&#xff08;\u961f\u5217\u5f0f\u540c\u6b65\u5668&#xff09;\u5b9e\u73b0&#xff0c;\u80fd\u66f4\u4f18\u96c5\u5730\u5904\u7406\u5404\u7c7b\u5e76\u53d1\u573a\u666f\u3002\u672c\u6587\u7528 \u201c","og_url":"https:\/\/www.wsisp.com\/helps\/70567.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-02-01T19:17:29+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260201191728-697fa6c80495f.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"6 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/70567.html","url":"https:\/\/www.wsisp.com\/helps\/70567.html","name":"Java \u5e76\u53d1\u5de5\u5177\u7c7b\u8be6\u89e3\uff1a4 \u5927\u6838\u5fc3\u5de5\u5177 + \u5b9e\u6218\u573a\u666f\uff0c\u544a\u522b synchronized - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-02-01T19:17:29+00:00","dateModified":"2026-02-01T19:17:29+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/70567.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/70567.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/70567.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Java \u5e76\u53d1\u5de5\u5177\u7c7b\u8be6\u89e3\uff1a4 \u5927\u6838\u5fc3\u5de5\u5177 + \u5b9e\u6218\u573a\u666f\uff0c\u544a\u522b synchronized"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/70567","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=70567"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/70567\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/70566"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=70567"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=70567"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=70567"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=70567"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}