现在的位置: 首页 > 综合 > 正文

synchronized,wait 与 notify

2012年09月29日 ⁄ 综合 ⁄ 共 2423字 ⁄ 字号 评论关闭

wait(),notify(),notifyAll()不属于Thread类,而是属于Object基础类,也就是说每个对像都有wait(),notify(),notifyAll() 

的功能.因为都个对像都有锁,锁是每个对像的基础,当然操作锁的方法也是最基础了.

 

wait,它可以让当前线程暂时放弃对象锁,而将它暂时让给其它需要对象锁的人。当前的线程之前必须拥有此对象锁调用对像wait方法后,当前线程释放对像锁,进入等待状态.直到其他线程(也只能是其他线程)通过notify唤醒. 但唤醒后还不一定拥有该对象锁,必须等到其他线程释放对象锁(如调用wait方法)重新获得对象锁的所有权后才能继续执行.

 

 


wait方法与notify方法必须在同步块内执行,即synchronized(obj之内).

  看一个很经典的例子:

 

  1. package ProductAndConsume;  
  2.   
  3. import java.util.List;  
  4.   
  5. public class Consume implements Runnable{  
  6.     private List container = null;  
  7.     private int count;  
  8.     public Consume(List lst){  
  9.      this.container = lst;  
  10.     }  
  11.  public void run() {  
  12.     
  13.   while(true){  
  14.    synchronized (container) {  
  15.     if(container.size()== 0){  
  16.      try {  
  17.       container.wait();//放弃锁  
  18.      } catch (InterruptedException e) {  
  19.       e.printStackTrace();  
  20.      }  
  21.     }  
  22.     try {  
  23.      Thread.sleep(100);  
  24.     } catch (InterruptedException e) {  
  25.      // TODO Auto-generated catch block  
  26.      e.printStackTrace();  
  27.     }  
  28.     container.remove(0);  
  29.     container.notify();  
  30.     System.out.println("我吃了"+(++count)+"个");  
  31.    }  
  32.   }  
  33.     
  34.  }  
  35.   
  36. }  
  37.   
  38. package ProductAndConsume;  
  39.   
  40. import java.util.List;  
  41.   
  42. public class Product implements Runnable {  
  43.  private List container = null;  
  44.     private int count;  
  45.  public Product(List lst) {  
  46.   this.container = lst;  
  47.  }  
  48.   
  49.  public void run() {  
  50.   while (true) {  
  51.    synchronized (container) {  
  52.     if (container.size() > MultiThread.MAX) {  
  53.      try {  
  54.       container.wait();  
  55.      } catch (InterruptedException e) {  
  56.       e.printStackTrace();  
  57.      }  
  58.     }  
  59.     try {  
  60.      Thread.sleep(100);  
  61.     } catch (InterruptedException e) {  
  62.      e.printStackTrace();  
  63.     }  
  64.     container.add(new Object());  
  65.     container.notify();  
  66.     System.out.println("我生产了"+(++count)+"个");  
  67.    }  
  68.   }  
  69.   
  70.  }  
  71.   
  72. }  
  73.   
  74. package ProductAndConsume;  
  75.   
  76. import java.util.ArrayList;  
  77. import java.util.List;  
  78.   
  79. public class MultiThread {  
  80.  private List container = new ArrayList();  
  81.  public final static int MAX = 5;  
  82.   
  83.   
  84.   public static void main(String args[]){  
  85.   
  86.  MultiThread m = new MultiThread();  
  87.   
  88.   new Thread(new Consume(m.getContainer())).start();  
  89.   new Thread(new Product(m.getContainer())).start();  
  90.   new Thread(new Consume(m.getContainer())).start();  
  91.   new Thread(new Product(m.getContainer())).start();  
  92.  }  
  93.  public List getContainer() {  
  94.   return container;  
  95.  }  
  96.   
  97.  public void setContainer(List container) {  
  98.   this.container = container;  
  99.  }  

 

抱歉!评论已关闭.