java多线程之 wait(),notify(),notifyAll()

wait(),notify(),notifyAll()不属于Thread类,而是属于Object基础类,也就是说每个对像都有wait(),notify(),notifyAll()的功能.因为都个对像都有锁,锁是每个对像的基础,当然操作锁的方法也是最基础了。
   wait导致当前的线程等待,直到其他线程调用此对象的 notify() 方法或 notifyAll() 方法,或被其他线程中断。wait只能由持有对像锁的线程来调用。
 notify唤醒在此对象监视器上等待的单个线程。如果所有线程都在此对象上等待,则会选择唤醒其中一个线程(随机)。直到当前的线程放弃此 对象上的锁,才能继续执行被唤醒的线程。同Wait方法一样,notify只能由持有对像锁的线程来调用.notifyall也一样,不同的是 notifyall会唤配所有在此对象锁上等待的线程。
"只能由持有对像锁的线程来调用"说明wait方法与notify方法必须在同步块内执行,即synchronized(obj)之内.再者synchronized代码块内没有锁是寸步不行的,所以线程要继续执行必须获得锁。相辅相成。
看一个很经典的例子(生产者与消费者):

package ProductAndConsume;
import java.util.List;

public class Consume implements Runnable{
         private List container = null ;
         private int count;
         public Consume(List lst){
             this .container = lst;
}
public void run() {
    
     while ( true ){
     synchronized (container) {
         if (container.size()== 0){
         try {
            container.wait(); //容器为空,放弃锁,等待生产
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
        }
         try {
         Thread.sleep(100);
        } catch (InterruptedException e) {
         e.printStackTrace();
        }
        container.remove(0);
        container.notify();
        System.out.println( "我吃了" +(++count)+ "个" );
     }
    }
    
}

}

package ProductAndConsume;
import java.util.List;

public class Product implements Runnable {
private List container = null ;
         private int count;
public Product(List lst) {
     this .container = lst;
}

public void run() {
     while ( true ) {
     synchronized (container) {
         if (container.size() > MultiThread.MAX) {
         //如果容器超过了最大值,就不要在生产了,等待消费
         try {
            container.wait();
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
        }
         try {
         Thread.sleep(100);
        } catch (InterruptedException e) {
         e.printStackTrace();
        }
        container.add( new Object());
        container.notify();
        System.out.println( "我生产了" +(++count)+ "个" );
     }
    }

}

}

package ProductAndConsume;
import java.util.ArrayList;
import java.util.List;

public class MultiThread {
private List container = new ArrayList();
public final static int MAX = 5;
public static void main(String args[]){

MultiThread m = new MultiThread();

     new Thread( new Consume(m.getContainer())).start();
     new Thread( new Product(m.getContainer())).start();
     new Thread( new Consume(m.getContainer())).start();
     new Thread( new Product(m.getContainer())).start();
}
public List getContainer() {
     return container;
}

public void setContainer(List container) {
     this .container = container;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值