• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Thread Interruption

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
These are not the droids you are looking for. Perhaps I can interest you in a tiny ad?
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic