Should I explicitly wake up a thread sucking BlockingQueue.take () for performance?

I understand that when sucking threads for BlockingQueue elements with the take() method , it will wait until the element is available (if it is not interrupted).

I have two questions:

i) Is the thread automatically woken up as soon as the item becomes available or there is a delay (i.e. the thread checks itself later)?

ii) If there is a delay, does it make sense to awaken the stream (for example, by interrupting it explicitly)? I think of latency and performance.

+4
source share
1 answer

There is no additional delay. A method call returns if the item is available or the thread is interrupted.

 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 does this automatically (impl. Of ArrayBlockingQueue ).

 // in add etc. notEmpty.signal(); // in take() while(count == 0) notEmpty.await(); 
+4
source

All Articles