{"id":3994,"date":"2019-03-14T14:14:02","date_gmt":"2019-03-14T06:14:02","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=3994"},"modified":"2019-03-14T14:14:02","modified_gmt":"2019-03-14T06:14:02","slug":"%e9%98%bf%e9%87%8c%e7%9a%84%e4%b8%80%e9%81%93%e7%ba%bf%e7%a8%8b%e9%9d%a2%e8%af%95%e9%a2%98%ef%bc%8c%e9%9d%a2%e5%93%ad%e4%ba%86%e6%97%a0%e6%95%b0%e4%ba%ba","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=3994","title":{"rendered":"\u963f\u91cc\u7684\u4e00\u9053\u7ebf\u7a0b\u9762\u8bd5\u9898\uff0c\u9762\u54ed\u4e86\u65e0\u6570\u4eba"},"content":{"rendered":"<p>\n\t\u5f88\u591a\u4eba\u90fd\u60f3\u8fdb BAT\uff0c\u4f46\u662f\u5e76\u4e0d\u662f\u6bcf\u4e2a\u4eba\u90fd\u80fd\u5982\u613f\u3002BAT \u7684\u9762\u8bd5\u975e\u5e38\u7684\u4e25\u683c\uff0c\u4eca\u5e74\u6625\u62db\uff0c\u963f\u91cc\u7684\u4e00\u9053\u7ebf\u7a0b\u9762\u8bd5\u9898\uff0c\u5c31\u6dd8\u6c70\u4e86\u65e0\u6570\u7684\u4eba\uff01\n<\/p>\n<p>\n\t\u4eca\u5929\u6211\u5bf9\u8fd9\u9053\u9898\uff0c\u5206\u522b\u7528\u4e86 3 \u79cd\u4e0d\u540c\u7684\u65b9\u6cd5\u5b9e\u73b0\u4e86\uff0c\u4f9b\u5927\u5bb6\u53c2\u8003\uff01\n<\/p>\n<p>\n\t\u9898\u76ee\u5982\u4e0b\uff1a\n<\/p>\n<blockquote>\n<p>\n\t\t\u901a\u8fc7N\u4e2a\u7ebf\u7a0b\u987a\u5e8f\u5faa\u73af\u6253\u5370\u4ece0\u81f3100\uff0c\u5982\u7ed9\u5b9aN=3\u5219\u8f93\u51fa:<br \/>\n\t\tthread0: 0<br \/>\n\t\tthread1: 1<br \/>\n\t\tthread2: 2<br \/>\n\t\tthread0: 3<br \/>\n\t\tthread1: 4<br \/>\n\t\t&#8230;..\n\t<\/p>\n<\/blockquote>\n<p>\n\t\u7b2c\u4e00\u79cd\uff0c\u901a\u8fc7\u5224\u65ad\u6267\u884c\u72b6\u6001\u6765\u5b9e\u73b0\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\nstatic int result = 0;\r\npublic static void orderPrintNum(final int n){\r\n    final boolean [] printFlag = new boolean [n];\r\n    for (int i = 0; i &lt; n; i++) {\r\n        final int index = i;\r\n        if(0 == i){\r\n            printFlag[index] = true;\r\n        }\r\n        new Thread(new Runnable() {\r\n            public void run() {\r\n                try {\r\n                    while (result &lt;= 100) {\r\n                        if(printFlag[index]){\r\n                            System.out.println(&quot;thread&quot; + index + &quot;: &quot; + result++);\r\n                            printFlag[index] = false;\r\n                            if(index == (n - 1)){\r\n                                printFlag[0] = true;\r\n                            }else {\r\n                                printFlag[index + 1] = true;\r\n                            }\r\n                        }\r\n                    }\r\n                } catch (Exception e) {\r\n                    e.printStackTrace();\r\n                }\r\n\r\n            }\r\n        }).start();\r\n    }\r\n}<\/pre>\n<p>\n\t\u60f3\u5230\u8fd9\u79cd\u65b9\u5f0f\u7684\uff0c\u5e94\u8be5\u5c31\u80fd\u60f3\u5230\u4fe1\u53f7\u91cf\u3002\u56e0\u6b64\uff0c\u7b2c\u4e8c\u79cd\u5b9e\u73b0\u65b9\u6cd5\u5c31\u662f\u901a\u8fc7\u4fe1\u53f7\u91cf\u6765\u5b9e\u73b0\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\nstatic int result = 0;\r\npublic static void orderPrintNum2(final int n) throws InterruptedException{\r\n    \/\/ N \u4e2a\u7ebf\u7a0b\r\n    Thread [] threads = new Thread[n];\r\n    \/\/ N \u4e2aSemaphore\r\n    final Semaphore[] syncSemaphore = new Semaphore[n];\r\n    for (int i = 0; i &lt; n; i++) {\r\n        \/\/\u975e\u516c\u5e73\u4fe1\u53f7\u91cf\uff0c\u8ba1\u6570\u4e3a1\r\n        syncSemaphore[i] = new Semaphore(1);\r\n        if (i != n-1){\r\n            \/\/\u83b7\u53d6\u4e00\u4e2a\u8bb8\u53ef\u524d\u7ebf\u7a0b\u5c06\u4e00\u76f4\u963b\u585e\r\n            syncSemaphore[i].acquire();\r\n        }\r\n    }\r\n    for (int i = 0; i &lt; n; i++) {\r\n        final Semaphore lastSemphore = i == 0 ? syncSemaphore[n - 1] : syncSemaphore[i - 1];\r\n        final Semaphore curSemphore = syncSemaphore[i];\r\n        final int index = i;\r\n        threads[i] = new Thread(new Runnable() {\r\n            public void run() {\r\n                try {\r\n                    while (true) {\r\n                        lastSemphore.acquire();\r\n                        System.out.println(&quot;thread&quot; + index + &quot;: &quot; + result++);\r\n                        if (result &gt; 100){\r\n                            System.exit(0);\r\n                        }\r\n                        curSemphore.release();\r\n                    }\r\n                } catch (Exception e) {\r\n                    e.printStackTrace();\r\n                }\r\n\r\n            }\r\n        });\r\n        threads[i].start();\r\n    }\r\n}<\/pre>\n<p>\n\t\u7b2c\u4e09\u79cd\uff0c\u901a\u8fc7 wait \u548c notifyAll \u6765\u5b9e\u73b0\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\nstatic int result = 0;\r\npublic static void orderPrintNum3(final int n) {\r\n    final Object LOCK = new Object();\r\n    final int [] flag = new int[1];\r\n    for (int i = 0;i &lt; n; i++){\r\n        final int index = i;\r\n        new Thread(new Runnable() {\r\n            public void run() {\r\n                try {\r\n                    while (result &lt;= 100) {\r\n                        synchronized (LOCK){\r\n                            if(flag[0] == index){\r\n                                System.out.println(&quot;thread&quot; + index + &quot;: &quot; + result++);\r\n                                flag[0] = index == n - 1 ? 0 : index + 1;\r\n                                \/\/ \u5524\u9192\u5176\u4ed6wait\u7ebf\u7a0b\r\n                                LOCK.notifyAll();\r\n                            }else {\r\n                                LOCK.wait();\r\n                            }\r\n                        }\r\n                    }\r\n                } catch (Exception e) {\r\n                    e.printStackTrace();\r\n                }\r\n            }\r\n        }).start();\r\n    }\r\n}<\/pre>\n<p>\n\t3 \u79cd\u65b9\u6cd5\u7684\u8fd0\u884c\u6548\u679c\uff0c\u622a\u56fe\u5982\u4e0b\uff1a\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"\u7ebf\u7a0b\u7b14\u8bd5\u9898\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/wx1.sinaimg.cn\/large\/8fa5dcfcgy1g12ao50cwbj207g03874e.jpg\" style=\"width: 268px;height: 116px\" \/>\n<\/p>\n<p>\n\t\u9664\u6b64\u4e4b\u5916\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u961f\u5217\uff0c\u94fe\u8868\u7b49\u5f62\u5f0f\u6765\u5b9e\u73b0\u3002\u5177\u4f53\uff0c\u5927\u5bb6\u53bb\u601d\u8003\u601d\u8003\u5427\uff01<\/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=3994\">\u963f\u91cc\u7684\u4e00\u9053\u7ebf\u7a0b\u9762\u8bd5\u9898\uff0c\u9762\u54ed\u4e86\u65e0\u6570\u4eba<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u5f88\u591a\u4eba\u90fd\u60f3\u8fdb BAT\uff0c\u4f46\u662f\u5e76\u4e0d\u662f\u6bcf\u4e2a\u4eba\u90fd\u80fd\u5982\u613f\u3002BAT \u7684\u9762\u8bd5\u975e\u5e38\u7684\u4e25\u683c\uff0c\u4eca\u5e74\u6625\u62db\uff0c\u963f\u91cc\u7684\u4e00\u9053\u7ebf\u7a0b\u9762\u8bd5\u9898\uff0c\u5c31\u6dd8 [&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":[565,202,531,1104,1931],"class_list":["post-3994","post","type-post","status-publish","format-standard","hentry","category-java","tag-java-","tag-locked","tag-531","tag-1104","tag-1931"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3994","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=3994"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3994\/revisions"}],"predecessor-version":[{"id":3995,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3994\/revisions\/3995"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3994"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3994"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3994"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}