67,512
社区成员
发帖
与我相关
我的任务
分享
class Mythread10 extends Thread {
@Override
public void run() {
super.run();
try {
for (int i = 0; i < 5000000; i++) {
if (Thread.interrupted()) {
System.out.println("线程终止:" + i + "---" + interrupted());
throw new InterruptedException();
}
System.out.println("i=[" + i + "]");
}
System.out.println("for after....");
} catch (InterruptedException e) {
System.out.println("catch ////");
e.printStackTrace();
}
}
}
main 方法中 对应代码
Mythread10 mythread10 = new Mythread10();
Thread thread = new Thread(mythread10);
thread.start();
Thread.sleep(1000);
thread.interrupt();
@Override
public void run() {
super.run();
System.out.println("进入run方法....");
for (int i = 0; i < 5000000; i++) {
try {
if (Thread.interrupted()) {
System.out.println("线程终止:" + i + "---" + interrupted());
throw new InterruptedException();
}
System.out.println("i=[" + i + "]");
System.out.println("for after....");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}