- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如果条件
本身可以单独使用,并且创建代码只是:
final ConditionObject newCondition() {
return new ConditionObject();
}
为什么不直接这样创建呢?在类ArrayBlockingQueue
中,构造函数中有代码:
lock = new ReentrantLock(fair);
notEmpty = lock.newCondition();
notFull = lock.newCondition();
其中 notEmpty
和 notFull
都是 Condition
类的实例。
最佳答案
来自 Condition
的文档:
Condition
factors out theObject
monitor methods (wait
,notify
andnotifyAll
) into distinct objects to give the effect of having multiple wait-sets per object, by combining them with the use of arbitraryLock
implementations. Where aLock
replaces the use ofsynchronized
methods and statements, aCondition
replaces the use of theObject
monitor methods.Conditions (also known as condition queues or condition variables) provide a means for one thread to suspend execution (to "wait") until notified by another thread that some state condition may now be true. Because access to this shared state information occurs in different threads, it must be protected, so a lock of some form is associated with the condition. The key property that waiting for a condition provides is that it atomically releases the associated lock and suspends the current thread, just like
Object.wait
.A
Condition
instance is intrinsically bound to a lock. To obtain aCondition
instance for a particularLock
instance use itsnewCondition()
method.
正如所解释的,Condition
实例必须与 Lock
实例1关联。将 Lock
函数作为创建 Condition
实例的工厂非常有意义,因为它暗示了两者之间的关系。强制执行这种关系的另一种方法是为 Condition
提供一个接受 Lock
实例的构造函数,但因为 Condition
也是一个接口(interface)它不能声明构造函数。我还认为,在这种情况下,无参工厂方法对用户更加友好。
注意:如果还不清楚的话,ReentrantLock
类是 Lock
接口(interface)和 ConditionObject
类是 Condition
接口(interface)的实现。
尝试直接使用 ConditionObject
的另一个问题是它是 AbstractQueuedSynchronizer
2 的内部类(即非静态嵌套类) 。这意味着您需要后一个类的实例才能创建前一个类的实例。但是,ReentrantLock
使用的 AbstractQueuedSynchronizer
实现是一个实现细节,并未向公众公开。换句话说,您无法调用 ConditionObject
的构造函数,这意味着创建实例的唯一方法是通过 newCondition()
。
回顾一下,使用工厂方法创建 Condition
对象至少有三个原因:
Lock
和Condition
之间的关系变得清晰。Lock
和 Condition
都是接口(interface),因此您需要一种方法将 Condition
与 Lock
关联起来在不了解实现的情况下。否则就不可能"program to an interface" .ConditionObject
是一个内部类,因此无法直接实例化,至少不能通过无法访问封闭类实例的代码来实例化。1。 Condition
的方法仅在拥有 Lock
的上下文中才有意义。就像线程必须在对象上同步
才能合法调用该对象的监视方法(即等待/通知)一样,线程必须在它之前拥有关联的Lock
可以合法地调用Condition
的方法(即await/signal)。
2。还有 AbstractQueuedLongSynchronizer
,它声明了自己的 ConditionObject
内部类。虽然该类与 AbstractQueuedSynchronizer
声明的类同名,但两者实际上是不同的类。
关于java - 为什么条件是从 Lock 创建的,而不是使用 'new' 运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58181637/
Or 运算符 对两个表达式进行逻辑“或”运算。 result = expression1 Or expression2 参数 result 任意数值变量。 expression1 任意
Not 运算符 对表达式执行逻辑非运算。 result = Not expression 参数 result 任意数值变量。 expression 任意表达式。 说明 下表显示如何
Is 运算符 比较两个对象引用变量。 result = object1 Is object2 参数 result 任意数值变量。 object1 任意对象名。 object2 任意
\ 运算符 两个数相除并返回以整数形式表示的结果。 result = number1\number2 参数 result 任意数值变量。 number1 任意数值表达式。 numbe
And 运算符 对两个表达式进行逻辑“与”运算。 result = expression1 And expression2 参数 result 任意数值变量。 expression1
运算符(+) 计算两个数之和。 result = expression1 + expression2 参数 result 任意数值变量。 expression1 任意表达式。 exp
我对此感到困惑snippet : var n1 = 5-"4"; var n2 = 5+"4"; alert(n1); alert(n2); 我知道 n1 是 1。那是因为减号运算符会将字符串“4”转
我想我会得到 12,而不是 7。 w++,那么w就是4,也就是100,而w++, w 将是 8,1000;所以 w++|z++ 将是 100|1000 = 1100 将是 12。 我怎么了? int
Xor 运算符 对两个表达式进行逻辑“异或”运算。 result = expression1 Xor expression2 参数 result 任意数值变量。 expression1
Mod 运算符 两个数值相除并返回其余数。 result = number1 Mod number2 参数 result 任意数值变量。 number1 任意数值表达式。 numbe
Imp 运算符 对两个表达式进行逻辑蕴涵运算。 result = expression1 Imp expression2 参数 result 任意数值变量。 expression1 任
Eqv 运算符 执行两个表达式的逻辑等价运算。 result = expression1 Eqv expression2 参数 result 任意数值变量。 expression1 任
我有一个运算符重载的简单数学 vector 类。我想为我的运算符(operator)获取一些计时结果。我可以通过计时以下代码轻松计时我的 +=、-=、*= 和/=: Vector sum; for(s
我是用户定义比较运算符的新手。我正在读一本书,其中提到了以下示例: struct P { int x, y; bool operator、运算符<等),我们
在 SQL 的维基百科页面上,有一些关于 SQL 中 bool 逻辑的真值表。 [1] 维基百科页面似乎来源于 SQL:2003 标准。 等号运算符 (=) 的真值表与 SQL:2003 草案中的 I
我遇到了一个奇怪的 C++ 运算符。 http://www.terralib.org/html/v410/classoracle_1_1occi_1_1_number.html#a0f2780081f
我正在阅读关于 SO 和 answers 中的一个问题,它被提到为: If no unambiguous matching deallocation function can be found, pr
我偶然发现了这个解决方案,但我无法理解其中到底发生了什么。谁能解释一下! 据我了解,它试图通过计算一半的单元格然后将其加倍来计算 a*b 网格中的单元格数量。但是我无法理解递归调用。 请不要建议其他解
Go的基本类型 布尔类型bool 长度:1字节 取值:布尔类型的取值只能是true或者false,不能用数字来表示 整型 通用整型 int / uint(有符号 / 无符号,下面也类似) 长度:根据运
在本教程中,您将学习JavaScript中可用的不同运算符,以及在示例的帮助下如何使用它们。 什么是运算符? 在JavaScript中,运算符是一种特殊符号,用于对运算数(值和变量)执行操作。例如,
我是一名优秀的程序员,十分优秀!