优雅的线程中止
程序员文章站
2024-03-13 16:43:45
...
不正确的线程中止-Stop
Stop:中止线程,并清除监控器锁的信息,但是可能导致线程安全问题,JDK不建议使用
线程安全问题:当子线程未执行完时在主线程使用stop终止了子线程会导致线程安全问题
代码示例:
package com.hzw;
public class Demo2 {
public static void main(String[] args) throws InterruptedException {
StopThread thread = new StopThread();
thread.start();
Thread.sleep(1000L);
thread.stop();
while (thread.isAlive()) {
//确保线程已经中止
}
thread.print();
}
}
package com.hzw;
public class StopThread extends Thread{
private int i = 0, j = 0;
@Override
public void run() {
synchronized (this) {
++i;
try {
Thread.sleep(10000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
++j;
}
}
public void print() {
System.out.println("i=" + i + "j=" + j);
}
}
正确的线程中止-interrupt
做了什么事
-
发出线程中断信号,修改线程中断状态
isInterrupted
线程中断状态不是线程状态,中断状态默认为false,interrupt方法会发出线程中断信号将其修改为true
-
中断阻塞,继续执行线程
当interrupt方法遇到阻塞方法时会打断阻塞并抛出
java.lang.InterruptedException
异常,同时中断信号会失效继续执行线程。即此时线程中断状态isInterrupted依旧为false
此时如果依旧希望线程中断,一般会在异常处理代码块(catch)中再次调用interrupt方法
示例代码:package com.study.interrupt; public class Demo3_interrupt { public static void main(String[] args) throws InterruptedException { Thread testThread = new Thread(){ @Override public void run() { while (true){ try { System.out.println("runing..."); Thread.sleep(1000L); } catch (InterruptedException e) { e.printStackTrace(); //当线程处于 Waiting、TimedWaiting状态, //执行interrupted方法后,会从Waiting、TimedWaiting中退出 //并且isInterrupted=true的信号被擦除 System.out.println(Thread.currentThread().getState()); boolean isInterrupted = Thread.currentThread().isInterrupted() System.out.println("isInterrupted:" + isInterrupted); } } } }; testThread.start(); Thread.sleep(5000); testThread.interrupt(); } }
-
中断线程禁用,继续执行线程
示例代码:
package com.hzw.subject1.video; import java.util.concurrent.locks.LockSupport; public class Demo7 { public static void main(String[] args) throws InterruptedException { Thread testThread = new Thread(){ @Override public void run() { while (true){ System.out.println("running..."); LockSupport.park(); //当调用 interrupt方法后,这个park将被打断 System.out.println("调用interrupt方法后,park被打断..."); } } }; testThread.start(); Thread.sleep(5000); testThread.interrupt(); } }
总结:interrupt方法会向线程中发出一个打断信号,当信号遇到线程中的阻塞方法时会打断阻塞,如果没有遇到阻塞方法则会“软打断”线程(不直接打断线程,只是将线程中断状态改为true)
如何终止线程
package com.hzw.subject1.video;
public class Demo7 {
public static void main(String[] args) throws InterruptedException {
Thread testThread = new Thread(){
@Override
public void run() {
while (!Thread.currentThread().isInterrupted()){
try {
System.out.println("runing...");
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
Thread.currentThread().interrupt();
}
}
}
};
testThread.start();
Thread.sleep(10000);
testThread.interrupt();
}
}
正确的线程中止-标志位
增加一个判断,用来控制线程的中止
package com.hzw;
public class Demo3 {
public volatile static boolean flag = true;
public static void main(String[] args) throws InterruptedException {
new Thread(new Runnable() {
@Override
public void run() {
while (flag) {
System.out.println("运行中");
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
Thread.sleep(3000L);
flag = false;
System.out.println("程序运行结束");
}
}
上一篇: java POI解析Excel 之数据转换公用方法(推荐)
下一篇: Sass(二)如何安装Sass