Note that there are some explanatory texts on larger screens.

plurals
  1. POConcurrent and Blocking Queue in Java
    text
    copied!<p>I have the classic problem of a thread pushing events to the incoming queue of a second thread. Only this time, I am very interested about performance. What I want to achieve is:</p> <ul> <li>I want concurrent access to the queue, the producer pushing, the receiver poping.</li> <li>When the queue is empty, I want the consumer to block to the queue, waiting for the producer.</li> </ul> <p>My first idea was to use a <code>LinkedBlockingQueue</code>, but I soon realized that it is not concurrent and the performance suffered. On the other hand, I now use a <code>ConcurrentLinkedQueue</code>, but still I am paying the cost of <code>wait()</code> / <code>notify()</code> on each publication. Since the consumer, upon finding an empty queue, does not block, I have to synchronize and <code>wait()</code> on a lock. On the other part, the producer has to get that lock and <code>notify()</code> upon every single publication. The overall result is that I am paying the cost of <code>sycnhronized (lock) {lock.notify()}</code> in every single publication, even when not needed.</p> <p>What I guess is needed here, is a queue that is both blocking and concurrent. I imagine a <code>push()</code> operation to work as in <code>ConcurrentLinkedQueue</code>, with an extra <code>notify()</code> to the object when the pushed element is the first in the list. Such a check I consider to already exist in the <code>ConcurrentLinkedQueue</code>, as pushing requires connecting with the next element. Thus, this would be much faster than synchronizing every time on the external lock.</p> <p>Is something like this available/reasonable? </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload