We are working on an application where a set of objects can be affected by receiving messages from 3 different sources. Each message (from any of the sources) has a single object as its target. Each message receiver will be running on its own thread.
We want the processing of the messages (after receiving), to be as high-speed as possible, so the message processing against the target objects will be done with another thread from a thread pool. The processing of the message will take longer than the reading/receiving of the messages from the senders.
I am thinking that it will be faster if each thread from the pool is dedicated only to a particular set of objects, for example:
Thread1 -> objects named A-L
Thread2 -> objects named M-Z
with each set of objects (or Thread) having a dedicated queue of messages to pending being processed.
My assumption is that if the only thread synchronization needed is between each receiving thread and one processing thread, for the duration of time that it needs to put the message on a blocking queue, that it will be faster than randomly assigning worker threads to process the messages (in which case there might be 2 different threads with messages for the same object).
My question is really 2 parts:
-
Do people agree with the assumption that dedicating worker threads
to a particular set of objects is a better/faster approach? -
Assuming this is a better approach, do the existing Java ThreadPool
classes have a way to support this? Or does it require us coding
our own ThreadPool implementation?
Thanks for any advice that you can offer.
I assume the overall goals is to trying to maximize the concurrent processing of these inbound messages. You have receivers from the 3 sources, that need to put the messages in a pool that will be optimally handled. Because messages from any of the 3 sources may deal with the same target object which cannot be processed simultaneously, you want someway to divide up your messages so they can be processed concurrently but only if they are guaranteed to not refer to the same target object.
I would implement the
hashCode()method on your target object (maybe justname.hashCode()) and then use the value to put the objects into an array ofBlockingQueues, each with a single thread consuming them. Using an array ofExecutors.newSingleThreadExecutor()would be fine. Mod the hash value mode by the number of queues and put it in that queue. You will need to pre-define the number of processors to maximum. Depends on how CPU intensive the processing is.So something like the following code should work:
One of the benefits of this mechanism is that you may be able to limit the synchronization about the target objects. You know that the same target object will only be updated by a single thread.
Yes. That seems the right way to get optimal concurrency.
I don’t know of any thread-pool which accomplishes this. I would not write your own implementation however. Just use them like the code outlines above.