class Thread3_1 extends Thread {
public static int count = 0;
String tname1;
public Thread3_1(String threadname) {
tname1 = threadname;
}
public void run() {
for (int i = 1; i <= 10; i++) {
System.out.println(tname1 + " : " + i);
count++;
if (count == 2) {
try {
sleep(1000);
count = 0;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
if (isInterrupted()) {
System.out.println("Stop Thread");
return;
}
}
}
class Thread3_2 extends Thread {
String tname2;
public Thread3_2(String threadname) {
tname2 = threadname;
}
@Override
public void run() {
for (int i = 1; i <= 10; i++) {
System.out.println(tname2 + " : " + i);
if (i == 5) {
Thread.currentThread().interrupt();
System.out.println("Going to interrupt Thread1");
}
}
}
}
给定后线程正在执行interrupt()