停止线程的多种方法

例一:stop强制停止线程

public class Thread06 extends Thread {
    private int i = 0;

    @Override
    public void run() {
        super.run();
        try{
            while(true) {
                i ++;
                System.out.println("i=" + (i + 1));
                Thread.sleep(1000);
            }
        } catch(InterruptedException e) {
            e.printStackTrace();
        }
    }
}

public static void main(String[] args) {
    thread06();
}

private static void thread06() {
    try{
        Thread06 thread = new Thread06();
        thread.start();
        Thread.sleep(9000);
        thread.stop();
    } catch(InterruptedException e) {
        e.printStackTrace();
    }
}

输出结果

i=2
i=3
i=4
i=5
i=6
i=7
i=8
i=9
i=10

thread.start();方法启动一个新的线程,该线程与main线程并行运行,main线程睡眠9秒后执行了thread.stop();方法,thread线程被main线程强制停止。

stop方法已被废弃,因为如果强制让线程停止则可能使一些清理的工作得不到完成。另外一个情况就是对锁定的对象进行了“解锁”,导致数据得不到同步的处理,出现数据不一致的结果。

例二:线程睡眠(sleep)时停止(interrupt)线程

public class Thread05 extends Thread {
    @Override
    public void run() {
        super.run();
        try{
            for(int i = 0; i < 1000; ++ i) {
                System.out.println("i="+(i+1));
            }
            System.out.println("run begin!");
            Thread.sleep(200000);
            System.out.println("run end!");
        } catch (InterruptedException e) {
            System.out.println("先停止,再遇到了sleep!进入catch");
            e.printStackTrace();
        }
    }
}


public static void main(String[] args) {
    thread05();
}

private static void thread05() {
    Thread05 thread = new Thread05();
    thread.start();
    thread.interrupt();
    log("end!====================");
}

输出结果:

end!====================
i=1
i=2
i=3
......
i=999
i=1000
run begin!
先停止,再遇到了sleep!进入catch
java.lang.InterruptedException: sleep interrupted
    at java.lang.Thread.sleep(Native Method)
    at com.qbian.thread.Thread05.run(Thread05.java:12)

线程在睡眠时(sleep)调用它的停止方法(interrupt)会停止线程并抛出InterruptedException异常。

例三、调用interrupt方法停止正在运行的线程,break跳出循环继续执行后续代码

public class Thread02 extends Thread{
    @Override
    public void run() {
        super.run();
        for(int i = 0; i < 500000; ++ i) {
            if(this.interrupted()) {
                System.out.println("已经是停止状态了!我要退出了!");
                break;
            }
            System.out.println("i=" + ( i + 1));
        }
        System.out.println("我被输出,线程并没有完全停止!");
    }
}


public static void main(String[] args) {
    thread02();
}

private static void thread02() {
    try{
        Thread02 thread = new Thread02();
        thread.start();
        Thread.sleep(2000);
        thread.interrupt();
    } catch(InterruptedException e) {
        log("main catch");
        e.printStackTrace();
    }
    log("end!");
}

输出结果:

......
i=245329
i=245330
i=245331
i=245332
i=245333
i=245334
end!
已经是停止状态了!我要退出了!
我被输出,线程并没有完全停止!

break只是跳出了for循环,并不会真正的停止线程,run方法内的后续代码会继续执行。

例四、调用interrupt方法停止正在运行的线程,并抛出异常阻止线程后续的代码执行

public class Thread03 extends Thread {
    @Override
    public void run() {
        super.run();
        try{
            for(int i = 0; i < 500000; ++ i) {
                if(this.interrupted()) {
                    System.out.println("已经是停止状态了!我要退出了!");
                    throw new InterruptedException();
                }
                System.out.println("i=" + ( i + 1));
            }
            System.out.println("因为抛出了异常,所以我不会被输出!");
        } catch(InterruptedException e) {
            System.out.println("进thread03类的run方法中的catch了!");
            e.printStackTrace();
        }
    }
}


public static void main(String[] args) {
    thread03();
}

private static void thread03() {
    try{
        Thread03 thread = new Thread03();
        thread.start();
        Thread.sleep(2000);
        thread.interrupt();
    } catch(InterruptedException e) {
        log("main catch");
        e.printStackTrace();
    }
    log("end!");
}

输出结果:

......
i=278267
i=278268
i=278269
i=278270
i=278271
已经是停止状态了!我要退出了!
end!
进thread03类的run方法中的catch了!
java.lang.InterruptedException
    at com.qbian.thread.Thread03.run(Thread03.java:11)

主线程调用interrupt方法停止一个正在运行的线程,在被停止的线程内部判断自身状态是否已经停止this.interrupted(),停止的话就抛出异常,可以阻止后续代码的执行,直接进入catch块内。

例五、使用interrupt和return停止线程并阻止后续代码执行

public class Thread07 extends Thread{
    @Override
    public void run() {
        super.run();
        while(true) {
            if(this.isInterrupted()) {
                System.out.println("停止了!后面的时间将不会被输出!");
                return;
            }
            System.out.println("timer=" + System.currentTimeMillis());
        }
    }
}


public static void main(String[] args) {
    thread07();
}

private static void thread07() {
    try {
        Thread07 thread = new Thread07();
        thread.start();
        Thread.sleep(2000);
        thread.interrupt();
    } catch (InterruptedException e) {
        log("main catch");
        e.printStackTrace();
    }

}

输出结果:

......
timer=1496420116082
timer=1496420116082
timer=1496420116082
停止了!后面的时间将不会被输出!

推荐使用抛出异常的方式停止线程执行,因为在catch块中还可以将异常向上抛,使线程停止的事件得到传播。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值