Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>SynchronousQueue</code> is a very special kind of queue - it implements a rendezvous approach (producer waits until consumer is ready, consumer waits until producer is ready) behind the interface of <code>Queue</code>.</p> <p>Therefore you may need it only in the special cases when you need that particular semantics, for example, <a href="https://stackoverflow.com/questions/4954708/single-threading-a-task-without-queuing-further-requests/4954938#4954938">Single threading a task without queuing further requests</a>.</p> <p>Another reason for using <code>SynchronousQueue</code> is performance. Implementation of <code>SynchronousQueue</code> seems to be heavily optimized, so if you don't need anything more than a rendezvous point (as in the case of <code>Executors.newCachedThreadPool()</code>, where consumers are created "on-demand", so that queue items don't accumulate), you can get a performance gain by using <code>SynchronousQueue</code>. </p> <p>Simple synthetic test shows that in a simple single producer - single consumer scenario on dual-core machine throughput of <code>SynchronousQueue</code> is ~20 time higher that throughput of <code>LinkedBlockingQueue</code> and <code>ArrayBlockingQueue</code> with queue length = 1. When queue length is increased, their throughput rises and almost reaches throughput of <code>SynchronousQueue</code>. It means that <code>SynchronousQueue</code> has low synchronization overhead on multi-core machines compared to other queues. But again, it matters only in specific circumstances when you need a rendezvous point disguised as <code>Queue</code>.</p> <p><strong>EDIT:</strong></p> <p>Here is a test:</p> <pre><code>public class Test { static ExecutorService e = Executors.newFixedThreadPool(2); static int N = 1000000; public static void main(String[] args) throws Exception { for (int i = 0; i &lt; 10; i++) { int length = (i == 0) ? 1 : i * 5; System.out.print(length + "\t"); System.out.print(doTest(new LinkedBlockingQueue&lt;Integer&gt;(length), N) + "\t"); System.out.print(doTest(new ArrayBlockingQueue&lt;Integer&gt;(length), N) + "\t"); System.out.print(doTest(new SynchronousQueue&lt;Integer&gt;(), N)); System.out.println(); } e.shutdown(); } private static long doTest(final BlockingQueue&lt;Integer&gt; q, final int n) throws Exception { long t = System.nanoTime(); e.submit(new Runnable() { public void run() { for (int i = 0; i &lt; n; i++) try { q.put(i); } catch (InterruptedException ex) {} } }); Long r = e.submit(new Callable&lt;Long&gt;() { public Long call() { long sum = 0; for (int i = 0; i &lt; n; i++) try { sum += q.take(); } catch (InterruptedException ex) {} return sum; } }).get(); t = System.nanoTime() - t; return (long)(1000000000.0 * N / t); // Throughput, items/sec } } </code></pre> <p>And here is a result on my machine:</p> <p><img src="https://i.stack.imgur.com/UuEpU.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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