It's not a secret anymore!*
The moose likes Threads and Synchronization and the fly likes How to wait until all threads finishes task? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "How to wait until all threads finishes task?" Watch "How to wait until all threads finishes task?" New topic
Author

How to wait until all threads finishes task?

Kamalakar Sharma
Greenhorn

Joined: Dec 08, 2004
Posts: 5
Hi all,

I am in process of writing a java multithread program.

Following are the steps.

1. In a method I am creating 4 threads
public void test()
{
DocThread t1 = new DocThread("one");
t1.start();
DocThread t2 = new DocThread("one");
t2.start();
DocThread t3 = new DocThread("one");
t3.start();
DocThread t4 = new DocThread("one");
t4.start();

Thread currentThread = Thread..currentThread();
try {
currentThread.sleep(10000);
}catch(Exception e) {
//do some thing
}

//do some work..

}
2. starting the above 4 threads.
3. using current thread to sleep let say 20000 in mani thread

but, what I am encountering is without waiting for all the above 4 threads the main thread exits after it sleeps for 20000?

Any one can give me better approch to wait for all the 4 threads finishes their jobs so the main thread will wait?

Thanks in Advance.
Kamal.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
Hi Kamal,

Insteaad of currentThread.sleep(10000); you need,

t1.join();
t2.join();
t3.join();
t4.join();

Please look at the API to see what Thread#join() method does. In short- it waits for the thread to return (which means until the thread's run() method returns).

Regards
Maulin
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
The first time I read about join I did this:

That ran one at a time as I waited for each thread to finish before starting the next! Be sure to start and join in different loops:


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Kamalakar Sharma
Greenhorn

Joined: Dec 08, 2004
Posts: 5
Hi all,

Thanks for your suggestions and it worked.

I have used the following code :

t1.start();
t2.start();
t3.start();
t4.start();

synchronized(this)
{
try{
t1.join();
t2.join();
t3.join();
t4.join();
}catch(Exception e) {
//do something
}
}

The above four threads will run concorrently and the synchronized blocks will casue the main thread to wait until each thread run() methods returns.

Thanks all,
Kamalakar B.
[ December 10, 2004: Message edited by: Kamalakar Sharma ]
 
wood burning stoves
 
subject: How to wait until all threads finishes task?
 
Similar Threads
thread overview.
How can i start multiple threads ??
ReentrantReadWriteLock's lock mechanism
Thread dependency
Threads Using Static Method