[concurrency-interest] Single producer, single consumer: unexpected delays for producer

Doug Lea dl at cs.oswego.edu
Sat Aug 9 11:16:02 EDT 2008

Daniel Harvey wrote:
> What is so odd to me is that 90% of the total time spent in onMessage() 
> comes from time spent in queue.offer() despite the fact that this is 
> only called for 20% of the messages. If I have the sendThread skip the 
> socketChannel.write() command, than the situation changes greatly: time 
> spent in queue.offer drops by a literally a factor of 6 or so.

This may be due to slow thread unblocking in your VM.
You can try to avoid this a bit by pre-spinning on take:

String prespinTake(BlockingQueue<String> q) {
   for (int i = 0; i < SPINS; ++i) {
     String x = q.poll();
     if (x != null)
        return x;
   return q.take();

where SPINS is a number say between 1 and 1000.
This will avoid offer() needing to wake up a blocked take thread.


More information about the Concurrency-interest mailing list