Java停止线程的方式
1、使用中断标志位
public class StopThreadTest extends Thread {private boolean exit = false;@Overridepublic void run() {while (!exit) {try {System.out.println("i am running,please wait a moment");Thread.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}}}public static void main(String[] args) {try {StopThreadTest threadTest = new StopThreadTest();threadTest.start();Thread.sleep(4000);threadTest.exit = true;} catch (InterruptedException e) {e.printStackTrace();}}
}
2、 使用 interrupt() 中断线程
严格的说,线程中断并不会使线程立即退出,而是给线程发送一个通知,告知目标线程,有人希望你退出了!至于目标线程接收到通知之后如何处理,则完全由目标线程自己决定
线程阻塞状态中如何中断?
public class StopThreadTest{public static void main(String[] args) throws InterruptedException {Thread thread = new Thread() {@Overridepublic void run() {while (true){System.out.println("i am running");try {TimeUnit.SECONDS.sleep(100);} catch (InterruptedException e) {
// this.interrupt();e.printStackTrace();}if (Thread.currentThread().isInterrupted()){System.out.println("i am exit");break;}}}};thread.start();TimeUnit.SECONDS.sleep(1);thread.interrupt();}
}
运行上面的代码,发现程序无法终止
sleep方法由于中断而抛出异常之后,线程的中断标志会被清除(置为false),所以在异常中需要执行this.interrupt()方法,将中断标志位置为true
public class StopThreadTest{public static void main(String[] args) throws InterruptedException {Thread thread = new Thread() {@Overridepublic void run() {while (true){System.out.println("i am running");try {TimeUnit.SECONDS.sleep(100);} catch (InterruptedException e) {this.interrupt();e.printStackTrace();}if (Thread.currentThread().isInterrupted()){System.out.println("i am exit");break;}}}};thread.start();TimeUnit.SECONDS.sleep(1);thread.interrupt();}
}
调用线程的interrupt()实例方法,线程的中断标志会被置为true
当线程处于阻塞状态时,调用线程的interrupt()实例方法,线程内部会触发InterruptedException异常,并且会清除线程内部的中断标志(即将中断标志置为false)
public class StopThreadTest {/*** 通过interrupt()方式进行中断,同时运用了volatile,保证了flag变量在主线程与T1线程可见性*/public volatile static boolean flag = true;public static class T1 extends Thread {public T1(String name) {super(name);}@Overridepublic void run() {System.out.println("线程 " + this.getName() + " in");while (flag) {}System.out.println("线程 " + this.getName() + " stop");}}public static void main(String[] args) throws InterruptedException {T1 cp = new T1("cp");cp.start();TimeUnit.SECONDS.sleep(1);flag = false;} }
Java停止线程的方式相关推荐
- java 创建线程的方式
Java创建线程的方式 1 官方API里面的说法 通过官方 API 可以发现创建线程的方式有两种,第一种是通过继承 Thread 类,重写 run 方法:第二种是通过实现 Runnable 接口,创建 ...
- 想不到吧,Java创建线程的方式只有一种
目录 前言 继承Thread方式 实现Runnable接口 实现callable接口 总结 前言 看到这个标题的小伙伴先别着急喷我--在面试的时候,我们经常会被问到这种基础题:Java创建线程的方式有 ...
- Java停止线程的3种方式
在Java中有以下3种方式终止正在运行的线程: 使用退出标志,使线程正常退出: 使用stop()方法强行终止线程,不推荐使用该方法,JDK已声明弃用: 使用interrupt方法中断线程. 使用标志位 ...
- Java创建线程的方式
Java中线程的创建有四i种方式: 1. 通过继承Thread类,重写Thread的run()方法,将线程运行的逻辑放在其中 2. 通过实现Runnable接口,实例化Thread类 3.应用程序 ...
- 【Java】线程创建方式:Callable接口 / 使用线程池
面试题:创建多线程有有哪几种方式? 回答:4种. 实现Runnable接口(重写Run方法) 继承Thread类 实现Callable接口(重写Call方法,与Run不同的是,Call方法有返回值) ...
- java创建线程的方式到底有几种?(详解)
创建线程的方式到底有几种? 一,创建多线程的方式 1,官方解释 2,实现Runnable接口 3,继承Thread类 3,二者区别 3.1,本质区别 3.2,优先考虑使用第一种 二,误以为是创建线程的 ...
- java实现线程的方式_java多线程实现的四种方式
java多线程实现的四种方式1.继承Thread类,重写run方法(其实Thread类本身也实现了Runnable接口) 2.实现Runnable接口,重写run方法 3.实现Callable接口,重 ...
- java 停止线程播放音频_Notification 播放 关闭 声音----转载
NotificationPlayer.java 定义一个播放Notification声音的player,本质上仍然是一个MediaPlayer,这个是多线程编程的很好的例子 public class ...
- 线程创建方式3-实现 callable接口(Java)
线程创建方式3-实现 callable接口(Java) 线程创建方式三:实现 callable接口 callable的好处: 1.可以定返回值 2.可以抛出异常 package src.Demo02; ...
最新文章
- 将request中的所有参数存放到自定义的map中
- Oracle語句大全
- Word那些你不知道的快捷功能
- 用户id可以出现在url中吗_下载Google Drive中的文件
- 删除microsoft_如何从您的Microsoft帐户中删除设备
- html 选择不能重复,人生,就是一次无法重复的选择(深度好文)
- 怎么修改提交git是的用户名_Git 修改用户名以及提交邮箱
- 【数据结构1.3笔记】研究内容
- 单例设计模式共享数据分析、解决,call_once
- Python字符串expandtabs()
- mysql 字符串某个位置_mysql 定位字符串的位置
- Linux (转)解析 xinetd.conf
- python下载安装教程
- 企业微信邮箱登录入口在哪里?
- 最简单快速合并B站缓存视频和音频(video.m4s,audio.m4s)
- Linux history命令
- 将java 文件夹里面的.java 文件 拷贝到temp文件夹下,并且修改后缀名为.txt
- 漫威超级争霸战辅助脚本,快速刷星币打竞技场副本
- prophet outliers异常值
- 转:授之于鱼还是授之以渔,金蝶给了企业SaaS一个完整答案
热门文章
- java学习(156):反序列化
- java常用jar_常用jar包含义
- 宁波python学习_python学习第五天
- python selenium 处理弹窗_python+selenium 抓取弹出对话框信息
- bigdecimal 保留两位小数_一律使用 BigDecimal,避免后患?
- android 同根动画_android 动画系列 (1) - tween 动画(view动画)
- Java写入的常用技巧
- 南京市儿童医院用医保身份(医保通道)网上预约挂号以及取号、付费看病流程...
- Python:IndentationError: unexpected indent
- 关于 ASP.NET MVC 中的视图生成