Forums Register Login

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

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
Look into Thread.join();
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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'
+Pie Number of slices to send: Send
if you want take a specific action on closure of threads,use java.util.concurrent.CyclicBarrier.

refer to synchronizers
for more info on CyclicBarrier
+Pie Number of slices to send: Send
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.
What's a year in metric? Do you know this metric stuff tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1021 times.
Similar Threads
How to display all the session variables from an HTML page?
Running the process even after the program quits!
setDaemon()
Threads problem.
thread again
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 06:58:56.