Results 1 to 4 of 4
Thread: Stop multithreads at same time
- 02-25-2009, 12:00 PM #1
Stop multithreads at same time
Nice to be here after a long time.
I had a question, simple one but still is eating up my head.
Condition is "Suppose if 15 threads are running and due to some condition i will have to shutdown jvm and before that i want to stop all the threads."
Can anyone tell me which methos is feasible to use in this condition and how?
Waiting for reply from all the giants here like Eranga and all..To finish sooner, take your own time....
- 02-25-2009, 12:31 PM #2
- 02-25-2009, 12:52 PM #3
Its opening the same link as this one.....
Kidding of me is it??? :rolleyes:To finish sooner, take your own time....
- 02-25-2009, 03:42 PM #4
DB is trying to tell you that you posted the same question twice. Please close the other one.
If you have a bunch of threads running and you want to shut down the JVM, you have several options.
Worst: System.exit(0); will stop the JVM
Second worst: Set each thread as daemon. When the main threads ends, the JVM will shut down.
Best: Keep track of all your threads in an ArrayList object. Place a flag in each Runnable a thread uses, make the flag "volatile", and check that flag regularly. At the same time, check Thread.currentThread().isInterruped(). Set the flag if Thread.sleep() is interrupted. Exit the run() method when the flag is set or the thread is interrupted.
When you want to shutdown, set the flag of each thread's Runnable. If the thread blocks for extended periods, such as waiting on a socket, you may need to wait a second or so, and then interrupt any threads that did not end when the flag was set.
- By Niveditha in forum Threads and SynchronizationReplies: 3Last Post: 02-25-2009, 05:51 PM
- By Exhonour in forum New To JavaReplies: 0Last Post: 01-16-2009, 09:44 PM
- By jyotigupta6 in forum Threads and SynchronizationReplies: 1Last Post: 10-31-2008, 02:41 PM
- By willemjav in forum Advanced JavaReplies: 19Last Post: 09-10-2008, 07:11 AM
- By cecily in forum New To JavaReplies: 1Last Post: 07-24-2007, 01:25 AM