{"id":217511,"date":"2020-12-05T10:04:01","date_gmt":"2020-12-05T02:04:01","guid":{"rendered":"http:\/\/4563.org\/?p=217511"},"modified":"2020-12-06T07:31:54","modified_gmt":"2020-12-05T23:31:54","slug":"%e9%97%ae%e4%b8%80%e4%b8%aa-java-%e7%9a%84-condition-%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=217511","title":{"rendered":"\u95ee\u4e00\u4e2a Java \u7684 Condition \u95ee\u9898"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u95ee\u4e00\u4e2a Java \u7684 Condition \u95ee\u9898               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : overthemoon <\/span>  <span><i><\/i> 2<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u6709\u4e24\u4e2a\u7ebf\u7a0b\uff0c\u4e00\u4e2a\u751f\u4ea7\u8005\uff0c\u4e00\u4e2a\u6d88\u8d39\u8005\uff0c\u4e0d\u65ad\u7684\u5728\u961f\u5217\u91cc\u9762\u63d2\u6570\u636e\u548c\u53d6\u6570\u636e\u3002 \u6709\u4e2a\u95ee\u9898 notFull.signalAll()\u8fd9\u53e5\u4ee3\u7801\u4e00\u4e2a\u653e\u5728 try \u91cc\u9762\uff0c\u4e00\u4e2a\u653e\u5728 finally \u91cc\u9762\u63a7\u5236\u53f0\u8f93\u51fa\u7ed3\u679c\u4e3a\u4ec0\u4e48\u4e0d\u4e00\u6837\uff1f<\/p>\n<pre><code> public class ConditionDemo2 {      private int queueSize = 10;     private PriorityQueue&lt;Integer&gt; queue = new PriorityQueue&lt;Integer&gt;(queueSize);     private Lock lock = new ReentrantLock();     private Condition notFull = lock.newCondition();     private Condition notEmpty = lock.newCondition();      public static void main(String[] args) {         ConditionDemo2 conditionDemo2 = new ConditionDemo2();         Producer producer = conditionDemo2.new Producer();         Consumer consumer = conditionDemo2.new Consumer();         producer.start();         consumer.start();     }      class Consumer extends Thread {          @Override         public void run() {             consume();         }          private void consume() {             while (true) {                 lock.lock();                 try {                     while (queue.size() == 0) {                         System.out.println(\"\u961f\u5217\u7a7a\uff0c\u7b49\u5f85\u6570\u636e\");                         try {                             notEmpty.await();                         } catch (InterruptedException e) {                             e.printStackTrace();                         }                     }                     queue.poll();                     notFull.signalAll();\/\/\u770b\u8fd9\u91cc----------------------                     System.out.println(\"\u4ece\u961f\u5217\u91cc\u53d6\u8d70\u4e86\u4e00\u4e2a\u6570\u636e\uff0c\u961f\u5217\u5269\u4f59\" + queue.size() + \"\u4e2a\u5143\u7d20\");                 } finally {                     lock.unlock();                 }             }         }     }      class Producer extends Thread {          @Override         public void run() {             produce();         }          private void produce() {             while (true) {                 lock.lock();                 try {                     while (queue.size() == queueSize) {                         System.out.println(\"\u961f\u5217\u6ee1\uff0c\u7b49\u5f85\u6709\u7a7a\u4f59\");                         try {                             notFull.await();                         } catch (InterruptedException e) {                             e.printStackTrace();                         }                     }                     queue.offer(1);                     notEmpty.signalAll();\/\/\u770b\u8fd9\u91cc----------------------                     System.out.println(\"\u5411\u961f\u5217\u63d2\u5165\u4e86\u4e00\u4e2a\u5143\u7d20\uff0c\u961f\u5217\u5269\u4f59\u7a7a\u95f4\" + (queueSize - queue.size()));                 } finally {                     lock.unlock();                 }             }         }     }  }   <\/code><\/pre>\n<pre><code> public class ConditionDemo3 {      private int queueSize = 10;     private PriorityQueue&lt;Integer&gt; queue = new PriorityQueue&lt;Integer&gt;(queueSize);     private Lock lock = new ReentrantLock();     private Condition notFull = lock.newCondition();     private Condition notEmpty = lock.newCondition();      public static void main(String[] args) {         ConditionDemo3 conditionDemo2 = new ConditionDemo3();         Producer producer = conditionDemo2.new Producer();         Consumer consumer = conditionDemo2.new Consumer();         producer.start();         consumer.start();     }      class Consumer extends Thread {          @Override         public void run() {             consume();         }          private void consume() {             while (true) {                 lock.lock();                 try {                     while (queue.size() == 0) {                         System.out.println(\"\u961f\u5217\u7a7a\uff0c\u7b49\u5f85\u6570\u636e\");                         try {                             notEmpty.await();                         } catch (InterruptedException e) {                             e.printStackTrace();                         }                     }                     queue.poll();                     System.out.println(\"\u4ece\u961f\u5217\u91cc\u53d6\u8d70\u4e86\u4e00\u4e2a\u6570\u636e\uff0c\u961f\u5217\u5269\u4f59\" + queue.size() + \"\u4e2a\u5143\u7d20\");                 } finally {                     notFull.signalAll();\/\/\u770b\u8fd9\u91cc----------------------                     lock.unlock();                 }             }         }     }      class Producer extends Thread {          @Override         public void run() {             produce();         }          private void produce() {             while (true) {                 lock.lock();                 try {                     while (queue.size() == queueSize) {                         System.out.println(\"\u961f\u5217\u6ee1\uff0c\u7b49\u5f85\u6709\u7a7a\u4f59\");                         try {                             notFull.await();                         } catch (InterruptedException e) {                             e.printStackTrace();                         }                     }                     queue.offer(1);                     System.out.println(\"\u5411\u961f\u5217\u63d2\u5165\u4e86\u4e00\u4e2a\u5143\u7d20\uff0c\u961f\u5217\u5269\u4f59\u7a7a\u95f4\" + (queueSize - queue.size()));                 } finally {                     System.out.println(\"final\");                     notFull.signalAll();\/\/\u770b\u8fd9\u91cc----------------------                     lock.unlock();                 }             }         }     }  }   <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>3<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"4426545\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : overthemoon <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6b64\u8d34\u7ec8\u7ed3\uff0c\u6211\u4ee3\u7801\u770b\u9519\u4e86==~~~~~~~~                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4426546\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : bxb100 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @overthemoon #1 <img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/12\/504J5BO2-14.png\" alt=\"\u95ee\u4e00\u4e2a Java \u7684 Condition \u95ee\u9898\"> \u5206\u4eab\u4e00\u4e0b                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4426547\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zcsz <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @overthemoon \u4e8f\u6211\u70b9\u8fdb\u6765\u4ece\u4e0a\u5230\u4e0b\u4ed4\u4ed4\u7ec6\u7ec6\u770b\u4e86\u4e00\u904d\uff0c\u62c9\u5230 1 &#8230;&#8230;&#8230;&#8230;..                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u95ee\u4e00\u4e2a Java \u7684 Condit&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\/217511"}],"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=217511"}],"version-history":[{"count":1,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/217511\/revisions"}],"predecessor-version":[{"id":217513,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/217511\/revisions\/217513"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=217511"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=217511"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=217511"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}