java - Implementation of BlockingQueue: What are the differences between SynchronousQueue and LinkedBlockingQueue -


i see these implementation of blockingqueue , can't understand differences between them. conclusion far:

  1. i won't ever need synchronousqueue
  2. linkedblockingqueue ensures fifo, blockingqueue must created parameter true make fifo
  3. synchronousqueue breaks collections method (contains, size, etc)

so when ever need synchronousqueue? performance of implementation better linkedblockingqueue?

to make more complicated... why executors.newcachedthreadpool use synchronousqueue when others (executors.newsinglethreadexecutor , executors.newfixedthreadpool) use linkedblockingqueue?

edit

the first question solved. still don't understand why executors.newcachedthreadpool use synchronousqueue when others (executors.newsinglethreadexecutor , executors.newfixedthreadpool) use linkedblockingqueue?

what is, synchronousqueue, producer blocked if there no free thread. since number of threads practically unlimited (new threads created if needed), never happen. why should uses synchronousqueue?

synchronousqueue special kind of queue - implements rendezvous approach (producer waits until consumer ready, consumer waits until producer ready) behind interface of queue.

therefore may need in special cases when need particular semantics, example, single threading task without queuing further requests.

another reason using synchronousqueue performance. implementation of synchronousqueue seems heavily optimized, if don't need more rendezvous point (as in case of executors.newcachedthreadpool(), consumers created "on-demand", queue items don't accumulate), can performance gain using synchronousqueue.

simple synthetic test shows in simple single producer - single consumer scenario on dual-core machine throughput of synchronousqueue ~20 time higher throughput of linkedblockingqueue , arrayblockingqueue queue length = 1. when queue length increased, throughput rises , reaches throughput of synchronousqueue. means synchronousqueue has low synchronization overhead on multi-core machines compared other queues. again, matters in specific circumstances when need rendezvous point disguised queue.

edit:

here test:

public class test {     static executorservice e = executors.newfixedthreadpool(2);     static int n = 1000000;      public static void main(string[] args) throws exception {             (int = 0; < 10; i++) {             int length = (i == 0) ? 1 : * 5;             system.out.print(length + "\t");             system.out.print(dotest(new linkedblockingqueue<integer>(length), n) + "\t");             system.out.print(dotest(new arrayblockingqueue<integer>(length), n) + "\t");             system.out.print(dotest(new synchronousqueue<integer>(), n));             system.out.println();         }          e.shutdown();     }      private static long dotest(final blockingqueue<integer> q, final int n) throws exception {         long t = system.nanotime();          e.submit(new runnable() {             public void run() {                 (int = 0; < n; i++)                     try { q.put(i); } catch (interruptedexception ex) {}             }         });              long r = e.submit(new callable<long>() {             public long call() {                 long sum = 0;                 (int = 0; < 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     } }     

and here result on machine:

enter image description here


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -