Try this, it works for me.
public class Thread_Ex {
public static void main(String [] args) {
Thread t1 = new Thread(new MyRunnable(), "gayan");
Thread t2 = new Thread(new MyRunnable(), "suranga");
t1.start();
t2.start();
while (true) {
try {
System.in.read();
t1.interrupt();
t2.interrupt();
}
catch (Exception eee) {
}
}
}
}
class MyRunnable implements Runnable {
@Override
public void run() {
while(true) {
System.out.println(Thread.currentThread().getName());
try {
Thread.sleep(200);
}
catch (InterruptedException zz) {
}
if (Thread.interrupted()) {
System.out.println("interrupted");
break;
}
}
}
}