java notify 用法_java wait notify的用法

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:

packageProductAndConsume;

importjava.util.List;

publicclassConsumeimplementsRunnable{

privateList container =null;

privateintcount;

publicConsume(List lst){

this.container = lst;

}

publicvoidrun() {

while(true){

synchronized(container) {

if(container.size()==0){

try{

container.wait();//放弃锁

} catch(InterruptedException e) {

e.printStackTrace();

}

}

try{

Thread.sleep(100);

} catch(InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

container.remove(0);

container.notify();

System.out.println("我吃了"+(++count)+"个");

}

}

}

}

packageProductAndConsume;

importjava.util.List;

publicclassProductimplementsRunnable {

privateList container =null;

privateintcount;

publicProduct(List lst) {

this.container = lst;

}

publicvoidrun() {

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(newObject());

container.notify();

System.out.println("我消费了"+(++count)+"个");

}

}

}

}

packageProductAndConsume;

importjava.util.List;

publicclassProductimplementsRunnable {

privateList container =null;

privateintcount;

publicProduct(List lst) {

this.container = lst;

}

publicvoidrun() {

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(newObject());

container.notify();

System.out.println("我消费了"+(++count)+"个");

}

}

}

}

packageProductAndConsume;

importjava.util.ArrayList;

importjava.util.List;

publicclassMultiThread {

privateList container =newArrayList();

publicfinalstaticintMAX =5;

publicstaticvoidmain(String args[]){

MultiThread m = newMultiThread();

newThread(newConsume(m.getContainer())).start();

newThread(newProduct(m.getContainer())).start();

newThread(newConsume(m.getContainer())).start();

newThread(newProduct(m.getContainer())).start();

}

publicList getContainer() {

returncontainer;

}

publicvoidsetContainer(List container) {

this.container = container;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值