Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Have a Thread wait for a Condition

In a GUI, I have a few buttons. These buttons spin off worker threads that send requests over the network to a server. In a seperate thread, there is a listener that receives the responses from the server. This response is passed to the same object that the worker threads are executing methods on via the Observer/Observable interface.

What I need to do is to have the worker threads wait for a response from the server that pertains to them. Essentially a worker thread should send the command, then wait for some condition that indicates the right response was received. I can think of multiple ways to do this (sleeping, polling, wait, notify, monitors, etc), but is there a particular method that would be best in this situation?

like image 548
compuguru Avatar asked Jun 29 '11 20:06

compuguru


People also ask

How do you make a thread wait for some time?

In between, we have also put the main thread to sleep by using TimeUnit. sleep() method. So the main thread can wait for some time and in the meantime, T1 will resume and complete its execution.

How do you wait on condition variable?

The pthread_cond_wait() release a lock specified by mutex and wait on condition cond variable. Return Value : On success, 0 is returned ; otherwise, an error number shall be returned to indicate the error. The pthread_cond_signal() wake up threads waiting for the condition variable.

Can multiple threads wait on the same condition variable?

Since condition variables can only be used within monitors, it is a good practice to have the initialization calls in the monitor initialization function. Multiple threads must not initialize the same condition variable at the same time.

What happens when a thread is waiting?

A thread is in the waiting state when it wants to wait on a signal from another thread before proceeding. Once this signal is received, it becomes runnable.


2 Answers

I would recommend to use a high level "locking" mechanism from the java.util.concurrent package Eg a CountDownLatch -- "A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes. "

like image 169
Schildmeijer Avatar answered Oct 24 '22 00:10

Schildmeijer


This doesn't seem like a place you want to block at all. You are already in a worker thread so you aren't holding any kind of state for your GUI or anything, I'd send the message and bail--when your response comes back, grab another thread out of the pool and send it on it's merry way.

If you have data that is shared between the two, put it into its own object in a collection keyed by some common value and have the new thread pull it out when it comes in.

For readability/simplicity make the object with the data also contain the code that knows how to handle that data so that all you have to do when you get an incoming message is retrieve a key that uniquely identifies which object sent it, retrieve that object by the key and call that object's "Data received" method passing in the information you got from the packet.

The nice thing about this is it makes it trivial for a simple listener to handle a huge variety of incoming packets by making the "Data received" method an interface that many different handlers can implement.

OO is cool, eh?

like image 37
Bill K Avatar answered Oct 24 '22 00:10

Bill K