线程停止方法一:
已经不推荐使用
学习源代码:
package thread8;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Run {
public static void main(String[] args) throws InterruptedException {
ThreadA threadA = new ThreadA();
threadA.start();
Thread.sleep(10000);
System.out.println((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())) + "suspend 暂停");
threadA.suspend();
Thread.sleep(10000);
System.out.println((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())) + "resume 恢复");
threadA.resume();
Thread.sleep(10000);
System.out.println((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())) + "stop 停止");
threadA.stop();
}
}
class ThreadA extends Thread {
@Override
public void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())) + "ThreadA 执行中……");
}
}
}
执行结果:
2022-01-17 15:39:53ThreadA 执行中……
2022-01-17 15:39:54ThreadA 执行中……
2022-01-17 15:39:55ThreadA 执行中……
2022-01-17 15:39:56ThreadA 执行中……
2022-01-17 15:39:57ThreadA 执行中……
2022-01-17 15:39:58ThreadA 执行中……
2022-01-17 15:39:59ThreadA 执行中……
2022-01-17 15:40:00ThreadA 执行中……
2022-01-17 15:40:01ThreadA 执行中……
2022-01-17 15:40:02suspend 暂停
2022-01-17 15:40:12resume 恢复
2022-01-17 15:40:12ThreadA 执行中……
2022-01-17 15:40:13ThreadA 执行中……
2022-01-17 15:40:14ThreadA 执行中……
2022-01-17 15:40:15ThreadA 执行中……
2022-01-17 15:40:16ThreadA 执行中……
2022-01-17 15:40:17ThreadA 执行中……
2022-01-17 15:40:18ThreadA 执行中……
2022-01-17 15:40:19ThreadA 执行中……
2022-01-17 15:40:20ThreadA 执行中……
2022-01-17 15:40:21ThreadA 执行中……
2022-01-17 15:40:22stop 停止
方式二:
设置线程结束标志
package thread3;
public class Run {
public static void main(String[] args) throws InterruptedException {
MyThread myThread = new MyThread();
myThread.start();
Thread.sleep(1000);
System.out.println("线程结束标志");
myThread.setFlag(false);
System.out.println("线程已结束");
}
}
class MyThread extends Thread {
//线程停止标志
private volatile boolean flag = true;
private volatile int count = 0;
public void setFlag(boolean flag) {
this.flag = flag;
}
@Override
public void run() {
while (flag) {
count++;
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("执行中,count:" + count);
}
}
}
执行结果:
执行中,count:1
执行中,count:2
执行中,count:3
线程结束标志
线程已结束
执行中,count:4
方法三:
中断方法
package interrupted;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Run {
public static void main(String[] args) throws InterruptedException {
System.out.println("Thread main 开始,时间:" + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date())));
ThreadA threadA = new ThreadA();
threadA.start();
Thread.sleep(3);
threadA.interrupt();
System.out.println("Thread main 结束,时间:" + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date())));
}
}
class ThreadA extends Thread {
@Override
public void run() {
while (true) {
if (this.isInterrupted()) {
System.out.println("执行中ThreadA,中断结束了," + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date())));
break;
}
System.out.println("执行中ThreadA,时间:" + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date())));
}
}
}
正常停止,执行结果:
Thread main 开始,时间:2022-01-17 16:59:40:199
执行中ThreadA,时间:2022-01-17 16:59:40:202
执行中ThreadA,时间:2022-01-17 16:59:40:203
执行中ThreadA,时间:2022-01-17 16:59:40:204
执行中ThreadA,时间:2022-01-17 16:59:40:204
执行中ThreadA,时间:2022-01-17 16:59:40:204
执行中ThreadA,时间:2022-01-17 16:59:40:204
执行中ThreadA,时间:2022-01-17 16:59:40:204
Thread main 结束,时间:2022-01-17 16:59:40:205
执行中ThreadA,时间:2022-01-17 16:59:40:205
执行中ThreadA,中断结束了,2022-01-17 16:59:40:205
修改代码:
class ThreadA extends Thread {
@Override
public void run() {
while (true) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println("执行中ThreadA,中断异常结束了," + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date())));
e.printStackTrace();
break;
}
if (this.isInterrupted()) {
System.out.println("执行中ThreadA,中断结束了," + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date())));
break;
}
System.out.println("执行中ThreadA,时间:" + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date())));
}
}
}
执行结果:
Thread main 开始,时间:2022-01-17 17:04:00:160
Thread main 结束,时间:2022-01-17 17:04:00:165
执行中ThreadA,中断异常结束了,2022-01-17 17:04:00:165
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at interrupted.ThreadA.run(Run.java:23)
总结:当线程被 sleep() 休眠时,如果被中断,这会就抛出这个异常