• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread Interruption

 
Ja King
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ja King
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21007
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic