Object.notifyAll()

void java.lang. Object.notifyAll()

 

Causes all threads which are waiting on this object's monitor (by means of calling one of the wait() methods) to be woken up. The threads will not run immediately. The thread that called notify() has to release the object's monitor first. Also, the threads still have to compete against other threads that try to synchronize on the same object.

This method can only be invoked by a thread which owns this object's monitor. A thread becomes owner of an object's monitor

  • by executing a synchronized method of that object;
  • by executing the body of a synchronized statement that synchronizes on the object;
  • by executing a synchronized static method if the object is of type Class.
Throws:
IllegalMonitorStateException - if the thread calling this method is not the owner of this object's monitor.
See Also:
notify
wait()
wait(long)
wait(long, int)
java.lang.Thread
作用:唤醒所有在object对象上调用wait()方法的线程,线程不一定会立即执行。调用了notify()方法的线程会首先释放object锁,而且线程仍然需要和其他线程竞争来同步同一个对象。此方法只能唤醒拥有对象锁的线程。一个对象要拥有一个对象锁,可以通过下面三个方法:
1.  执行此对象的一个同步方法
2.  执行同步此对象的方法块
3.  如果对象是Class类型,执行一个同步静态方法
抛出异常: IllegalMonitorStateException    如果调用notifyAll()的线程不是锁的拥有者

转载于:https://www.cnblogs.com/luow/p/4484655.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值