`
- 浏览:
1238227 次
- 性别:
- 来自:
北京
-
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.
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
- 线程同步机制包括synchronized关键字、wait()、notify()和notifyAll()方法,以及Lock接口和相关的类。 9. **反射**: - 反射允许程序在运行时动态获取类的信息,并能创建对象、调用方法和访问字段。 10. **...
此外,多线程编程也是Java的一大亮点,笔记会介绍线程的创建与同步机制,如synchronized关键字和wait/notify机制。 文件I/O操作是任何编程语言都不可或缺的部分,Java也不例外。笔记会讲解如何在Java中读写文件,...
- `wait()`和`notify()`/`notifyAll()`方法用于实现线程间的同步和唤醒机制,是更安全的线程控制方式。 - 线程在等待IO操作完成时也会进入等待状态。 - 当多个线程试图访问同一个资源时,可能会发生死锁现象。 2...
- Java提供了Thread类和Runnable接口来创建和管理线程,线程同步是多线程编程中的重要概念,包括synchronized关键字、wait()和notify()方法等。 7. **反射机制** - 反射允许程序在运行时动态获取类的信息并操作类...
- 线程常用操作方法笔记:讲解了线程的启动、同步、通信以及线程间的协作方法,如wait、notify和join等。 - 线程操作范例笔记:通过实例展示了如何在Java中实现和管理线程,包括守护线程、线程池等高级用法。 五...
【Java学习笔记Markdown版】是针对Java初学者和进阶者的一份详尽教程,以Markdown格式编写,便于阅读和整理。Markdown是一种轻量级的标记语言,它允许用户使用易读易写的纯文本格式编写文档,然后转换成结构化的HTML...
通过阅读和理解这些笔记,你将能够掌握Java的基本语法、核心概念以及高级特性,为进一步学习Java的框架和技术打下坚实的基础。同时,笔记采用Markdown格式,方便转换为PDF、Word等其他格式,适应不同的阅读和学习...
Java编程语言是目前全球最流行的编程语言之一,尤其在企业级应用开发中占据主导地位。...通过系统的阅读和实践,初学者能够建立起坚实的Java编程基础,为今后的深入学习和实际项目开发打下坚实的基础。
笔记会解释如何创建和管理线程,以及同步机制如synchronized关键字和wait/notify机制。 IO流和NIO(非阻塞I/O)是Java处理输入输出的重要工具。笔记会讲述流的分类、读写操作、文件操作,以及NIO的新特性,如通道、...
笔记将介绍线程的创建方式、同步机制(如synchronized关键字、wait/notify、Lock接口)、线程池的使用等。 6. **网络编程**:Java提供Socket编程接口,可用于实现客户端/服务器模型的网络通信。笔记会讲解TCP和UDP...
5. **多线程**:Java内置了强大的多线程支持,笔记会讲述如何创建和管理线程,以及同步机制如synchronized关键字和wait/notify方法,帮助开发者理解和实现并发程序。 6. **网络编程**:Java提供了丰富的网络编程API...
笔记会涵盖Thread类、Runnable接口、synchronized关键字和wait/notify机制,帮助读者理解和实现高效的并发程序。 【反射与注解】 反射是Java动态访问类、接口和对象的能力,注解则是一种元数据,用于向编译器或...
笔记中可能会介绍Thread类的创建与管理,同步控制(synchronized关键字、wait/notify机制、Lock接口)、线程池(ExecutorService、ThreadPoolExecutor)等知识点。 3. **序列化**: Java序列化是将对象转换为字节...
笔记中详细阐述了线程的创建方式,包括Thread类和Runnable接口,以及同步机制,如synchronized关键字、wait/notify机制和锁对象。同时,也讨论了线程池的概念和ExecutorService的使用。 七、网络编程 Java的Socket...
5. **多线程**:Java支持多线程编程,笔记会解释如何创建和管理线程,包括同步机制(synchronized关键字、wait/notify、Lock接口)和并发工具类。 6. **反射与注解**:反射允许程序在运行时检查和操作类、接口、...
【标题】"蜗牛学院-第三阶段笔记.zip" 暗示这是一份来自蜗牛学院的关于其培训课程第三阶段的学习笔记。...通过详细阅读和理解这些笔记,学员能更好地理解和运用Java编程语言,提升自己的编程技能。
11. **多线程**:线程的创建方式(实现Runnable接口和继承Thread类),线程同步机制(synchronized关键字、wait/notify机制)。 12. **文件与目录操作**:创建、删除、读写文件,以及目录的管理。 13. **枚举与...
7. **多线程**:讲解并发编程的基础,包括线程的创建、同步机制(synchronized、wait、notify等)、线程池和死锁问题。 8. **反射机制**:通过反射来动态访问类的信息,创建对象,调用方法,以及处理泛型。 9. **...
笔记会解释线程的创建、同步和通信,包括synchronized关键字、wait/notify机制、ThreadLocal等。 7. **反射与注解**:反射允许在运行时检查类、接口、字段和方法的信息,而注解为代码提供元数据。这两部分都是Java...