Condition give the effect of having multiple wait-sets per object?(条件会产生每个对象有多个等待集的效果吗?)
问题描述
我正在阅读 java.util.concurrent.locks.Condition
中的 Condition.
I am reading about Condition in java.util.concurrent.locks.Condition
.
条件因素将对象监视器方法(等待、通知和通知所有)>分解为不同的对象,以产生具有多个的效果每个对象的等待集,通过将它们与任意锁的使用结合起来实现.
Condition factors out the Object monitor methods (wait, notify and notifyAll) >into distinct objects to give the effect of having multiple wait-sets per object, by combining them with the use of arbitrary Lock implementations.
有人可以解释一下吗?
与普通同步块或方法相比,这有什么好处?
How this is a benefit over normal synchronization blocks or method?
推荐答案
一个锁可以关联多个条件.锁是一个对象",每个条件都是一个等待集".这允许独立条件共享临界区.例如,考虑有界生产者-消费者问题.解决它的一种方法是拥有一个保护队列的锁和两个独立的等待集:一个用于生产者,等待槽将项目放入队列,另一个用于等待项目获取的消费者.使用普通的旧 synchronized
和 wait/notify
API,我们能做的最好的事情就是遵循以下原则:
One Lock can be associated with many Conditions. Lock is an "object", each condition is a "waiting set". This allows for independent conditions sharing critical section. For example, consider bounded producers-consumers problem. One way to solve it is to have one lock that protects the queue, and two independent waiting sets: one for producers, waiting for slot to place item in the queue, other for consumers waiting for items to take. Using plain old synchronized
and wait/notify
API, best we can do is along these lines:
制作人:
producer:
synchronized (lock) {
while (queue.isFull()) {
lock.wait();
}
queue.put(sth);
lock.notify();
}
消费者:
consumer:
synchronized (lock) {
while (queue.isEmpty() {
lock.wait();
}
product = queue.take();
lock.notify();
}
这样做的缺点是在队列的每次更改时都会唤醒生产者和消费者,即使它不可能允许给定线程继续进行(例如,当其他消费者从队列).使用 Lock/Condition API,我们可以实现分离等待消费者和生产者的解决方案,从而减少冗余唤醒和检查:
This has the disadvantage of waking up both producers and consumers on every change to the queue, even if it cannot possibly allow given thread to proceed (e.g. consumer is awoken when some other consumer takes item from the queue). Using Lock/Condition API we can implement solution that separates waiting consumers and producers, and hence reduce redundant wakeups and checking:
Lock lock = new ReentrantLock();
Condition hasPlace = lock.newCondition();
Condition hasItems = lock.newCondition();
制作人:
producer:
lock.lock(); try { while (queue.isFull()) { hasPlace.await(); } queue.put(sth); hasItems.signal(); } finally { lock.unlock(); }
消费者:
consumer:
lock.lock(); try { while (queue.isEmpty()) { hasItems.await(); } product = queue.take(); hasPlace.signal(); } finally { lock.unlock(); }
这样,消费者等待生产者生产一些项目(hasItems 条件),并在从队列中删除项目时通知生产者有一个空槽(hasPlace 条件).这两个条件都与同一个临界区(锁定)相关联,因此我们保持了通常的排除和等待时释放锁的保证,同时获得了分离等待队列的能力.
This way, consumer waits for producers to produce some item (hasItems condition), and upon removing an item from the queue it notifies producers that there is an empty slot (hasPlace condition). Both conditions are associated with the same critical section (Lock), so we keep the usual exclusion and release-lock-while-waiting guarantees, while gaining the ability to separate waiting queues.
这篇关于条件会产生每个对象有多个等待集的效果吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:条件会产生每个对象有多个等待集的效果吗?
基础教程推荐
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 降序排序:Java Map 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01