Java中的wait()方法与notify(),notifyAll()方法
2016-03-31 16:40
399 查看
void java.lang.Object.wait() throws InterruptedException
这个方法是将当前线程进入等待池(不是线程池),同理notify(),notifyAll()是将线程从等待池中唤醒。
Causes the current thread to wait until another thread invokes the java.lang.Object.notify() method
or the java.lang.Object.notifyAll() method for this object. In other words, this method behaves exactly as if it simply performs the call wait(0).
The current thread must own this object's monitor.(使用了此方法的方法必须加上synchronized关键字)The
thread releases ownership of this monitor(说明wait()使拥有锁的线程被堵塞) and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method. The
thread then waits until it can re-obtain ownership of the monitor and resumes execution.
As in the one argument version, interrupts and spurious wakeups are possible, and this method should always be used in a loop:
Throws:
IllegalMonitorStateException - if the current thread is not the owner of the object's monitor.
InterruptedException - if any thread interrupted the current thread before or while the current thread was waiting for a notification. The interrupted
status of the current thread is cleared when this exception is thrown.
void java.lang.Object.notify()
Wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and occurs at
the discretion of the implementation. A thread waits on an object's monitor by calling one of the wait methods.
The awakened thread will not be able to proceed until the current thread relinquishes the lock on this object. The awakened thread will compete in the usual manner with any other threads
that might be actively competing to synchronize on this object; for example, the awakened thread enjoys no reliable privilege or disadvantage in being the next thread to lock this object.
This method should only be called by a thread that is the owner of this object's monitor. A thread becomes the owner of the object's
monitor in one of three ways:
- By executing a synchronized instance method of that object.
- By executing the body of a synchronized statement that synchronizes on the object.
- For objects of type Class, by executing a synchronized static method of that class.
Only one thread at a time can own an object's monitor.
Throws:
IllegalMonitorStateException - if the current thread is not the owner of this object's monitor.
void java.lang.Object.notifyAll()
Wakes up all threads that are waiting on this object's monitor(在消费者生产者问题中,说明唤醒了所有的生产者和消费者,因为锁是Store对象的). A thread waits on an
object's monitor by calling one of the wait methods.
The awakened threads will not be able to proceed until the current thread relinquishes the lock on this object. The awakened threads will compete in the usual manner with any other threads
that might be actively competing to synchronize on this object; for example, the awakened threads enjoy no reliable privilege or disadvantage in being the next thread to lock this object.
This method should only be called by a thread that is the owner of this object's monitor. See the notify method for a description of the ways
in which a thread can become the owner of a monitor.
Throws:
IllegalMonitorStateException - if the current thread is not the owner of this object's monitor.
这个方法是将当前线程进入等待池(不是线程池),同理notify(),notifyAll()是将线程从等待池中唤醒。
Causes the current thread to wait until another thread invokes the java.lang.Object.notify() method
or the java.lang.Object.notifyAll() method for this object. In other words, this method behaves exactly as if it simply performs the call wait(0).
The current thread must own this object's monitor.(使用了此方法的方法必须加上synchronized关键字)The
thread releases ownership of this monitor(说明wait()使拥有锁的线程被堵塞) and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method. The
thread then waits until it can re-obtain ownership of the monitor and resumes execution.
As in the one argument version, interrupts and spurious wakeups are possible, and this method should always be used in a loop:
synchronized (obj) { while (<condition does not hold>) obj.wait(); ... // Perform action appropriate to condition } This method should only be called by a thread that is the owner of this object's monitor.
See the notify method for a description of the ways in which a thread can become the owner of a monitor.
Throws:
IllegalMonitorStateException - if the current thread is not the owner of the object's monitor.
InterruptedException - if any thread interrupted the current thread before or while the current thread was waiting for a notification. The interrupted
status of the current thread is cleared when this exception is thrown.
void java.lang.Object.notify()
Wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and occurs at
the discretion of the implementation. A thread waits on an object's monitor by calling one of the wait methods.
The awakened thread will not be able to proceed until the current thread relinquishes the lock on this object. The awakened thread will compete in the usual manner with any other threads
that might be actively competing to synchronize on this object; for example, the awakened thread enjoys no reliable privilege or disadvantage in being the next thread to lock this object.
This method should only be called by a thread that is the owner of this object's monitor. A thread becomes the owner of the object's
monitor in one of three ways:
- By executing a synchronized instance method of that object.
- By executing the body of a synchronized statement that synchronizes on the object.
- For objects of type Class, by executing a synchronized static method of that class.
Only one thread at a time can own an object's monitor.
Throws:
IllegalMonitorStateException - if the current thread is not the owner of this object's monitor.
void java.lang.Object.notifyAll()
Wakes up all threads that are waiting on this object's monitor(在消费者生产者问题中,说明唤醒了所有的生产者和消费者,因为锁是Store对象的). A thread waits on an
object's monitor by calling one of the wait methods.
The awakened threads will not be able to proceed until the current thread relinquishes the lock on this object. The awakened threads will compete in the usual manner with any other threads
that might be actively competing to synchronize on this object; for example, the awakened threads enjoy no reliable privilege or disadvantage in being the next thread to lock this object.
This method should only be called by a thread that is the owner of this object's monitor. See the notify method for a description of the ways
in which a thread can become the owner of a monitor.
Throws:
IllegalMonitorStateException - if the current thread is not the owner of this object's monitor.
相关文章推荐
- Web必知必会Cookie与Session
- Java多线程模拟实现消费者生产者问题
- Java的动态代理
- [Java]数据存储(栈,堆,常量池)
- 【eclipse】No enclosing instance of type A is accessible. Must qualify the allocation with an enclosing instance of type A
- SpringWeb ContextLoaderListener 初始化过程
- 认识java中的堆和栈
- SpringMVC DispatcherServlet 初始化过程
- java后台通过url获取返回数据
- java#2servlet
- Java 四种线程池的用法分析
- Struts2 传递参数map对象
- 华为上机题-二师弟的星际加法
- JavaEE_3_30_servlet详细介绍
- Swagger与SpringMVC项目整合
- maven eclipse 安装
- java传递String参数
- Spring IoC
- 用Java语言打印九九乘法口诀表
- spring+ibatis事务配置