62,623
社区成员
发帖
与我相关
我的任务
分享import java.util.Date;
public class TestInterruption {
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
}
}
}
class MyThread extends Thread {
private int n;
public void run() {
while (true) {
System.out.println(new Date());
try {
sleep(1000);
n++;
if(n==5)
break;
} catch (InterruptedException e) {
return;
}
}
}
}import java.util.Date;
public class TestInterruption {
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start();
try {
//sleep是静态方法,要用类名去调用
Thread.sleep(10000);
thread.interrupt();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class MyThread extends Thread {
public void run() {
while (true) {
System.out.println(new Date());
try {
sleep(1000);
} catch (InterruptedException e) {
return;
}
}
}
}