Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a Java thread wait for another thread's output?

I'm making a Java application with an application-logic-thread and a database-access-thread. Both of them persist for the entire lifetime of the application and both need to be running at the same time (one talks to the server, one talks to the user; when the app is fully started, I need both of them to work).

However, on startup, I need to make sure that initially the app thread waits until the db thread is ready (currently determined by polling a custom method dbthread.isReady()). I wouldn't mind if app thread blocks until the db thread was ready.

Thread.join() doesn't look like a solution - the db thread only exits at app shutdown.

while (!dbthread.isReady()) {} kind of works, but the empty loop consumes a lot of processor cycles.

Any other ideas? Thanks.

like image 267
Piskvor left the building Avatar asked Nov 14 '08 07:11

Piskvor left the building


People also ask

How do I make one thread wait for another?

The wait() Method Simply put, calling wait() forces the current thread to wait until some other thread invokes notify() or notifyAll() on the same object. For this, the current thread must own the object's monitor.

Which command allows one thread to wait for the completion of another thread?

Thread class provides the join() method which allows one thread to wait until another thread completes its execution. If t is a Thread object whose thread is currently executing, then t. join() will make sure that t is terminated before the next instruction is executed by the program.

What is wait () and notify () in multithreading?

The wait() method causes the current thread to wait until another thread invokes the notify() or notifyAll() methods for that object. The notify() method wakes up a single thread that is waiting on that object's monitor. The notifyAll() method wakes up all threads that are waiting on that object's monitor.

What is wait () in Java?

Simply put, wait() is an instance method that's used for thread synchronization. It can be called on any object, as it's defined right on java. lang. Object, but it can only be called from a synchronized block. It releases the lock on the object so that another thread can jump in and acquire a lock.


2 Answers

Use a CountDownLatch with a counter of 1.

CountDownLatch latch = new CountDownLatch(1); 

Now in the app thread do-

latch.await(); 

In the db thread, after you are done, do -

latch.countDown(); 
like image 139
pdeva Avatar answered Sep 28 '22 04:09

pdeva


I would really recommend that you go through a tutorial like Sun's Java Concurrency before you commence in the magical world of multithreading.

There are also a number of good books out (google for "Concurrent Programming in Java", "Java Concurrency in Practice".

To get to your answer:

In your code that must wait for the dbThread, you must have something like this:

//do some work synchronized(objectYouNeedToLockOn){     while (!dbThread.isReady()){         objectYouNeedToLockOn.wait();     } } //continue with work after dbThread is ready 

In your dbThread's method, you would need to do something like this:

//do db work synchronized(objectYouNeedToLockOn){     //set ready flag to true (so isReady returns true)     ready = true;     objectYouNeedToLockOn.notifyAll(); } //end thread run method here 

The objectYouNeedToLockOn I'm using in these examples is preferably the object that you need to manipulate concurrently from each thread, or you could create a separate Object for that purpose (I would not recommend making the methods themselves synchronized):

private final Object lock = new Object(); //now use lock in your synchronized blocks 

To further your understanding:
There are other (sometimes better) ways to do the above, e.g. with CountdownLatches, etc. Since Java 5 there are a lot of nifty concurrency classes in the java.util.concurrent package and sub-packages. You really need to find material online to get to know concurrency, or get a good book.

like image 39
Herman Lintvelt Avatar answered Sep 28 '22 04:09

Herman Lintvelt