正确停止线程(四)
1.使用interrupt关闭线程
/**
* 使用interrupt关闭线程,注意不可强制,而是要判断true如果当前线程已被中断; false除此以外。!Thread.currentThread().isInterrupted()
*/
public class RightWayStopThreadWithoutSleep implements Runnable{
@Override
public void run() {
int num = 0;
while (!Thread.currentThread().isInterrupted() && num<=Integer.MAX_VALUE /2){
if(num%10000==0){
System.out.println(num+"是1000的倍数");
}
num++;
}
System.out.println("结束啦");
}
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(new RightWayStopThreadWithoutSleep());
thread.start();
Thread.sleep(1000);
thread.interrupt();
}
}
2.堵塞状态中断的方式是抛出异常:
1.循环里面使用了Thread.sleep(1000);代表每次运行都延迟1s,最下面的5s是整个线程运行5s然后会抛出异常结束线程
2.循环里面可以不用Thread.currentThread().isInterrupted()来判断线程是否要到点关闭了
/**
* 如果在执行过程中,每次循环都会调用sleep或wait等方法,那么不需要每次迭代都检查是否已中断
*/
public class RightWayStopThreadWithSleepEveryLoop {
public static void main(String[] args) throws InterruptedException {
Runnable runnable = ()->{
int num = 0;
try {
while (num<=10000000){
if (num%10 ==0){
System.out.println(num+"是10的倍数");
}
num+=10;
Thread.sleep(1000);
System.out.println("hhh");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
};
Thread thread = new Thread(runnable);
thread.start();
Thread.sleep(5000);
thread.interrupt();
}
}
3.把try-catch放在while里面堵塞状态中断会失效:
4.把try-catch放在while里面堵塞状态中断会失效怎么办?:1.传递中断
1.这里try-catch还是在while里面,最好是在catch里面做出选择停止线程,这个demo是把休息时间放在了throwInMethod这个方法里面,也try-catch了,但是呢
/**
* 最佳时间 catch了InterruptedEcxetion之后的优先选择
* :在方法签名中抛出异常
* 那么在run()就会强制try/catch
*/
public class RightWayStopThreadInProd implements Runnable{
@Override
public void run() {
while (true && !Thread.currentThread().isInterrupted()){
System.out.println("我来啦");
new RightWayStopThreadInProd().throwInMethod();
}
}
private void throwInMethod(){
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(new RightWayStopThreadInProd());
thread.start();
Thread.sleep(5000);
thread.interrupt();
}
}
2.这里try-catch还是在while里面,最好是在catch里面做出选择停止线程,这个demo是把休息时间放在了throwInMethod这个方法里面,也try-catch了,但是呢try-catch是在使用的地方try-catch的这样可以根据不同的需求做不同的处理
/**
* 最佳时间 catch了InterruptedEcxetion之后的优先选择
* :在方法签名中抛出异常
* 那么在run()就会强制try/catch
*/
@Slf4j
public class RightWayStopThreadInProd implements Runnable{
@Override
public void run() {
while (true && !Thread.currentThread().isInterrupted()){
System.out.println("我来啦");
try {
new RightWayStopThreadInProd().throwInMethod();
} catch (InterruptedException e) {
log.error("我在这中断了,我要看我跳出来了之后干嘛",e.getMessage());
break;
}
}
}
private void throwInMethod() throws InterruptedException {
Thread.sleep(2000);
}
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(new RightWayStopThreadInProd());
thread.start();
Thread.sleep(5000);
thread.interrupt();
}
}
5.把try-catch放在while里面堵塞状态中断会失效怎么办?:2.恢复中断
既然是因为每次catch之后会把interrupt给清掉,导致无法获取是否中断,那么在catch中:Thread.currentThread().interrupt();实现中断就可以了
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~