Reputation: 2206
My Google skills seem to have failed me, so apologies if this is a simple question.
I simply want to interrupt ("kill" it I guess) the thread which I am currently running.
My class implements Runnable, and inside the run() method I want the thread to interrupt if a certain condition is met.
Cheers! :)
Upvotes: 1
Views: 102
Reputation: 757
You have two choices:
return;
and most correct and recommended
Thread.currentThread().interrupt();
Upvotes: -1
Reputation: 4676
One other option depending on the context. Thread.interrupt will likely be better, but this one might be useful too by just introducing a cancel boolean inside your class.
public class MyLongRunningTask implements Runnable
{
private volatile boolean cancelled;
public void run() {
while (!cancelled) {
System.out.println("Do something...");
}
}
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
}
Upvotes: 2