wait(),notify(),notifyAll()不属于Thread类,而是属于Object基本类,也就是说每个对像都有wait(),notify(),notifyAll()的功能.由于都个对像都有锁,锁是每个对像的基本 ,当然操作锁的要领也是最基本了.

  先看java doc如何说:

  wait导致当前的线程等待,直到其他线程调用此对象的 notify() 要领或 notifyAll() 要领。当前的线程必须拥有此对象监视器。该线程揭晓对此监视器的一切权并等待,直到其他线程议决调用 notify 要领,或 notifyAll 要领告诉在此对象的监视器上等待的线程醒来。然后该线程将等到重新获得 对监视器的一切权后才能继续执行.

  notify唤醒在此对象监视器上等待的单个线程。假如一切线程都在此对象上等待,则会挑选唤醒其中一个线程。直到当前的线程放弃此对象上的锁定,才能继续执行被唤醒的线程。此要领只应由作为此对象监视器的一切者的线程来调用.

  "当前的线程必须拥有此对象监视器"与"此要领只应由作为此对象监视器的一切者的线程来调用"表明 wait要领与notify要领必须在同步块内执行,即synchronized(obj之内).

  调用对像wait要领后,当火线程释放对像锁,进入等待形状 .直到其他线程(也只好是其他线程)议决 notify 要领,或 notifyAll.该线程重新获得 对像锁.

  继续执行,记得线程必须重新获得 对像锁才能继续执行.由于 synchronized代码块内没有锁是寸步无法走的.看一个很经典的例子:

  Code:  

  1. package ProductAndConsume; 
  2.  
  3.   import java.util.List; 
  4.  
  5.   public class Consume implements Runnable{ 
  6.  
  7.   private List container = null
  8.  
  9.   private int count; 
  10.  
  11.   public Consume(List lst){ 
  12.  
  13.   this.container = lst; 
  14.  
  15.   } 
  16.  
  17.   public void run() { 
  18.  
  19.   while(true){ 
  20.  
  21.   synchronized (container) { 
  22.  
  23.   if(container.size()== 0){ 
  24.  
  25.   try { 
  26.  
  27.   container.wait();//放弃锁 
  28.  
  29.   } catch (InterruptedException e) { 
  30.  
  31.   e.printStackTrace(); 
  32.  
  33.   } 
  34.  
  35.   } 
  36.  
  37.   try { 
  38.  
  39.   Thread.sleep(100); 
  40.  
  41.   } catch (InterruptedException e) { 
  42.  
  43.   // TODO Auto-generated catch block 
  44.  
  45.   e.printStackTrace(); 
  46.  
  47.   } 
  48.  
  49.   container.remove(0); 
  50.  
  51.   container.notify(); 
  52.  
  53.   System.out.println("我吃了"+(++count)+"个"); 
  54.  
  55.  
  56.   
  57.  
  58.   } 
  59.  
  60.   } 
  61.  
  62.   } 
  63.  
  64.   package ProductAndConsume; 
  65.  
  66.   import java.util.List; 
  67.  
  68.   public class Product implements Runnable { 
  69.  
  70.   private List container = null
  71.  
  72.   private int count; 
  73.  
  74.   public Product(List lst) { 
  75.  
  76.   this.container = lst; 
  77.  
  78.   } 
  79.  
  80.   public void run() { 
  81.  
  82.   while (true) { 
  83.  
  84.   synchronized (container) { 
  85.  
  86.   if (container.size() > MultiThread.MAX) { 
  87.  
  88.   try { 
  89.  
  90.   container.wait(); 
  91.  
  92.   } catch (InterruptedException e) { 
  93.  
  94.   e.printStackTrace(); 
  95.  
  96.   } 
  97.  
  98.   } 
  99.  
  100.   try { 
  101.  
  102.   Thread.sleep(100); 
  103.  
  104.   } catch (InterruptedException e) { 
  105.  
  106.   e.printStackTrace(); 
  107.  
  108.   } 
  109.  
  110.   container.add(new Object()); 
  111.  
  112.   container.notify(); 
  113.  
  114.   System.out.println("我消费了"+(++count)+"个"); 
  115.  
  116.  } 
  117.  
  118.   
  119.  
  120.   } 
  121.  
  122.   } 
  123.  
  124.   } 
  125.  
  126.   package ProductAndConsume; 
  127.  
  128.   import java.util.List; 
  129.  
  130.   public class Product implements Runnable { 
  131.  
  132.   private List container = null
  133.  
  134.   private int count; 
  135.  
  136.   public Product(List lst) { 
  137.  
  138.   this.container = lst; 
  139.  
  140.   } 
  141.  
  142.   public void run() { 
  143.  
  144.   while (true) { 
  145.  
  146.   synchronized (container) { 
  147.  
  148.   if (container.size() > MultiThread.MAX) { 
  149.  
  150.   try { 
  151.  
  152.   container.wait(); 
  153.  
  154.   } catch (InterruptedException e) { 
  155.  
  156.   e.printStackTrace(); 
  157.  
  158.   } 
  159.  
  160.   } 
  161.  
  162.   try { 
  163.  
  164.   Thread.sleep(100); 
  165.  
  166.   } catch (InterruptedException e) { 
  167.  
  168.   e.printStackTrace(); 
  169.  
  170.   } 
  171.  
  172.   container.add(new Object()); 
  173.  
  174.   container.notify(); 
  175.  
  176.   System.out.println("我消费了"+(++count)+"个"); 
  177.  
  178.   } 
  179.  
  180.   } 
  181.  
  182.   } 
  183.  
  184.   } 
  185.  
  186.   package ProductAndConsume; 
  187.  
  188.   import java.util.ArrayList; 
  189.  
  190.   import java.util.List; 
  191.  
  192.   public class MultiThread { 
  193.  
  194.   private List container = new ArrayList(); 
  195.  
  196.   public final static int MAX = 5
  197.  
  198.   public static void main(String args[]){ 
  199.  
  200.   MultiThread m = new MultiThread(); 
  201.  
  202.   new Thread(new Consume(m.getContainer())).start(); 
  203.  
  204.   new Thread(new Product(m.getContainer())).start(); 
  205.  
  206.   new Thread(new Consume(m.getContainer())).start(); 
  207.  
  208.   new Thread(new Product(m.getContainer())).start(); 
  209.  
  210.   } 
  211.  
  212.   public List getContainer() { 
  213.  
  214.   return container; 
  215.  
  216.   } 
  217.  
  218.   public void setContainer(List container) { 
  219.  
  220.   this.container = container; 
  221.