[concurrency-interest] Strange behavior with LinkedBlockingQueue

David Holmes dholmes@dltech.com.au
Wed, 27 Oct 2004 09:24:47 +1000


>            if (count > 0)
>              l.clear();
>            else
>              Thread.yield();

There is no guarantee that Thread.yield will actually do anything. Unless
your consumer blocks for some other reason, it can run forever without the
producer getting a further chance to run. yield() is just a hint to the
scheduler, you can not rely on it to guarantee progression.

Similarly, priorities are just hints - and in the JDK on certain systems,
have quite unexpected affects these days. (See the release notes)

Either change the yield to a sleep, or else utilise the blocking take()
method to ensure producer and consumer coordinate their actions properly.

David Holmes