本文主要是介绍如何安全的停止一个线程,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
如何停止中断运行中的线程?
1.1通过一个volatile变量实现
package com.bilibili.juc.interrupt;import java.util.concurrent.TimeUnit;public class InterruptDemo {static volatile boolean isStop = false; // volatile修饰的变量具有可见性public static void main(String[] args) {new Thread(() -> {while (true) {if (isStop) {System.out.println(Thread.currentThread().getName() + "\t isStop的值被改为true,程序停止");break;}System.out.println(Thread.currentThread().getName() + "--------hello volatile--------");}}, "t1").start();try {TimeUnit.MILLISECONDS.sleep(10);} catch (InterruptedException e) {e.printStackTrace();}new Thread(() -> isStop = true, "t2").start();}}输出结果:
t1--------hello volatile--------
t1--------hello volatile--------
t1--------hello volatile--------
t1--------hello volatile--------
...
t1--------hello volatile--------
t1--------hello volatile--------
t1--------hello volatile--------
t1--------hello volatile--------
t1 isStop的值被改为true,程序停止
1.2通过AutomicBoolean
package com.bilibili.juc.interrupt;import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;public class InterruptDemo2 {static AtomicBoolean atomicBoolean = new AtomicBoolean(false);public static void main(String[] args) {new Thread(() -> {while (true) {if (atomicBoolean.get()) {System.out.println(Thread.currentThread().getName() + "\t atomicBoolean的值被改为true,程序停止");break;}System.out.println(Thread.currentThread().getName() + "--------hello atomicBoolean--------");}}, "t1").start();try {TimeUnit.MILLISECONDS.sleep(10);} catch (InterruptedException e) {e.printStackTrace();}new Thread(() -> atomicBoolean.set(true), "t2").start();}}输出结果:
t1--------hello atomicBoolean--------
t1--------hello atomicBoolean--------
t1--------hello atomicBoolean--------
t1--------hello atomicBoolean--------
...
t1--------hello atomicBoolean--------
t1--------hello atomicBoolean--------
t1--------hello atomicBoolean--------
t1--------hello atomicBoolean--------
t1 atomicBoolean的值被改为true,程序停止
1.3通过Thread类自带的中断API实例方法实现
在需要中断的线程中不断监听中断状态,一旦发生中断,就执行相应的中断处理业务逻辑stop线程。
package com.bilibili.juc.interrupt;import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;public class InterruptDemo3 {public static void main(String[] args) {Thread t1 = new Thread(() -> {while (true) {if (Thread.currentThread().isInterrupted()) {System.out.println(Thread.currentThread().getName() + "\t isInterrupted的值被改为true,程序停止");break;}System.out.println(Thread.currentThread().getName() + "--------hello interrupt api--------");}}, "t1");t1.start();System.out.println("--------t1的默认中断标识位:" + t1.isInterrupted());try {TimeUnit.MILLISECONDS.sleep(10);} catch (InterruptedException e) {e.printStackTrace();}new Thread(t1::interrupt, "t2").start(); // t2向t1发出协商,将t1线程的中断状态为true
// t1.interrupt(); // 也可以自己调用interrupt()方法将自己线程的中断状态为true}}输出结果:
--------t1的默认中断标识位:false
t1--------hello interrupt api--------
t1--------hello interrupt api--------
t1--------hello interrupt api--------
t1--------hello interrupt api--------
...
t1--------hello interrupt api--------
t1--------hello interrupt api--------
t1--------hello interrupt api--------
t1--------hello interrupt api--------
t1 isInterrupted的值被改为true,程序停止
这篇关于如何安全的停止一个线程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!