本文介绍了signalAll在条件接口对对象的notifyAll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1)昨天只有我问这个问题

2)我想编辑相同的内容,并在我的问题中添加几个ifs,但是因为它可能变得繁琐,包含足够的文本,



3)对于我的帖子的上下文,其url在第1点给出),考虑4个线程,P1,T1和P2,T2作用于单个数据结构S。



4)我试图再次利用Condition interface over wait notify的优点。

5)考虑代码

  final Lock lock = new ReentrantLock 
条件c1 = lock.newCondition();
条件c2 = lock.newCondition();
条件c3 = lock.newCondition();
条件c4 = lock.newCondition(); 6)考虑P1,T1使用c1,c2(在标准的await()/ signalAll中) ()方式)。考虑P2,T2使用c3,c4(在标准的await()/ signalAll()方式)我们分别说put,take,put1,take1方法。



7)当我做c1.signalAll(),将只有等待/由于condition1 的线程将接收信号。我有意义吗?



8)考虑一个等待/通知机制来实现相同的说法,

  private static final Object lock = new Object(); 
同步(锁定)

考虑put,take,put1,take1,在任何一个条件实现上执行lock.notifyAll(),甚至由于其他条件等待/打开的线程将接收通知。真的吗 ?。这是我们可以算作使用wait / notify over Condition机制的缺点吗?

解决方案

条件类是内在条件队列的概括(通过 Object.wait Object.notify Object.notifyAll )。



Brian Goetz的Java并发实践[p.306-307]


1) Yesterday only I asked this question Condition vs wait notify mechanism

2) I wanted to edit the same and add a few ifs to my question, but because it could have become cumbersome and contained enough text to disinterest and baffle the reader, I thought of asking a new question here.

3) With context of my post whose url is given in point number 1), consider a case of 4 threads, P1,T1 and P2,T2 acting on a single data structure 'S'.

4) I am trying to again draw the advantages of using Condition interface over wait notify.

5) Consider the code

final Lock lock = new ReentrantLock();
Condition c1 = lock.newCondition();
Condition c2 = lock.newCondition();
Condition c3 = lock.newCondition();
Condition c4 = lock.newCondition();

6) Consider P1,T1 making use of c1,c2 (in a standard await()/signalAll() manner). Consider P2,T2 making use of c3,c4 (in a standard await()/signalAll() manner) in let's say put,take,put1,take1 methods respectively.

7) When I do c1.signalAll(), will only the threads waiting on/because of condition1 will recieve signal. Do I make sense ?

8) Consider a wait/notify mechanism to implement the same say,

private static final Object lock= new Object();
synchronized(lock)

Consider put,take,put1,take1, so if any thread does a lock.notifyAll() on any one of the condition fulfillment, even the threads waiting for/on because of other conditions will recieve a notification. Is that true ?. Is that something we can count as a disadvantage of using wait/notify over Condition mechanism ?

解决方案

Yes, you are right. The Condition class is a generalization of intrinsic condition queues (the ones which are controlled through Object.wait, Object.notify and Object.notifyAll).

I will quote Brian Goetz`s Java Concurrency in Practice [p.306-307]

这篇关于signalAll在条件接口对对象的notifyAll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 18:31