当前位置:首页 > 开发 > 编程语言 > 消息中间件 > 正文

Wait和Notify的读书笔记

发表于: 2010-05-27   作者:asialee   来源:转载   浏览次数:
摘要: Wait和notify Just as each object has a lock that can be obtained and released, each object also provides a mechanism that allows it to be a waiting area. And just like the lock mechanism, the main re

Wait和notify
Just as each object has a lock that can be obtained and released, each object also provides a
mechanism that allows it to be a waiting area. And just like the lock mechanism, the main reason for this mechanism is to aid communication between threads.[1] The idea behind the mechanism is actually simple: one thread needs a certain condition to exist and assumes that another thread will create that condition. When this other thread creates the condition, it notifies the first thread that has been waiting for the condition. This is accomplished with the following methods:

What is the purpose of the wait and notify mechanism, and how does it work? The wait and notify mechanism is also a synchronization mechanism; however, it is more of a communication mechanism: it allows one thread to communicate to another thread that a particular condition has occurred. The wait and notify mechanism does not specify what the specific condition is.

Can wait and notify be used to replace the synchronized method? Actually, the answer is no. Wait
and notify does not solve the race condition problem that the synchronized mechanism solves. As a matter of fact, wait and notify must be used in conjunction with the synchronized lock to prevent a race condition in the wait and notify mechanism itself.

The wait() method releases the lock prior to waiting, and reacquires the lock prior to
returning from the wait() method. This is done so that no race condition exists. If you
recall, there is no concept of releasing and reacquiring a lock in the Java API. The wait()
method is actually tightly integrated with the synchronization lock, using a feature not
available directly from the synchronization mechanism. In other words, it is not possible for
us to implement the wait() method purely in Java: it is a native method.

What happens when notify() is called and there is no thread waiting?
Since the wait and notify mechanism does not know the condition about which it
is sending notification, it assumes that a notification for which there is no thread waiting is a
notification that goes unheard. In other words, if notify() is called without another thread waiting, then notify() simply returns.

What are the details of the race condition that exists in wait and notify? In general, a thread that uses the wait() method confirms that a condition does not exist (typically by checking a variable) and then calls the  wait() method. When another thread sets the condition (typically by setting that same variable), it then calls the notify() method. A race condition occurs when:
1. The first thread tests the condition and confirms that it must wait.
2. The second thread sets the condition.
3. The second thread calls the notify() method; this goes unheard, since the first thread is not
yet waiting.
4. The first thread calls the wait() method.

How does this potential race condition get resolved? This race condition is resolved by the
synchronization lock discussed earlier. In order to call wait() or notify(), we must have obtained
the lock for the object on which we're calling the wait() or notify() method. This is mandatory: the methods will not work properly and will generate an exception condition if the lock is not held. Furthermore, the wait() method also releases the lock prior to waiting and reacquires the lock prior to returning from the wait() method. The developer must use this lock to ensure that checking the condition and setting the condition is atomic, which typically means that the condition is held in an instance variable within the locked object.

What happens when there is more than one thread waiting for the notification? Which thread
actually gets the notification when notify() is called? The answer is that it depends: the Java
specification doesn't define which thread gets notified. Which thread actually receives the notification varies based on several factors, including the implementation of the Java virtual machine and scheduling and timing issues during the execution of the program. There is no way to determine, even on a single platform, which of multiple threads receives the notification.

Why would you want to wake up all of the threads? There are a few possible reasons, one of which is if there is more than one condition to wait for. Since we cannot control which thread gets the notification, it is entirely possible that a notification wakes up a thread that is waiting for an entirely different condition. By waking up all the waiting threads, we can design the program so that the threads decide among themselves which should execute next.

The difference in this case is that we are calling the notifyAll() method instead of the notify()
method. There are two reasons for this:
•  It is entirely possible for the system to wake up a thread that needs more resources than are
available, even with the resources that have just been freed. If we had used the  notify()
method, another thread that could be satisfied with the current amount of resources would
not get the chance to grab those resources because the system picked the wrong thread to
wake up.
•  It is possible to satisfy more than one thread with the number of resources we have just freed.
As an example, if we free ten resources, we can then let four other threads grab three, four,
one, and two resources, respectively. There is not a one-to-one ratio between the number of
threads freeing resources and the number of threads grabbing resources.


Wait和Notify的读书笔记

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
wait-notify机制 a. 当synchronized方法中的wait方法被调用时,当前线程将被中断运行,并且放弃该对
注意:这些方法属于Object,而不属于Thread。 每一个对象除了有一个锁之外,还有一个等待队列(wait
wait-notify机制 a. 当synchronized方法中的wait方法被调用时,当前线程将被中断运行,并且放弃该对
继续来看blog:http://blog.csdn.net/zyplus/article/details/6672775 “Obj.wait(),与Obj.notify(
Java 中除了关键字 synchronized 能够实现线程同步外,还可以使用 wait 和 notify/notify 实现同步
wait-notify用在经典的生产者-消费者模型。一般代码都是先初始化consumer,然后再初始化producer,
wait-notify用在经典的生产者-消费者模型。一般代码都是先初始化consumer,然后再初始化producer,
线程同步 前言:我们上一篇http://davidisok.iteye.com/blog/2105369讲了线程的基本介绍,这一篇主
wait-notify用在经典的生产者-消费者模型。一般代码都是先初始化consumer,然后再初始化producer,
wait-notify用在经典的生产者-消费者模型。一般代码都是先初始化consumer,然后再初始化producer,
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号