| Author |
How to make a main thread wait until all other threads die out?
|
Bhargav Bobby
Greenhorn
Joined: May 07, 2007
Posts: 1
|
|
|
In my program i have 10 threads working simultaneously, the threads are used to fetch data(one row per thread)from the database and send it to some other method. when the data in the database reaches the nth record the first thread that encounters this will spawn a main governing thread and it dies out. My problem is that i have to wait till all the other threads die out for the main governing thread to continue with its operation. How can i know the wait time for the main governing thread to suspend with its operation till all the other threads die out. Can anyone give me a solution for how to wait till the other threads die out and then continue with the main threads execution... NOTE: All the threads are individual threads, they are not in a single pool..
|
 |
Edward Harned
Ranch Hand
Joined: Sep 19, 2005
Posts: 287
|
|
Look at Thread.join() Your main thread can create the "main governing thread" and pass it the thread objects for the other threads. The main... can issue a join(). This will block until the joined threads complete. You could just make the main thread the governing thread.
|
Ed's latest article: A Java Parallel Calamity http://coopsoft.com/ar/Calamity2Article.html
|
 |
Chandra Bhatt
Ranch Hand
Joined: Feb 28, 2007
Posts: 1707
|
|
Hi Edward, Could you please give an example for this, I tried that but no. I want the main thread to join all the threads. Thanks,
|
cmbhatt
|
 |
Chandra Bhatt
Ranch Hand
Joined: Feb 28, 2007
Posts: 1707
|
|
I tried this but it does not look good. Please suggest other nice way to do so. Thanks,
|
 |
Edward Harned
Ranch Hand
Joined: Sep 19, 2005
Posts: 287
|
|
|
Look nice?
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
Well, you could always use another for loop around the join() calls. Copying the same code 10 times (with a slight change of index) is often a clue that a for loop can be introduced. Other than that, what's wrong with the way the code "looks"?
|
"I'm not back." - Bill Harding, Twister
|
 |
Chandra Bhatt
Ranch Hand
Joined: Feb 28, 2007
Posts: 1707
|
|
Thanks Jim, I made change to the code, availing the benefit of for loop: Thanks,
|
 |
M Poongundran
Greenhorn
Joined: Jan 05, 2013
Posts: 2
|
|
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16680
|
|
M Poongundran wrote:
Two points.
First, posting code without any explanation or any context is not very useful.
Second, answering a topic that hasn't been responded to for *five* years is also likely useless -- as the original poster has likely moved on.
Perhaps your time is better spent helping someone who may be still expecting an answer?
Henry
PS... Welcome to the ranch !!
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
M Poongundran
Greenhorn
Joined: Jan 05, 2013
Posts: 2
|
|
Try this.
Main Thread will be in sleep until all the execution of treads in "lThreadGroup" gets completed. Execution of Threads in Group "lThreadGroup" are independent.
|
 |
 |
|
|
subject: How to make a main thread wait until all other threads die out?
|
|
|