错误的线程中断
首先我们先介绍一种错误的线程中断
不正确的线程中止 ————Stop
Stop:中止线程,并且清楚监控器锁的信息,但是可能导致线程安全问题,JDK不建议用。
Destroy:JDK未实现该方法
现在我们都一个demo来说明stop导致线程安全问题
package demo2;
public class demo3 {
public static void main(String[] args) throws InterruptedException{
StopThread thread=new StopThread();
thread.start();
// 休眠1秒,确保i变量自增成功
Thread.sleep(1000);
// 暂停线程
thread.stop();//错误的终止
while (thread.isAlive()){
//确保线程已经终止
}//输出结果
thread.print();
}
}
package demo2;
public class StopThread extends Thread{
private int i=0,j=0;
@Override
public void run(){
synchronized (this){
// 增加同步锁,确保线程安全,实现原子性
++i;
try{
//休眠10秒,模拟耗时操作
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
++j;
}
}
/**打印i和j**/
public void print(){
System.out.println("i="+i+", j="+j);
}
}
上述代码的运行结果是 i=1,j=0。 没有实现 i , j 的同步增加,破坏了线程的安全。原因是stop强制把线程关闭了,所以现在JDK不建议使用stop来处理线程中断
正确的线程中断方式
方式一 interrupt
以异常的情况通知开发者,实现线程安全
如果目标线程在条用Object class的wait(),wait(long)或wait(long,int)方法,join(),join(long,int)或sleep(long,int)方法时被阻塞,那么interrupt会生效,该线程的中断状态将被清除,抛出InterruptedException异常
如果目标线程时被I/O或者NIO中的Channel所阻塞,同样,I/O操作会被中断或者返回特殊异常值。达到终止线程的目的。
如果以上条件都不满足,则会设置此线程的中断状态。
我们用一个demo来解释
package demo2;
public class demo3 {
public static void main(String[] args) throws InterruptedException{
StopThread thread=new StopThread();
thread.start();
// 休眠1秒,确保i变量自增成功
Thread.sleep(1000);
// 暂停线程
thread.interrupt();//正确终止
while (thread.isAlive()){
//确保线程已经终止
}//输出结果
thread.print();
}
}
package demo2;
public class StopThread extends Thread{
private int i=0,j=0;
@Override
public void run(){
synchronized (this){
// 增加同步锁,确保线程安全,实现原子性
++i;
try{
//休眠10秒,模拟耗时操作
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
++j;
}
}
/**打印i和j**/
public void print(){
System.out.println("i="+i+", j="+j);
}
}
上图结果已经实现的 i j 值的统一,上面的红色是InterruptedException抛出异常,以异常的情况通知开发者,实现线程安全,开发者可以根据异常来进行相对应的处理
方式二 标志位
代码逻辑中,增加一个判断,用来控制线程执行的中止。
package demo2;
public class demo4 extends Thread{
public volatile static boolean flag=true;
public static void main(String[] args)throws InterruptedException{
new Thread(()->{
try {
while (flag){
System.out.println("运行中");
Thread.sleep(1000L);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
//3秒之后,将状态标志位改为Flase,代表不继续运行
Thread.sleep(3000L);
flag=false;
System.out.println("程序运行结束");
}
}
上面这段代码就是采用flag标志位,来控制程序的中断,一开始flag是true,说明线程是正常运行的,之后flag=false;将线程中断掉,一来就实现了线程的中断
关于线程中断就先分享到这里,后续还有很多的Java系列课,尽情期待