1

I have object instances of a custom class, and each instance processes messages (via methods) coming through independently for each instance. No instances "talk" to other instances.

My question is, is putting each object in its own thread necessary since each object processes independently real-time messages (logs etc...) coming through anyhow?

Thanks for any responses.

7
  • What is your care about multithreading? Give each object to thread for processing Commented Jul 3, 2013 at 18:17
  • Does your program work as it's supposed to? Commented Jul 3, 2013 at 18:18
  • yes, I'm just under the impression multithreading solution might improve it, but it may also be pointless. Commented Jul 3, 2013 at 18:21
  • By "improve it" do you mean it's performance? Commented Jul 3, 2013 at 18:21
  • 1
    If there isn't a performance issue I would assume that your implementation is fine the way it is, but wrapping the objects around a thread would improve the performance (execution time). Commented Jul 3, 2013 at 18:26

2 Answers 2

1

My question is, is putting each object in its own thread necessary since each object processes independently real-time messages (logs etc...) coming through anyhow?

You need to process each of the message acquired by each object in new separate thread. This will lead to fast processing of the incoming messages for your object. And since , there is not interaction between each object so no thread synchronization is needed which is good for your application. Or, better that you use pool of threads. Have a look at ThreadPoolExecutor

Sign up to request clarification or add additional context in comments.

1 Comment

If you wish to get more clarification you can ask me.I would love to know and solve your queries..
0

It is not necessary for each object to have its own thread, however, you may gain improved performance by having more than one message processing thread. The ideal number of threads is not necessarily (or even likely) to be the same as the number of processing objects.

Typically, in a situation like you describe the approach would be to use a task / message processing queue where each object you have adds tasks to the queue, and then multiple threads process items from the queue in order. The number of threads used here is configurable so that the application can be optimized for the platform it is running on.

An easy way to achieve this design is to simply use an ExecutorService as your task queue (in which case your messages themselves must implement Runnable):

// For 2 threads, adjust as appropriate.
ExecutorService executor = Executors.newCachedThreadPool(2);

And then to add a Runnable message:

// Add a message to the queue for concurrent / asynchronous processing
executor.submit(message);

Note that the executor itself should be shared across all of your message handling objects, so that each object is adding messages to the same queue (assuming you have many message handling objects). It is also possible to have a queue per message handling object, but that decision would depend on the number of handling objects and any requirements surrounding how messages are processed.

3 Comments

Yes, this is exactly whats happening in the background in terms of the message queue. I'm overriding methods that I believe work the way you explained, the message goes on the queue and the method checks whether there is a message on the queue, then processes it. each object instance has these methods basically. thanks for the response.
@lcplusplus If the messages are already queued but need to be handed off to different handler objects then the ideal design would be to have a fixed set of threads reading the queue and handing the messages off to the appropriate message handler. The idea being to decouple the number of threads from the number of message handlers, since the ideal number of threads is probably not the same as the number of handlers.
yes, thats already happening in the background as far as I can tell.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.