Java concurrency - Should block or yield?

Posted by teto on Stack Overflow See other posts from Stack Overflow or by teto
Published on 2010-03-09T20:36:43Z Indexed on 2010/03/16 14:36 UTC
Read the original article Hit count: 151

Filed under:
|
|

Hi,

I have multiple threads each one with its own private concurrent queue and all they do is run an infinite loop retrieving messages from it. It could happen that one of the queues doesn't receive messages for a period of time (maybe a couple seconds), and also they could come in big bursts and fast processing is necessary.

I would like to know what would be the most appropriate to do in the first case: use a blocking queue and block the thread until I have more input or do a Thread.yield()?

I want to have as much CPU resources available as possible at a given time, as the number of concurrent threads may increase with time, but also I don't want the message processing to fall behind, as there is no guarantee of when the thread will be reescheduled for execution when doing a yield(). I know that hardware, operating system and other factors play an important role here, but setting that aside and looking at it from a Java (JVM?) point of view, what would be the most optimal?

© Stack Overflow or respective owner

Related posts about java

Related posts about concurrency