• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to make a main thread wait until all other threads die out?

 
Bhargav Bobby
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 291
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried this but it does not look good.



Please suggest other nice way to do so.

Thanks,
 
Edward Harned
Ranch Hand
Posts: 291
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look nice?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"?
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jim,

I made change to the code, availing the benefit of for loop:





Thanks,
 
M Poongundran
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


 
Henry Wong
author
Marshal
Pie
Posts: 21427
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !!
 
M Poongundran
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.



 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic