{"id":91439,"date":"2020-05-03T13:27:42","date_gmt":"2020-05-03T05:27:42","guid":{"rendered":"http:\/\/4563.org\/?p=91439"},"modified":"2020-05-03T13:27:42","modified_gmt":"2020-05-03T05:27:42","slug":"java-%e7%bc%96%e7%a8%8b%e6%80%9d%e6%83%b3%e7%9a%84%e8%bf%99%e4%b8%aa-ornamentalgarden-%e4%be%8b%e5%ad%90%e4%b8%ba%e4%bb%80%e4%b9%88%e6%98%af%e8%bf%99%e7%a7%8d%e6%89%93%e5%8d%b0%e7%bb%93%e6%9e%9c","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=91439","title":{"rendered":"Java \u7f16\u7a0b\u601d\u60f3\u7684\u8fd9\u4e2a OrnamentalGarden \u4f8b\u5b50\u4e3a\u4ec0\u4e48\u662f\u8fd9\u79cd\u6253\u5370\u7ed3\u679c\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  Java \u7f16\u7a0b\u601d\u60f3\u7684\u8fd9\u4e2a OrnamentalGarden \u4f8b\u5b50\u4e3a\u4ec0\u4e48\u662f\u8fd9\u79cd\u6253\u5370\u7ed3\u679c\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : amiwrong123 <\/span>  <span><i><\/i> 15<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u8fd9\u662f java \u7f16\u7a0b\u601d\u60f3\u7684\u4e00\u4e2a\u5c0f\u4f8b\u5b50\uff0c\u5927\u6982\u610f\u601d\uff0c\u4e94\u4e2a Runnable \u5bf9\u8c61\u4f1a\u5bf9\u81ea\u5df1\u52a0\u9501\uff08\u4f46\u5bf9\u81ea\u5df1\u52a0\u9501\uff0c\u56e0\u4e3a\u6ca1\u6709\u522b\u4eba\u548c\u5b83\u672c\u8eab\u51b2\u7a81\uff0c\u6240\u4ee5\u6ca1\u6709\u610f\u4e49\uff09\uff0c\u8fd8\u6709\u4e2a Count \u5bf9\u8c61\uff0c\u5b83\u4e5f\u6709\u540c\u6b65\u65b9\u6cd5\uff0c\u4e94\u4e2a\u7ebf\u7a0b\u90fd\u4f1a\u8c03\u5230\u5b83\uff0c\u81ea\u7136\uff0c\u53ea\u6709\u5b83\u7684\u540c\u6b65\u65b9\u6cd5\u624d\u662f\u6709\u610f\u4e49\u7684\u3002<\/p>\n<pre><code>import java.util.concurrent.*; import java.util.*; import java.util.concurrent.atomic.AtomicInteger;  import static net.mindview.util.Print.*;  class Count {     private int count = 0;     private Random rand = new Random(47);     \/\/ Remove the synchronized keyword to see counting fail:     public synchronized int increment() {         int temp = count;         System.out.println(Thread.currentThread().getName()+\" befor yield\");         if(rand.nextBoolean()) \/\/ Yield half the time             Thread.yield();         System.out.println(Thread.currentThread().getName()+\" after yield\");         return (count = ++temp);     }     public synchronized int value() { return count; } }  class Entrance implements Runnable {     private static Count count = new Count();     private static List&lt;Entrance&gt; entrances =             new ArrayList&lt;Entrance&gt;();     private int number = 0;     \/\/ Doesn't need synchronization to read:     private final int id;     private static volatile boolean canceled = false;     \/\/ Atomic operation on a volatile field:     public static void cancel() { canceled = true; }     public Entrance(int id) {         this.id = id;         \/\/ Keep this task in a list. Also prevents         \/\/ garbage collection of dead tasks:                  entrances.add(this);     }     public void run() {         while(!canceled) {             synchronized(this) {                 ++number;             }             print(this + \" Total: \" + count.increment());             try {                 TimeUnit.MILLISECONDS.sleep(100);             } catch(InterruptedException e) {                 print(\"sleep interrupted\");             }         }         print(\"Stopping \" + this);     }     public synchronized int getValue() { return number; }     public String toString() {         return \"Entrance \" + Thread.currentThread().getName() + \": \" + getValue();     }     public static int getTotalCount() {         return count.value();     }     public static int sumEntrances() {         int sum = 0;         for(Entrance entrance : entrances)             sum += entrance.getValue();         return sum;     } }  public class OrnamentalGarden {     public static void main(String[] args) throws Exception {         AtomicInteger a = new AtomicInteger();         ExecutorService exec = new ThreadPoolExecutor(0, Integer.MAX_VALUE,                 60L, TimeUnit.SECONDS,                 new SynchronousQueue&lt;Runnable&gt;(),new ThreadFactory(){ public Thread newThread(Runnable r) {             return new Thread(r, \"pool_\" + a.getAndIncrement());         }});         for(int i = 0; i &lt; 5; i++)             exec.execute(new Entrance(i));         \/\/ Run for a while, then stop and collect the data:         TimeUnit.SECONDS.sleep(3);         Entrance.cancel();         exec.shutdown();         if(!exec.awaitTermination(250, TimeUnit.MILLISECONDS))             print(\"Some tasks were not terminated!\");         print(\"Total: \" + Entrance.getTotalCount());         print(\"Sum of Entrances: \" + Entrance.sumEntrances());     } } <\/code><\/pre>\n<p>\u53ef\u80fd\u597d\u591a\u4eba\u770b\u5230\u8fd9\u91cc\u76f4\u63a5\u7565\u8fc7\u4e86\uff0c\u4f46\u6211\u770b\u5b8c\u8fd9\u4e2a\u4f8b\u5b50\u786e\u5b9e\u4f3c\u61c2\u975e\u61c2\u7684\u3002https:\/\/paste.ubuntu.com\/p\/B7GQMcMhv7\/ \uff0c\u8fd9\u662f\u539f\u7248\u4ee3\u7801\uff0c\u4e3a\u4e86\u770b\u5230\u7b80\u5355\u7684\u7ebf\u7a0b\u540d\uff0c\u7a0d\u5fae\u6539\u4e86\u4e0b\u3002\u6267\u884c\u5b8c\u540e\uff0c\u6253\u5370\u7ed3\u679c\u662f\uff1a<\/p>\n<pre><code>pool_0 befor yield pool_0 after yield pool_2 befor yield pool_2 after yield pool_4 befor yield pool_4 after yield pool_3 befor yield pool_3 after yield pool_1 befor yield pool_1 after yield Entrance pool_0: 1 Total: 1 Entrance pool_3: 1 Total: 4   \u4e3a\u5565\u8fd9\u91cc\u76f4\u63a5\u662f 4 \u4e86\uff1f Entrance pool_1: 1 Total: 5 Entrance pool_2: 1 Total: 2 Entrance pool_4: 1 Total: 3 pool_0 befor yield pool_0 after yield <\/code><\/pre>\n<p>\u5982\u4e0a\uff0c\u4e3a\u5565\u90a3\u91cc\u76f4\u63a5\u662f 4 \u4e86\uff1f\u90a3\u4e2a 4 \u662f count \u5bf9\u8c61\u7684\u540c\u6b65\u65b9\u6cd5\uff0c\u6309\u7406\u8bf4\u5e94\u8be5\u63a5\u4e0b\u6765\u6253\u5370 2 \u554a\u3002\u6240\u4ee5\u8fd9\u4e2a\u7a0b\u5e8f\u662f\u60f3\u8bf4\u660e\uff0c1 \u8fc7\u4e86\u76f4\u63a5\u6253\u5370 4\uff0c\u662f\u56e0\u4e3a\u7ebf\u7a0b\u8c03\u5ea6\u5417\uff1f\u4f5c\u8005\u60f3\u8bf4\u660e\u4ec0\u4e48\u554a<\/p>\n<p>PS\uff1a\u6709\u65f6\u5019\u611f\u89c9 Java \u7f16\u7a0b\u601d\u60f3\u7684\u4f8b\u5b50\u597d\u6076\u5fc3\u3002\u3002\u3002<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>8<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"1248848\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : hfc <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8fd9\u5c31\u662f\u591a\u7ebf\u7a0b\u95ee\u9898\u5427\uff0c\u4f60\u6309\u7ebf\u7a0b\u6253\u5370\u201cbefore\/after yield\u201d\u7684\u987a\u5e8f\u53bb\u770b\u5bf9\u5e94\u7684 Total \u503c\u662f\u6b63\u786e\u9012\u589e\u7684\u3002<br \/>Count \u5bf9\u8c61\u7684\u540c\u6b65\u65b9\u6cd5\u4e5f\u6ca1\u610f\u4e49\uff0c\u56e0\u4e3a\u6bcf\u4e2a Entrance \u90fd\u6709\u81ea\u5df1\u7684 Count \u5b9e\u4f8b\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1248849\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : amiwrong123 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @hfc <br \/>\u4e0d\u662f\u5427\uff0c\u6211\u611f\u89c9\u53ea\u6709\u4e00\u4e2a Count \u5bf9\u8c61\u5b58\u5728\u5427\u3002\u4f5c\u4e3a\u4e00\u4e2a\u9759\u6001\u57df                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1248850\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : hfc <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @amiwrong123 \u54e6\uff0c\u5931\u8bef\u4e86\uff0cCount \u8fd9\u4e2a\u6211\u5c11\u770b\u4e86\u4e00\u4e2a static \u3002\u3002\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1248851\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : amiwrong123 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @hfc <br \/>\u54ce\uff0c\u6240\u4ee5\u73b0\u5728\u6211\u5c31\u6709\u70b9\u6ca1\u770b\u61c2\u8fd9\u4e2a\u6253\u5370\u7ed3\u679c\u4e86\u3002\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1248852\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : etoah <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8fd9\u4e2a\u95ee\u9898\u6700\u4e3b\u8981\u7684\u5c31 &lt;print(this + &#8221; Total: &#8221; + count.increment());&gt; \u8fd9\u53e5\u8bdd<\/p>\n<p>\u524d\u9762\u7684\u540c\u6b65\u5757 \u5176\u5b9e\u6ca1\u4ec0\u4e48\u7528. \u73b0\u5728\u5047\u8bbe 5 \u4e2a\u7ebf\u7a0b\u90fd\u8d70\u5b8c\u540c\u6b65\u5757, \u540c\u65f6\u5230\u8fbe &lt;print(this + &#8221; Total: &#8221; + count.increment());&gt;<\/p>\n<p>\u5148\u5c06\u8fd9\u4e2a\u8bed\u53e5\u5206\u6210\u4e24\u4e2a<br \/>int countValue = count.increment();<br \/>print(this + &#8220;Total:&#8221; + countValue);<\/p>\n<p>\u56e0\u4e3a\u540c\u65f6\u6709 5 \u4e2a\u7ebf\u7a0b\u5728\u8dd1 \u6211\u4eec\u5c06\u4e0a\u9762\u7684\u4ee3\u7801\u590d\u5236 5 \u4efd (\u7b2c\u4e00\u884c\u6807\u8bc6\u4ee3\u7801\u884c\u53f7 \u5341\u4f4d\u6570\u4ee3\u8868\u7ebf\u7a0b id)<\/p>\n<p>pool_0 \u7ebf\u7a0b:<br \/>01 int countValue00 = count.increment(); <br \/>02 print(this + &#8220;Total:&#8221; + countValue00); <\/p>\n<p>pool_1 \u7ebf\u7a0b:<br \/>11 int countValue01 = count.increment();<br \/>12 print(this + &#8220;Total:&#8221; + countValue01);<\/p>\n<p>pool_2 \u7ebf\u7a0b:<br \/>21 int countValue02 = count.increment();<br \/>22 print(this + &#8220;Total:&#8221; + countValue02);<\/p>\n<p>pool_3 \u7ebf\u7a0b:<br \/>31 int countValue03 = count.increment();<br \/>32 print(this + &#8220;Total:&#8221; + countValue03);<\/p>\n<p>pool_4 \u7ebf\u7a0b:<br \/>41 int countValue04 = count.increment();<br \/>42 print(this + &#8220;Total:&#8221; + countValue04);<\/p>\n<p>\u5206\u522b\u4ee3\u8868\u6bcf\u4e00\u6761\u7ebf\u7a0b\u6267\u884c\u7684\u4ee3\u7801 <\/p>\n<p>\u4ece\u8f93\u51fa\u7ed3\u679c\u770b cpu \u6267\u884c\u7684\u987a\u5e8f\u662f<br \/> 01 21 41 31 11 <br \/> 02 32 12 22 42<\/p>\n<p>\u7b2c\u4e00\u884c\u6267\u884c\u540e countValue00 &#8211; 05 \u7684\u503c \u5206\u522b\u662f <br \/>\u7ebf\u7a0b pool_id: 0 2 4 3 1 \/\/ \u53d6 cpu \u6267\u884c\u987a\u5e8f\u7684\u5341\u4f4d\u6570<br \/>countValue : 1 2 3 4 5 \/\/ \u6309\u6267\u884c\u987a\u5e8f\u9012\u589e <\/p>\n<p>\u7b2c\u4e8c\u884c\u7684\u987a\u5e8f<br \/>\u7ebf\u7a0b pool_id: 0 3 1 2 4<br \/>\u8f93\u51fa countValue: 1 4 5 2 3<\/p>\n<p>\u662f\u4e0d\u662f\u5c31\u5bf9\u4e0a\u4e86<\/p>\n<p>\u4e3b\u8981\u662f &lt;print(this + &#8221; Total: &#8221; + count.increment());&gt; \u8fd9\u6761\u8bed\u53e5\u5176\u5b9e\u662f\u4e0d\u662f\u4e00\u4e2a\u539f\u5b50\u64cd\u4f5c                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1248853\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : amiwrong123 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @etoah <br \/>\u8c22\u8c22\u56de\u7b54\uff0c\u4f60\u8fd9\u4e48\u4e00\u7406\uff0c\u786e\u5b9e\u662f\u5bf9\u4e0a\u4e86\u3002<br \/>\u628a\u90a3\u4e2a\u8bed\u53e5\u62c6\u5206\u6210\u4e24\u53e5\u786e\u5b9e\u6e05\u695a\u4e86\uff0c\u8fd8\u6709\u5c31\u662f\u62c6\u5206\u540e\u7684\u7b2c\u4e8c\u53e5\u7684\u6267\u884c\u987a\u5e8f\uff0c\u8ddf\u7b2c\u4e00\u53e5\u7684\u4e0d\u4e00\u6837\u7684\u539f\u56e0\uff1a\u662f\u4e0d\u662f\u56e0\u4e3a\uff0c\u7ebf\u7a0b\u4e4b\u95f4\u6ca1\u6709\u4e92\u65a5\u9501\uff0c\u6240\u4ee5\u6267\u884c\u987a\u5e8f\u5c31\u662f\u968f\u673a\u7684\uff08\u8ddf\u7ebf\u7a0b\u6c60\u6709\u5173\u5417\uff09\uff1f<\/p>\n<p>\u5f53\u7136\uff0c\u6bcf\u6b21\u6253\u5370\u7ed3\u679c\u90fd\u53ef\u80fd\u4e0d\u4e00\u6837\uff0c\u4f46\u81f3\u5c11\uff0c\u80fd\u7528\u4f60\u7684\u65b9\u6cd5\u5bf9\u4e0a\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1248854\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : etoah <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @amiwrong123 <br \/>\u7b2c\u4e8c\u53e5\u548c\u7b2c\u4e00\u53e5\u6267\u884c\u987a\u5e8f\u4e0d\u540c\u5c31\u662f\u7ebf\u7a0b\u8c03\u5ea6\u95ee\u9898\u4e86<br \/>\u6bcf\u6b21\u7ed3\u679c\u4e0d\u4e00\u6837\u5c31\u662f\u6267\u884c\u987a\u5e8f\u968f\u673a\u7684\u95ee\u9898<\/p>\n<p>\u5982\u679c\u60f3\u63a7\u5236\u7ebf\u7a0b\u6267\u884c\u987a\u5e8f\u53ef\u4ee5\u7528\u7ebf\u7a0b\u540c\u6b65\u7684\u65b9\u6cd5                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1248855\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : amiwrong123 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @etoah <br \/>\u8c22\u8c22\uff0c\u5927\u6982\u660e\u767d\u4e86\u3002\u6bd4\u5982\uff0c\u6211\u53ef\u4ee5 Entrance \u91cc\u6539\u6210 synchronized(Entrance.getClass()) {\/\/run \u91cc\u9762\u7684\u6240\u6709\u903b\u8f91}\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<p>\u867d\u8bf4\u8fd9\u6837\u6539\u53d8\u7a0b\u5e8f\u7684\u672c\u610f\uff0cEntrance \u4ee3\u8868\u5404\u4e2a\u5927\u95e8\uff0c\u90a3\u81ea\u7136\u5404\u4e2a\u5927\u95e8\u53ef\u4ee5\u540c\u65f6\u8fdb\u5165\u4eba\uff0c\u4f46\u8fd9\u6837\u6539\u4e86\uff0c\u5373\u4f7f\u6709\u4e94\u4e2a\u5927\u95e8\uff0c\u540c\u4e00\u65f6\u523b\uff0c\u53ea\u80fd\u6709\u4e00\u4e2a\u5927\u95e8\u53ef\u4ee5\u8fdb\u4eba\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>Java \u7f16\u7a0b\u601d\u60f3\u7684\u8fd9\u4e2a Orna&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/91439"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=91439"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/91439\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=91439"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=91439"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=91439"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}