Reputation:
In my current code I am creating a thread that implements runnable
. I am starting it in the main then letting the main interrupt it. However it once it is interrupted it continues to run. I would like the thread to completely stop.
Thread Class:
public class MyThread implements Runnable {
@Override
public void run() {
// string relating to the threads name
// Note that you actually set the threads name in the main
String threadName = Thread.currentThread().getName();
System.out.println(threadName + " is now started...");
System.out.println(threadName + " about to count down...");
for (int loop = 100; loop > 1; loop--) {
try {
//for 0.5 seconds
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println(threadName + " was Interupted!");
}
System.out.println(loop);
}
}
}
Main:
public class Runner {
public static void main(String[] args) {
//Create thread from class
MyThread myt= new MyThread();
//Passing in the thread from class and also naming it
Thread t= new Thread(myt, "My Thread");
//Actually starts the thread
t.start();
//Main thread has a sleep for 5 seconds then interrupts t
try {
Thread.sleep(5000);
t.interrupt();
System.out.println("Interupting...");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Output (note how thread does not stop):
My Thread is now started...
My Thread about to count down...
100
99
98
97
96
95
94
93
92
Interupting...
java.lang.InterruptedException: sleep interrupted
My Thread was Interupted!
91
at java.lang.Thread.sleep(Native Method)
at threads.MyThread.run(MyThread.java:22)
at java.lang.Thread.run(Unknown Source)
90
89
88
87
86
85
Upvotes: 0
Views: 189
Reputation: 4306
Just break
out of the loop:
catch (InterruptedException e) {
e.printStackTrace();
System.out.println(threadName + " was Interupted!");
Thread.currrentThread().interrupt();
break;
}
Upvotes: 3
Reputation: 46841
Simply use a boolean
flag that will be checked before doing any operation in the run()
method and based on its state do whatever is needed. Make sure it should be volatile
.
simply call myt.setRunning(false)
instead of t.interrupt()
Sample cde:
class MyThread implements Runnable {
private volatile boolean isRunning=true;
@Override
public void run() {
for (int loop = 100; loop > 1; loop--) {
if(!isRunning){
// break or do whatever is needed here
}
}
}
}
Upvotes: 2
Reputation: 54791
For such a repeating task you could also use a TimerTask
:
Just note this code won't stop at zero.
public class CountdownTask extends TimerTask {
private int from;
public CountdownTask(int from) {
this.from = from;
}
@Override
public void run() {
System.out.println(from--);
}
}
private void example() throws InterruptedException {
Timer timer = new Timer("Countdown");
timer.schedule(new CountdownTask(100), 0, 500);
Thread.sleep(2000); //do something
timer.cancel(); //stop task
}
Upvotes: 0
Reputation: 81694
Just write the run() method correctly, so that it breaks out of the loop when interrupted, rather than just continuing.
Thread does have a stop() method , but it's deprecated for a reason; it can leave things in an inconsistent and potentially unstable state. Don't use it!
Upvotes: 0