public class Demo{
public static void main(String[] args) {
Thread t = new Thread(new MyRunnable());
t.setName("t");
t.start();
//睡眠5秒
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//中段t线程的睡眠
t.interrupt();
}
}
class MyRunnable implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"--->begin");
try {
Thread.sleep(1000*60);
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("测试");
}
System.out.println(Thread.currentThread().getName()+"--->end");
}
}
输出:
public class Demo{
public static void main(String[] args) {
Thread t = new Thread(new MyRunnable());
t.setName("t");
t.start();
//中段t线程的睡眠
t.interrupt();
}
}
class MyRunnable implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"--->begin");
try {
Thread.sleep(1000*60);
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("测试");
}
System.out.println(Thread.currentThread().getName()+"--->end");
}
}
输出:
这里的异常按理说应该在中间输出,但是跑到了后面。原因如下:
System.out.println();
System.err.println();
原文:https://www.cnblogs.com/yu011/p/13028749.html