Java中的Interrupt使用
初心
用interrupt中断程序
初步实现
public class InterruptionInJava implements Runnable{
@Override
public void run() {
while (true) {
if (Thread.currentThread().isInterrupted()) {
System.out.println("Yes!! I'm Interupted, but I'm still running");
} else {
}
}
// System.out.println("Oh, myGod!");
}
public static void main(String[] args) {
Thread thread = new Thread(new InterruptionInJava(), "testThread");
thread.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
System.out.println("Begin Interupt...");
thread.interrupt();
System.out.println("End Interupt...");
}
}
输出
Yes!! I'm Interupted, but I'm still running
Yes!! I'm Interupted, but I'm still running
Yes!! I'm Interupted, but I'm still running
Yes!! I'm Interupted, but I'm still running
Yes!! I'm Interupted, but I'm still running
Yes!! I'm Interupted, but I'm still running
Yes!! I'm Interupted, but I'm still running
.....
问题:虽然是被中断状态,但实际并未中断
interrupt说明
在java中主要有3个相关方法,interrupt(),isInterrupted()和interrupted()。
- interrupt(),在一个线程中调用另一个线程的interrupt()方法,即会向那个线程发出信号——线程中断状态已被设置。至于那个线程何去何从,由具体的代码实现决定。
- isInterrupted(),用来判断当前线程的中断状态(true or false)。
- interrupted()是个Thread的static方法,用来恢复中断状态(!!!)
解决不中断问题
处于被中断状态时,return 或 bread 中断线程
public class InterruptionInJava implements Runnable{
@Override
public void run() {
while (true) {
if (Thread.currentThread().isInterrupted()) {
System.out.println("Yes!! I'm Interupted, but I'm still running");
return;
} else {
}
}
}
public static void main(String[] args) {
Thread thread = new Thread(new InterruptionInJava(), "testThread");
thread.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
System.out.println("Begin Interupt...");
thread.interrupt();
System.out.println("End Interupt...");
}
}
等价开关
public class InterruptionInJava2 implements Runnable{
private volatile static boolean on = false;
@Override
public void run() {
while (!on) {
try {
System.out.println("begin Sleep");
Thread.sleep(10000000);
System.out.println("end Sleep");
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Oh, myGod!");
}
}
public static void main(String[] args) {
Thread thread = new Thread(new InterruptionInJava2(), "testThread");
thread.start();
try {
System.out.println("main Begin sleep");
Thread.sleep(5000);
System.out.println("main End sleep");
} catch (InterruptedException e) {
}
InterruptionInJava2.on = true;
}
}
使用静态变量on
但是在run方法中Thread.sleep(10000000),
Java中的Interrupt使用的更多相关文章
- java中的interrupt(),InterruptException和wait(),sleep()
标题中的几个概念大概设计到线程同步以及线程阻塞这两个概念.线程同步,就是同一时刻,只有一个线程能执行指定的代码:另外一个线程阻塞就是当前线程暂时停在某个位置,等待某个条件成立之后再继续往下面执行. ...
- Java中interrupt的使用
通常我们会有这样的需求,即停止一个线程.在java的api中有stop.suspend等方法可以达到目的,但由于这些方法在使用上存在不安全性,会带来不好的副作用,不建议被使用.具体原因可以参考Why ...
- java中interrupt、join、sleep、notify、notifyAll、wait详解
首先介绍一下中断概念:举个例子容易理解一点 例子:假如你正在给朋友写信,电话铃响了.这时,你放下手中的笔,去接电话.通话完毕,再继续写信.这个例子就表现了中断及其处理过程:电话铃声使你暂时中止当前的工 ...
- java中interrupt,interrupted和isInterrupted的区别
文章目录 isInterrupted interrupted interrupt java中interrupt,interrupted和isInterrupted的区别 前面的文章我们讲到了调用int ...
- java中的锁
java中有哪些锁 这个问题在我看了一遍<java并发编程>后尽然无法回答,说明自己对于锁的概念了解的不够.于是再次翻看了一下书里的内容,突然有点打开脑门的感觉.看来确实是要学习的最好方式 ...
- Java中的多线程你只要看这一篇就够了
学习Java的同学注意了!!! 学习过程中遇到什么问题或者想获取学习资源的话,欢迎加入Java学习交流群,群号码:279558494 我们一起学Java! 引 如果对什么是线程.什么是进程仍存有疑惑, ...
- java中的多线程
什么是多线程? 首先得知道什么是线程? 线程是一组指令的集合,或者是程序的特殊段,它可以在程序里独立执行.也可以把它理解为代码运行的上下文.所以线程基本上是轻量级的进程,它负责在单个程序里执行多任务. ...
- JAVA中关于并发的一些理解
一,JAVA线程是如何实现的? 同步,涉及到多线程操作,那在JAVA中线程是如何实现的呢? 操作系统中讲到,线程的实现(线程模型)主要有三种方式: ①使用内核线程实现 ②使用用户线程实现 ③使用用户线 ...
- Java中的wait和sleep
sleep()和wait() 首先,Java中的多线程是一种抢占式的机制,而不是分时机制.抢占式的机制是有多个线程处于可运行状态,但是只有一个线程在运行. 这种机制决定了,对于同一对象的多线程访问,必 ...
随机推荐
- PHP安装+使用
curl -s http://php-osx.liip.ch/install.sh | bash -s 5.4 ...... Extracting usr/local/php5-5 ...
- EC20指令测试
cat /dev/ttyUSB2 & echo -e "AT+CGMM\r\n" >/dev/ttyUSB2 //输出模块型号 echo -e "AT+ ...
- mac下crontab定时任务使用
这篇文章的作用 BREAK TIME 本地pc配置定时任务,开机后每隔一小时执行一次,open这个页面,休息半分钟 cron创建备忘 首先创建定时任务 crontab -e 0 */ * * * op ...
- Connect To Ubuntu 16.04 / 17.10 / 18.04 Desktop Via Remote Desktop Connection (RDP) With Xrdp
[1] https://websiteforstudents.com/connect-to-ubuntu-16-04-17-10-18-04-desktop-via-remote-desktop-co ...
- 【python-appium】模拟手机按键搜索异常
执行代码的过程中运行self.driver.press_keycode(84)设备没反映,则需要关闭#desired_caps["unicodeKeyboard"] = " ...
- 【Solidity】学习(4)
solidity函数修饰符 view 没有改变任何值或者写任何东西.只能读取,不能修改 function sayHello() public view returns (string) { } pu ...
- Linux系统安装 OpenSSL两种方法
OpenSSL是一个开源的ssl技术,由于安装pytbull,需要安装openssl,并下载对应的版本下载地址:https://www.openssl.org/source/ 方法一,编译安装Open ...
- 包建强的培训课程(12):iOS深入学习(内存管理、Block和GCD等)
@import url(/css/cuteeditor.css); @import url(http://i.cnblogs.com/Load.ashx?type=style&file=Syn ...
- 字符编码那点事:快速理解ASCII、Unicode、GBK和UTF-8
原作者:阮一峰(ruanyifeng.com),现重新整理发布,感谢原作者的无私分享. 1.引言 今天中午,我突然想搞清楚 Unicode 和 UTF-8 之间的关系,就开始查资料. 这个问题比我想象 ...
- Python爬虫2-检测编码(使用chardet)
GitHub代码练习地址:https://github.com/Neo-ML/PythonPractice/blob/master/SpiderPrac02_chardet.py 网页编码问题解决 c ...