0

I need to write a multi-threaded java application where I will be creating number of threads to handle different types of operation. For example, one thread will monitor state of the application, one thread will manage communication with other nodes in the cluster, one thread will have some application logic and so on.

What is the good way to pass the data and signals between the threads? For example, the application logic thread may need to send a message which it can hand off to the communication thread. Here the data will need to be transferred between the two threads. One way I thought was to use a queue where all the threads wanting to send a message can insert their message. However, it presents few problems:

  1. The queue and the method used for insertion will need to be static OR
  2. Each thread that wishes to send a message has to have the object of the communication thread

Moreover, such send method will provide no way for me to tell the calling thread if the sending fails (for example, throwing an exception) because once the message is inserted in the queue, the calling thread thinks it sent its message.

What is the good way to pass signal and data between the threads for such applications?

This might be too fundamental question for software engineers (I come from electronics background). If so, can anyone point me to a good source where I can read about designing multi-threaded or multi-processed applications?

Thanks a lot.

1
  • Are you sure that threads is the right way to slice this? It seems to me that each of the tasks you have named is a class, not a thread. The number of threads should be related to the number of clients, or requests, etc, not to the number of distinct tasks in the application. The fact that you've come across this communications difficulty reinforces that view. Maybe everything should just be a method call? Commented Oct 9, 2013 at 1:20

1 Answer 1

1

I posted here on a similar question recently.

You are looking for a BlockingQueue and as the code there shows, they are easy to work with.

Of your points:

The queue and the method used for insertion will need to be static OR

No - as you can see in the sample, if you build the queue and pass it to both objects that wish to communicate (or keep them all in a Map like I did in that answer) they can work quite happily together.

Each thread that wishes to send a message has to have the object of the communication thread

No - just the queue.

Moreover, such send method will provide no way for me to tell the calling thread if the sending fails ...

Just because you have put the message in the queue doesn't mean you can't keep hold of it. You could wait for it to be changed by the other thread and respond to that or you could have two queues, one for the message and one for the response or put a semaphore in the massage that gets tripped when the message is actioned. It's just like wiring up a circuit.

Being from an electronics background doesn't mean you have to apologise - someone has to keep the lights on. {grin}

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

2 Comments

Thanks for the response. There a still a couple of things not very clear to me. I understood that there can be number of queues shared between threads. However, if a worker thread finishes its work and terminates, its queue will remain in the communicator thread's Map and there will not be a way of knowing which of the queues are dead. Also, each thread willing to send a message has to have a queue shared with the communicator, but in my case, the worker thread may be spawned when communicator is already in run. How do I pass the queue then? Do I need a static method to insert queue in map?
About sending the signal back, the worker thread needs to have a way to know if the sending has failed (which can be after the communicator pops the message from queue). In other words, the worker can't have peace by knowing that his message is operated on. He needs to know if the operation was successful. I was thinking of checking return value of a method like C or catching an exception. But if I need the comm thread send a message to the worker in another queue, then I will need to have kind of handshake messages in place right? Thanks a lot..

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.