Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java concurrency - Should block or yield?

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?

like image 796
teto Avatar asked Mar 09 '10 20:03

teto


2 Answers

Always just block on the queues. Java yields in the queues internally.

In other words: You cannot get any performance benefit in the other threads if you yield in one of them rather than just block.

like image 118
Christopher Oezbek Avatar answered Sep 29 '22 02:09

Christopher Oezbek


You certainly want to use a blocking queue - they are designed for exactly this purpose (you want your threads to not use CPU time when there is no work to do).

Thread.yield() is an extremely temperamental beast - the scheduler plays a large role in exactly what it does; and one simple but valid implementation is to simply do nothing.

like image 20
Steven Schlansker Avatar answered Sep 29 '22 03:09

Steven Schlansker