Reputation: 177
Could someone please tell me how to stop a thread if I have the following structure?
I want to stop the thread B after it expires thread C.
c = new c();
c.start();
b = new b();
b.start();
class c extends Thread {
@Override
public void run() {
// DRAW IMAGE
// b.stop(); - doenst work
}
}
class b extends Thread {
@Override
public void run() {
// PROGRESS BAR
}
}
Upvotes: 1
Views: 368
Reputation: 1224
The solution to this is explained quite well here. Any thread that might need a status flag for shutdown could have the following structure:
volatile boolean shutdownRequested;
...
public void shutdown() { shutdownRequested = true; }
public void doWork() {
while (!shutdownRequested) {
// do stuff
}
}
Thus, in your case, your class B would look similar to the above. And then, in class C, you can call the shutdown() method of class B.
Upvotes: 1
Reputation: 8657
There is no good way to stop a thread instantly.
There is Thread.stop()
, but it is dangerous and deprecated. Don't use it unless you have thoroughly analyzed your code and determined that the risks are acceptable.
There is Thread.interrupt()
, but there is no guarantee that the thread will stop quickly, or even stop at all.
For Example:
while (!Thread.interrupted()) {
try {
//do stuff
} catch (InterruptedException e) {
// end up
}
}
Please Refer to this for more details
Upvotes: 5
Reputation: 5341
Use a Boolean
flag.
For Thread safety, use AtomicBoolean
.
AtomicBoolean running = new AtomicBoolean(Boolean.TRUE);
In your run()
method check this flag in a while condition:
public void run(){
while(running){
...
}
}
When you want to stop this Thread
, change the running
to false
Upvotes: 0
Reputation: 832
Try something like
private void startActionPerformed(java.awt.event.ActionEvent evt) {
p=new Progress();
myThread=new Thread(p);
p.setLocationRelativeTo(null);
p.setVisible(true);
myThread.start();
}
private void stopActionPerformed(java.awt.event.ActionEvent evt) {
if(myThread!=null){
p.Terminate();
try {
myThread.join();
} catch (InterruptedException ex) {
Logger.getLogger(ClassA.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
////////////////////////////////////////////////////////////////
How it Works and Stopped!
int i;
volatile boolean running=true;
public void run(){
while(running){
for(i=0;i<=100;i++){
pro.setValue(i);
try {
Thread.sleep(200);
} catch (InterruptedException ex) {
Logger.getLogger(Progress.class.getName()).log(Level.SEVERE, null, ex);
return;
}
if(i==100){
Terminate();
break;
}
}
}
}
public void Terminate(){ running=false; }
/////////////////////////////////////////////////////////////////////
Upvotes: 0
Reputation: 140
Well, try this :
while(true) {
if (!c.isAlive() && b.isAlive()){
b.interrupt();
}
}
Upvotes: 0
Reputation: 568
Don't use Thread.stop() method, It's already deprecated, in this case you can handle the stopping of the b thread in your code.
For example:
class b extends Thread {
private volatile boolean stopped = false;
public void stop () {
stopped = true;
}
@Override
public void run() {
// PROGRESS BAR
while ( ! stopped ) {
// paint the progress bar
}
}
}
Upvotes: 2
Reputation: 11
You might want to take a look at this. You can use a flag or just use Thread.currentThread().interrupt()
, you can check if a thread is interrupted by calling Thread.isInterrupted()
on it.
Upvotes: 1
Reputation: 44740
Don't use .stop()
use interrupt()
instead
You need to check periodically in your b
thread if it gets interrupted, if interrupted , you can take proper actions -
if(b.isInterrupted()){
//end your work
}
--->
http://docs.oracle.com/javase/tutorial/essential/concurrency/interrupt.html
Upvotes: 2
Reputation: 44813
create a lockable object in your calling code
Boolean canRun = true;
c = new c();
when b
has finished set canRun
to false
periodically check value of canRun
in c
Upvotes: 0