Java concurrency - Should it block or be inferior?

I have several threads, each of which has its own private parallel queue, and everything that they do starts an infinite loop, receiving messages from it. It may happen that one of the queues will not receive messages for a certain period of time (maybe a couple of seconds), and they can also be in large packets and fast processing is required.

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

I want to have as many CPU resources as possible at a given time, since the number of parallel threads can increase over time, but I also do not want message processing to lag, because there is no guarantee when the thread will be redistributed for execution when yield () is executed. I know that hardware, operating system and other factors play an important role here, but putting that aside and looking at it from the point of view of Java (JVM?), What would be the most optimal?

+6
java multithreading concurrency
source share
4 answers

It’s always easy to block queues. Java queues internally.

In other words: you cannot gain any performance advantage in other threads if you give way in one of them and not just block.

+9
source share

Of course, you want to use a blocking queue - they are designed specifically for this purpose (you want your threads to not use processor time when there is no work).

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

+7
source share

Alternatively, consider converting your implementation to use one of the managed ExecutorService β€” possibly ThreadPoolExecutor .

This may not be appropriate for your use case, but if it does, it removes the bother of worrying about managing threads from your own code - and these are questions about whether to give in or not just disappear.

In addition, if in the future there are better flow control algorithms - for example, something similar to Apple Grand Central Dispatch - you may be able to convert your application to use it almost effortlessly.

0
source share

Another thing you can do is use a parallel hash map for your queue. When you read, it gives you a link to the object you were looking for, so you can skip the message that was just queued. But if all this does is to listen to the message, which you will understand at the next iteration. It would be different if messages could be updated by other threads. But actually, there seems to be no reason to block what I see.

0
source share

All Articles