Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes Thread Interruption Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Thread Interruption" Watch "Thread Interruption" New topic
Author

Thread Interruption

Ja King
Greenhorn

Joined: May 15, 2009
Posts: 28
I am running multiple threads (Assume Thread1,Thread2,Thread3) for doing single operation.If any one of the threads done that operation(Thread2) then the remaining threads(Thread1,Thread3) will stop suddenly.

please guide me how to kill that remaining threads suddenly.I don't know how to stop it suddenly.(Marked in red color).

Ex:

Thread1 running....

Thread2 running....

Thread3 running....

Thread2 started to processing....

Thread1 killed.....

Thread3 Killed.....

Thread2 processing...

Thread2 processing...

Thread2 completed.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4164
    
  21

The only safe way to stop another Thread is to have a signal between all concerned Threads, and have all of the Threads periodically check that signal. One very common signal is the 'interrupted' flag. The Thread which wants to stop the others calls otherThread.interrupt() on each of the other Threads. The ones which need to respond check the interrupt status using Thread.currentThread().isInterrupted(). You would also want to correctly handle InterruptedExceptions.

This may or may not be what you need. If you need more information we would need more details about what you have and what you want to do.


Steve
Ja King
Greenhorn

Joined: May 15, 2009
Posts: 28
Ex:

import java.util.Random;


class RandomGenerator implements Runnable{

public void run(){
random();
}

public void random(){
Random random = new Random();
int randomNum = 0;
do{
randomNum = random.nextInt(100);
}while(!(randomNum == 5));
System.out.println(Thread.currentThread() + " identified the random number : " + randomNum);
}
}

public class TestThread1 {
public static void main(String args[]){
RandomGenerator rg = new RandomGenerator();
Thread th1 = new Thread(rg,"Thread1");
th1.start();
Thread th2 = new Thread(rg,"Thread2");
th2.start();
Thread th3 = new Thread(rg,"Thread3");
th3.start();
}

}


Actual output :
Thread[Thread2,5,main] identified the random number : 5
Thread[Thread1,5,main] identified the random number : 5
Thread[Thread3,5,main] identified the random number : 5

Expected output:
Thread[Thread2,5,main] identified the random number : 5
Thread1 killed
Thread2 Killed

Note: Don't use boolean variable to check continuously.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4164
    
  21

Have you tried to implement my suggestion above? If not, that what about it does not suit your needs?

Note: Don't use boolean variable to check continuously.

Why this limitation? There is no safe way to stop a running Thread externally. You should make the Thread choose to come to an end (and thus give it the ability to shut down its own resources, etc...) by sending it a signal. There are different ways to send that signal, but the best is some sort of boolean.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18496
    
  40

Steve Luke wrote:
Note: Don't use boolean variable to check continuously.

Why this limitation? There is no safe way to stop a running Thread externally. You should make the Thread choose to come to an end (and thus give it the ability to shut down its own resources, etc...) by sending it a signal. There are different ways to send that signal, but the best is some sort of boolean.


Agreed. If a thread is spinning on the flag, then yeah, that's bad. Spin waits eats a cpu, which could be used elsewhere.

However, there nothing wrong with checking per iteration. It is already checking per iteration for the loop handling already.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Thread Interruption
 
Similar Threads
notifyAll()
Behavior of yield() in Threads
setPriority not working as expected
Is notifyAll good enough?
How to interrupt or stop the threads