• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to know any one thread is alive while running mulple thread?

 
Kaleeswaran Karuppusamy
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my application we are using 20 runnable interface class and its running at a time.however we can check a single thread is alive using isAlive method.i dont specific object but i want to know all thread is completed....
so please tell me how to know?
 
Edward Harned
Ranch Hand
Posts: 291
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look into Thread.join();
 
Bob Ruth
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not certain that I understand your question but I'll propose an idea for what I currently THINK your question is. (p.s. Some might call ideas like this heresy, and they might be right...but it is at least an idea.

You are spinning off multiple threads and it appears that you want to know when ALL threads are no longer alive. Here is a question and it is important because the idea depends on it.

Do your threads come up and run until complete and then end? Or is it an error situation that terminates them?

What I am thinking is to pass each thread a unique serial number between 0 and the max number of threads up to 19 (for 20 possible threads). Design a coordination object that contains an int value that acts as a "bit mask" of live threads. When your controlling activity spins off a thread, it would call a method to SET the bit ( bitMask |= ( 2 << threadNumber) ). Once all threads have been started, the controllin/monitoring thread could call a method like getStat() that would return the bitMask for evaluation of any kind that you choose. AS each thread exits (whether through running in to a normal end, or in a finally block in case of an exception) it could call a method to CLEAR it's own bit ( bitMask &= ~(2 << threadNumber) ).

This way, when all threads have ended the mask should be zero and you would know immediately that they have all gone to end.

Bear in mind that there are definite coordination/synchronization issues within this idea but it might spin off an idea in your own head that you might be able to get working.
-----
Code. Legos for the adult mind.
 
chandrakant karale
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something equivalent to the concept of 'counting semaphore' can be used.
Each thread increments the lock value while starting, and after finishing the work decrements the lock value.
When the lock value first becomes 20 (you mentioned 20 threads) and then again zero, you are sure that all threads have finished their work.

Look for ways to implement 'Counting semaphore'
 
sarvesh meens
Ranch Hand
Posts: 43
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you want take a specific action on closure of threads,use java.util.concurrent.CyclicBarrier.

refer to synchronizers
for more info on CyclicBarrier
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I like the concept Bob. I think its closer to the point. I would want to know if the job is complete more than to know if the "thread" is complete.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!