高性能编程-1.1.3线程中止

135 阅读1分钟

不正确的线程中止-stop

stop: 中止线程,并且清除监控器锁的信息,但是可能导致线程安全问题,JDK不建议使用。 Destroy:JDK未实现该方法

public class Demo3 {
    public static void main(String[] args) throws InterruptedException {
        StopThread thread = new StopThread();
        thread.start();
        Thread.sleep(1000);
        thread.stop();
//        thread.interrupt();
        while (thread.isAlive()) {
            //确保线程已经中止
        }//输出结果
        thread.print();
    }
}
public class StopThread extends Thread {
    private int i=0,j=0;

    @Override
    public void run(){
        synchronized (this){
            //增加同步锁,确保线程安全
            ++i;
            try {
                System.out.println("test");
                Thread.sleep(10000);
            }catch (Exception e){
                e.printStackTrace();
            }
            ++j;
        }
    }
    public void print(){
        System.out.println("i="+i+" j="+j);
    }
}

运行结果

test
i=1 j=0

从结果上看,没有保证同步代码块中数据的一致性,破坏了线程安全。

正确的线程终止:interrupt

如果目标线程在调用Object class的wait()、wait(long)或wait(long,int)方法,join()、join(long,int)或者sleep(long,int)方法时被阻塞,那么interrupt会生效,该线程的中断状态将被清除,抛出interruptException异常。

如果目标线程是被I/O或者中的Channel所阻塞,同样,I/O操作会被中断或者返回特殊异常值。达到终止线程的目的。

如果以上条件都不满足,则会设置此线程的的中断状态。 对于Demo3中的示例,stop改成interrupt后,最终输出为:"i=1,j=1",数据一致。 这里例子就不举了。

正确的线程终止:标志位

我们先通过代码例子引入:

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秒后,将状态标志位false,代表不继续运行
        Thread.sleep(3000L);
        flag = false;
        System.out.println("程序运行结束");

    }
}

运行结果

运行中
运行中
运行中
程序运行结束