Java Thread.interrupt()
Syntax
Thread.interrupt() has the following syntax.
public void interrupt()
Example
In the following code shows how to use Thread.interrupt() method.
class ThreadDemo implements Runnable {
/*from w w w. j a v a 2 s . co m*/
public void run() {
try {
while (true) {
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println(e.toString());
}
}
}
public class Main {
public static void main(String args[]) {
Thread t = new Thread(new ThreadDemo());
System.out.println("Executing " + t.getName());
t.start();
if (!t.interrupted()) {
t.interrupt();
}
// block until other threads finish
try {
t.join();
} catch (InterruptedException e) {
}
}
}