Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我是否应该显式唤醒一个正在阻塞队列.take()的线程以提高性能?_Java_Multithreading_Blockingqueue_Wakeup - Fatal编程技术网

Java 我是否应该显式唤醒一个正在阻塞队列.take()的线程以提高性能?

Java 我是否应该显式唤醒一个正在阻塞队列.take()的线程以提高性能?,java,multithreading,blockingqueue,wakeup,Java,Multithreading,Blockingqueue,Wakeup,我知道,如果让线程使用take()为BlockingQueue中的元素吸吮,将等待元素可用(除非它被中断) 我有两个问题: i) 当一个元素可用时,线程是否会自动唤醒,或者是否存在延迟(即线程稍后会检查自身) ii)如果存在延迟,唤醒线程(例如明确中断线程)是否有意义?我在考虑延迟和性能。没有额外的延迟。如果元素可用或线程中断,则方法调用将返回 Retrieves and removes the head of this queue, waiting if necessary until an

我知道,如果让线程使用
take()
BlockingQueue
中的元素吸吮,将等待元素可用(除非它被中断)

我有两个问题:

i) 当一个元素可用时,线程是否会自动唤醒,或者是否存在延迟(即线程稍后会检查自身)


ii)如果存在延迟,唤醒线程(例如明确中断线程)是否有意义?我在考虑延迟和性能。

没有额外的延迟。如果元素可用或线程中断,则方法调用将返回

Retrieves and removes the head of this queue, waiting if necessary until an element becomes available.

Returns:
    the head of this queue 
Throws:
    InterruptedException - if interrupted while waiting
BlockinQueue
正在自动执行此操作(如
ArrayBlockingQueue


@向下投票人:请留下评论。我只能假设向下投票人(不,不是我:)反对“没有延迟”-可能有计划延迟。当生产者发布项目时,消费者线程已准备就绪,但如果优先级高于消费者的准备/运行线程集的元素数与内核数相同或更多,则不会立即运行消费者线程。不管是哪种方式,中断或是其他类似的对线程的摆弄都不会改变任何事情。将延迟降至最低的唯一方法是确保消费者具有足够高的优先级。感谢您的解释。我编辑了我的答案,因为我指的是可以加快的额外延迟。
// in add etc.
notEmpty.signal();

// in take()
while(count == 0) 
  notEmpty.await();