In case of producer consumer problem, the code of SynchronousBlockingQueue is almost identical to linked blocking queue one, but the application has an added benefit, in that SynchronousQueue will allow an insert into the queue only if there is a thread waiting to consume it.
As discussed here, synchronous blocking queue has capacity of zero. So it implements a rendezvous approach (producer waits until consumer is ready, consumer waits until producer is ready) behind the interface of
Queue
.Also implementation of
SynchronousQueue
seems to be heavily optimized, so if you don't need anything more than a rendezvous point (as in the case of Executors.newCachedThreadPool()
, where consumers are created "on-demand", so that queue items don't accumulate), you can get a performance gain by using SynchronousQueue
.
No comments:
Post a Comment