Source Code : Interrupt a thread.
Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com.
Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com.
Click Here or search from google with Libraries Name you get jar file related it
Interrupt a thread.
class MyThread implements Runnable {
public void run() {
System.out.println(Thread.currentThread().getName() + " starting.");
try {
Thread.sleep(30000);
for (int i = 1; i < 10; i++) {
if (Thread.interrupted()) {
System.out.println("interrupted.");
break;
}
System.out.print(".");
for (long x = 0; x < 1000; x++)
System.out.println("/");
}
} catch (Exception exc) {
System.out.println(Thread.currentThread().getName() + " interrupted.");
}
System.out.println(Thread.currentThread().getName() + " exiting.");
}
}
public class Main {
public static void main(String args[]) throws Exception {
Thread thrd = new Thread(new MyThread(), "MyThread #1");
Thread thrd2 = new Thread(new MyThread(), "MyThread #2");
thrd.start();
Thread.sleep(1000);
thrd.interrupt();
thrd2.start();
Thread.sleep(4000);
thrd2.interrupt();
}
}
Thank with us