一个很好的小例子来演示java中的wait()和notify()方法
任何人都可以给我提供一个很好的小例子来演示java中的wait()和notify()功能。我已经尝试过下面的代码,但它没有显示我所期望的。
public class WaitDemo {
int i = 10;
int display() {
System.out.println("Lexmark");
i++;
return i;
}
}
public class ClassDemo1 extends Thread {
private WaitDemo wd = new WaitDemo();
public static void main(String[] args) {
ClassDemo1 cd1 = new ClassDemo1();
ClassDemo1 cd2 = new ClassDemo1();
cd1.setName("Europe");
cd2.setName("America");
cd1.start();
cd2.start();
}
synchronized void display() {
System.out.println("Hello");
notifyAll();
}
public void run() {
synchronized (this) {
try {
{
notify();
System.out.println("The thread is " + currentThread().getName());
wait();
System.out.println("The value is " + wd.display());
}
} catch (InterruptedException e) {
}
}
}
}
问题是 WaitDemo 类中的方法没有被执行,根据我的想法, wait() 之后的 SOP 应该执行。请帮我解决这个问题。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
下面是 wait & 的示例在 Object 类中进行通知。客户试图提取价值 2000 的资金,但账户上只有 1000,因此必须等待存款。一旦存款完成,客户就可以提取金额。在存款完成之前,客户将等待。
Below is an example of wait & notify in the Object class. The customer is trying to withdraw money of value 2000 but the account is having only 1000 so it has to wait for the deposit. Once the deposit is made, then the customer will be able to withdraw the amount. Until the deposit is made, the customer will be waiting.
您的
try
块中有两层大括号{
。如果删除内部集(它似乎没有执行任何操作),是否可以解决问题?周围有几个示例,所有这些示例都演示了其用法。最后一个链接是一组可以帮助您的结果。如果您需要更具体的事情,请让我知道您的应用程序正在尝试做什么,我可以尝试找到更适合您情况的示例。
You've got two levels of braces
{
in yourtry
block. If you remove the inner set (which doesn't appear to do anything), does that fix the problem?There are several examples around, all of which demonstrate the use. The last link is a set of results that can help you out. If you need more specific things, let me know what it is that your app is trying to do, and I can try to find examples that are more specific to your situation.
我创建了两个线程,一个用于打印奇数 (OddThread),另一个用于打印偶数 (EvenThread)。在每个线程的 run 方法中,我使用 Print 类的共享对象分别为 Odd 和 EvenThread 调用 printOdd() 和 printEven()。我将 Print 的共享对象设为静态,以便只制作一份副本。现在在 Print 对象上同步,我使用了一个布尔标志,这样当奇数线程打印奇数时,它将被发送到等待状态,同时通知偶数线程执行。逻辑的编写方式是奇数线程无论如何都会先打印奇数,因为标志设置为 false 最初会阻止偶数线程执行并将其发送到等待状态。
I created two threads one for printing odd numbers (OddThread) and another for even numbers (EvenThread). Inside the run method of each of the threads I used the shared object of class Print to call printOdd() and printEven() for the Odd and EvenThread respectively. I made the shared object of Print static so that only one copy is made. Now synchronizing on the Print object I used a Boolean flag such that when the odd thread printed an odd number it will be sent into the waiting state and the at the same time notifying the even thread to execute. The logic is written in such a way that the odd thread will always print the odd number first no matter what, as the flag is set to false initially preventing the even thread to execute and sending it to a waiting state.
wait方法的作用是,当某个线程通过锁定某个对象(我们称该对象为“a”)来执行同步块时,然后在该同步块内,当线程执行对象“a”的wait方法时,如下
所示对象将被释放,并且线程必须进入等待状态,直到它从该状态释放。
现在另一个线程可以使用该对象,因为它是一个释放对象。因此,如果另一个线程锁定了该对象,并且执行了该对象的通知方法,例如
,那么由对象“a”进入等待状态的线程之一可以从等待状态释放。否则,当调用notifyAll时,所有线程对象将从该状态释放。
What wait method does is , when some thread executed a synchronized block by locking some object (we call that object is "a") , then inside that synchronized block when the thread executed the wait method of object "a" like this
Then the a object will release and the thread has to go to the wait state until it has been release from that state.
and anothet thread now can use the a object beacause its a release object. so if another thread locked that object and it executed the notify method from that object like
Then one of a thread of the threads that went to wait state by object "a" can be released from the wait state. Other wise when call the notifyAll then the all the thread objects will release from that state.
您的问题是您正在创建 Thread 类的两个实例。因此,当调用 wait() 时,它位于两个不同的实例上,这两个实例都没有另一个线程争夺监视器,也没有另一个线程调用 notificationAll() 将线程从等待状态唤醒。
因此,您启动的每个线程将永远等待(或直到因其他原因而中断)。
您希望有多个线程访问同一监视器,因此首先尝试编写一些代码,其中所讨论的代码实际上不是线程,而只是被线程使用。
@normalocity 已经提供了多个示例的链接。
Your problem is that you are creating two instances of the Thread class. Thus when the wait() is called, it is on two different instances, neither of which has another thread that is in contention for your monitor, nor is there another thread to call notifyAll() to wake the thread from its wait state.
Thus each thread you have started will wait forever (or until interrupted for some other reason).
You want to have multiple threads accessing the same monitor, so start by trying to code something in which the code in question is not actually a thread, but is simply being used by a thread.
@normalocity has already provided links to multiple examples.
我刚刚更新了此答案以包含SCCE。
工作人员在 WorkerPauseManager 上调用pauseIfNeeded。如果管理器在工作线程调用pauseIfNeeded()时暂停,我们将调用wait(),它告诉调用线程等待,直到另一个线程对正在等待的对象调用notify()或notifyAll()。当 Swing 事件调度线程调用管理器上的 play() 时,就会发生这种情况,而管理器又调用 notificationAll()。
请注意,您必须在调用 wait() 或 notification() 的对象上拥有同步锁。由于 WorkerPauseManager 中的方法是同步的,因此所有同步方法都会获得 WorkerPauseManager 本身的同步锁。
I just updated this answer to include an SCCE.
The workers call pauseIfNeeded on the WorkerPauseManager. If the manager is paused when the worker thread calls pauseIfNeeded(), we call wait(), which tells the calling thread to wait until a different thread calls notify() or notifyAll() on the object being waited on. This happens when the Swing Event Dispatch Thread calls play() on the manager, which in turn calls notifyAll().
Note that you must have a synchronized lock on the object you are calling wait() or notify() on. Since the methods in WorkerPauseManager are synchronized, all the synchronized methods are getting a synchronized lock on the WorkerPauseManager itself.