Java Gossip: wait()、notify()

http://caterpillar.onlyfun.net/GossipCN/JavaGossip-V2/WaitNotify.htm


wait()、notify() notifyAll()   Object      ,               (  Java             Object),wait()、notify()  notifyAll()     "final",           ,                  Runnable  。

                wait()  ,    wait()     ,             “    ”(Wait set) ,           ,            ,                 ;                     ,wait()         ,        ,                ,      0    ,         ,      (interrupt)     (notify)      。

    notify()   ,            “  ”       ,           ,                             ;     notifyAll(), “  ”              ,                           。

    ,          wait()   ,                      ,           ,                   ,           ,             (   1     ),     (     )         。

  wait()、notify() notifyAll()           ,     (Producer)    (Consumer)   ,             ,            ,              ,             ,         (wait),              (notify)       ,         ,           (wait),           (notify)        。


         :         int        ,            ,            。

       ,      : 

Producer.java 
package onlyfun.caterpillar; public class Producer implements Runnable {    private Clerk clerk;         public Producer(Clerk clerk) {         this.clerk = clerk;     }         public void run() {         System.out.println(                "         ......");         //   1 10           for(int product = 1; product <= 10; product++) {             try {                 //                       Thread.sleep((int) (Math.random() * 3000));             }             catch(InterruptedException e) {                 e.printStackTrace();             }             //                    clerk.setProduct(product);         }           } } 

      :

Consumer.java 
package onlyfun.caterpillar; public class Consumer implements Runnable {    private Clerk clerk;         public Consumer(Clerk clerk) {         this.clerk = clerk;     }         public void run() {         System.out.println(                "         ......");         //   10           for(int i = 1; i <= 10; i++) {             try {                 //                       Thread.sleep((int) (Math.random() * 3000));             }             catch(InterruptedException e) {                 e.printStackTrace();             }             //                     clerk.getProduct();         }     }  } 

          ,            ,                 。

Clerk.java 
package onlyfun.caterpillar; public class Clerk {    // -1             private int product = -1;      //               public synchronized void setProduct(int product) {         if(this.product != -1) {             try {                 //            ,   !                wait();             }             catch(InterruptedException e) {                 e.printStackTrace();             }         }          this.product = product;         System.out.printf("      (%d)%n", this.product);         //                            notify();     }         //               public synchronized int getProduct() {         if(this.product == -1) {             try {                 //    ,   !                wait();             }             catch(InterruptedException e) {                 e.printStackTrace();             }         }          int p = this.product;         System.out.printf(                  "      (%d)%n", this.product);         this.product = -1;          //                            notify();                return p;     } } 

           :

WaitNotifyDemo.java 
package onlyfun.caterpillar; public class WaitNotifyDemo {    public static void main(String[] args) {        Clerk clerk = new Clerk();          Thread producerThread = new Thread(                new Producer(clerk));         Thread consumerThread = new Thread(                new Consumer(clerk));          producerThread.start();         consumerThread.start();     }} 

    :
          ......
         ......
      (1)
      (1)
      (2)
      (2)
      (3)
      (3)
      (4)
      (4)
      (5)
      (5)
      (6)
      (6)
      (7)
      (7)
      (8)
      (8)
      (9)
      (9)
      (10)
      (10)
 

      10   ,       10   ,             ,            ,           。

                ,         ,      InterruptedException    ,interrupt()           。

좋은 웹페이지 즐겨찾기