- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在解释多线程中的有界缓冲区示例时,以下术语是什么:
1.一进一出要求
2.服务员统一要求
我在书中看到以下说法java concurrency in practice
BoundedBuffer meets the one-in, one-out requirement, but does not meet the uniform waiters requirement because waiting threads might be waiting for either the “not full” and “not empty” condition.
有人可以向我解释一下这两个术语及其区别吗?
谢谢
最佳答案
这些术语指的是在任意监视器上 wait()
的线程:
只有在满足两个属性时才应使用 notify()
,否则调用 notifyAll()
(尽管效率较低)。
Goetz 在第 303 页对此进行了解释,并给出了两个具体示例:
BoundedBuffer
meets the one-in, one-out requirement, but does not meet the uniform waiters requirement because waiting threads might be waiting for either the "not full" [producers block on put] and "not empty" [consumers block on take] condition". A "starting gate" latch […] in which a single event releases a set of threads, does not meet the one-in, one-out requirement because opening the starting gate lets multiple threads proceed.
您还可以使用 java.util.concurrent
中的类来演示这一点:
BlockingQueue<String> queue = new ArrayBlockingQueue<>(1);
new Thread(() -> {
try {
TimeUnit.SECONDS.sleep(1L);
System.out.println("Producer starts.");
queue.put("foo");
} catch (InterruptedException e) { /* NOP */ }
}).start();
try {
System.out.println("Consumer starts.");
System.out.println(queue.take());
} catch (InterruptedException e) { /* NOP */ }
消费者必须等待生产者将“foo”
放入队列。两者等待不同的条件,因此, BlockingQueue
不符合服务员统一要求。
另一方面,CountDownLatch
满足服务员统一要求,但不满足一进一出要求。一旦锁存器达到零,所有等待线程都会被释放:
CountDownLatch latch = new CountDownLatch(1);
Runnable r = () -> {
try {
latch.await();
System.out.println("Thread proceeds.");
} catch (InterruptedException e) { /* NOP */ }
};
new Thread(r).start();
new Thread(r).start();
try {
TimeUnit.SECONDS.sleep(1L);
System.out.println("Count down.");
latch.countDown();
} catch (InterruptedException e) { /* NOP */ }
关于java - 多线程java中的一进一出要求与统一服务员要求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38857942/
需要帮助将这些给定的数字打印成星号,但我是编程新手;我该怎么做? #include int main(void) { int a[5]={20,1,5,15,12}; int i=0
使用 Delphi XE 2 我试图确定缩放方向以将缩放效果应用于图像(TImage),但没有找到执行此操作的函数,并且图像的 OnGesture 事件中的 EventInfo 属性没有此信息. 我见
我不知道制服在内存中是如何表示的。 制服似乎会占用宝贵的寄存器空间,但它们最终会传入/通过/传出到全局内存中,对吗? 制服不用时情况会发生变化吗?编译器可以将它们优化掉吗?--在这种情况下,我已经将无
我正在尝试在名为“timeclock”的模型上记录“time_in”和“time_out”记录。这是我想做但无法开始工作的事情! 检查最后一个时钟条目,看看它是否同时填充了“time_in”和“tim
我想听听您如何解决这种编程任务!?每种类型(OPER = 1 类型)对应一种特定的信息。 这只是大约 10 个具有相同结构的规范之一。首选创建这些“转换器”(协议(protocol))的通用方法。 最
我正在使用 Rest API(NodeJS、Express)和 PostgreSQL 制作 React-Native 应用。 在我的本地机器上托管时一切正常。当 API 托管在我的机器上并且 Post
我是一名优秀的程序员,十分优秀!