如何在非线程的对象上调用wait()和notify()方法? [英] How can the wait() and notify() methods be called on Objects that are not threads?

查看:157
本文介绍了如何在非线程的对象上调用wait()和notify()方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不是线程的对象上调用 wait() notify()方法?这是不是真的有意义,是吗?

How can the wait() and notify() methods be called on Objects that are not Threads? That doesn't really make sense, does it?

当然,它必须有意义,因为这两种方法可用于所有Java对象。有人可以提供解释吗?我无法理解如何使用 wait() notify()进行线程之间的通信。

Surely, it must make sense, however, because the two methods are available for all Java objects. Can someone provide an explanation? I am having trouble understanding how to communicate between threads using wait() and notify().

推荐答案

锁与线程不同。锁定在受保护的数据结构上。线程是访问数据结构的东西。锁定在数据结构对象上,以防止线程以不安全的方式访问数据结构。

Locks are different from threads. The lock is on the data structure being protected. The threads are the things accessing the data structure. The locks are on the data structure object in order to keep the threads from accessing the data structure in an unsafe way.

任何对象都可以用作内部锁(意味着与 synchronized 一起使用)。这样,您可以通过将synchronized修饰符添加到访问共享数据的方法来保护对任何对象的访问。 (这不是一个好主意,因为它允许任何可以访问该对象的线程获取其锁,即使它没有调用它上面的任何方法;最好将锁保持为数据结构的私有成员被锁定,所以访问它是有限的。)

Any object can be used as an intrinsic lock (meaning used in conjunction with synchronized). This way you can guard access to any object by adding the synchronized modifier to the methods that access the shared data. (Not that it's a good idea, because that allows any thread that can access the object to acquire its lock, even if it's not calling any methods on it; it's better to keep the lock as a private member of the data structure being locked, so that access to it is limited.)

等待通知在被用作锁的对象上调用。锁是一个共享通信点:

wait and notify are called on objects that are being used as locks. The lock is a shared communication point:


  • 当一个有锁的线程调用 notifyAll 上,等待同一个锁的其他线程得到通知。当一个具有锁的线程调用通知时,其中一个等待同一个锁的线程会收到通知。

  • When a thread that has a lock calls notifyAll on it, the other threads waiting on that same lock get notified. When a thread that has a lock calls notify on it, one of the threads waiting on that same lock gets notified.

当一个有锁的线程在其上调用等待时,该线程会释放锁并继续休眠直到a)它收到通知,或b)它只是任意唤醒(虚假的唤醒);由于这两个原因之一,等待线程仍然停留在调用等待直到它被唤醒,然后线程必须重新获取锁定才能退出等待方法。

When a thread that has a lock calls wait on it, the thread releases the lock and goes dormant until either a) it receives a notification, or b) it just wakes up arbitrarily (the "spurious wakeup"); the waiting thread remains stuck in the call to wait until it wakes up due to one of these 2 reasons, then the thread has to re-acquire the lock before it can exit the wait method.

参见关于保护块的Oracle教程,Drop类是共享数据结构,使用Producer和Consumer runnables的线程正在访问它。锁定Drop对象控制线程如何访问Drop对象的数据。

See the Oracle tutorial on guarded blocks, the Drop class is the shared data structure, threads using the Producer and Consumer runnables are accessing it. Locking on the Drop object controls how the threads access the Drop object's data.

线程被用作JVM实现中的锁,建议应用程序开发人员避免使用线程作为锁。例如,针对Thread的文档.join 说:

Threads get used as locks in the JVM implementation, application developers are advised to avoid using threads as locks. For instance, the documentation for Thread.join says:


此实现使用this.wait调用的循环以this.isAlive为条件。当一个线程终止时,将调用this.notifyAll方法。建议应用程序不要在Thread实例上使用wait,notify或notifyAll。

This implementation uses a loop of this.wait calls conditioned on this.isAlive. As a thread terminates the this.notifyAll method is invoked. It is recommended that applications not use wait, notify, or notifyAll on Thread instances.

这篇关于如何在非线程的对象上调用wait()和notify()方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆