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中的多线程是一种抢占式的机制,而不是分时机制.抢占式的机制是有多个线程处于可运行状态,但是只有一个线程在运行. 这种机制决定了,对于同一对象的多线程访问,必 ...
随机推荐
- java基本类型的默认值
基本类型 默认值 取值范围 (最大/最小) 字节数 二进制位数 byte 0 127(2^7-1) -128(-2^7) 1byte 8bit short 0 32767(2^15 - 1) -327 ...
- POI对Excel的操作
1. 先导包 commons-io-2.6.jar包,用于对文件的操作. 下载地址:http://commons.apache.org/proper/commons-io/download_io.cg ...
- Office Web addin 踩坑计:替换后台网站为MVC框架时遇到的问题
Office Web Addin 模板程序的后台本质上是一个网站,你在调试的时候可以发现它的进程是一个32位的IE进程 所以可以把它替换成Asp.net的网站. 替换方法: 1.点击WordRevie ...
- flask-钩子函数&g对象
常用钩子函数 在Flask中钩子函数是使用特定的装饰器装饰的函数.钩子函数可以在正常执行的代码中,插入一段自己想要执行的代码.那么这种函数就叫做钩子函数.(hook) before_first_req ...
- La protezione del puntatore laser
Questo puntatore laser è sempre sufficientemente efficiente per eseguire il test più accurato su qua ...
- 剑指C++面试
传闻公司老总欠下巨款,带着小姨子跑路了~ 树倒猢狲散,接下来要好好准备面试,以期找到一份满意的工作. 面试准备分下面几个方面进行,形成面试系列文章,文章内容以问答的方式呈现. 1.C++语言基础 传 ...
- weblogic 控制台访问速度很慢的解决方案
实际是JVM在Linux下的bug 他想调用一个随机函数 但取不到 暂时的解决办法是 1)较好的解决办法: 在Weblogic启动参数里添加 “- Djava.security.egd=file:/d ...
- CSS常见布局问题整理
实现div的水平居中和垂直居中 多元素水平居中 实现栅格化布局 1. 实现div的水平居中和垂直居中 实现效果: 这大概是最经典的一个题目了,所以放在第一个. 方法有好多, 一一列来 主要思路其实就是 ...
- 【高速接口-RapidIO】4、Xilinx RapidIO核详解
一.RapidIO核概述 RapidIO核的设计标准来源于RapidIO Interconnect Specification rev2.2,它支持1x,2x和4x三种模式,每通道的速度支持1.25G ...
- 移动web-bootstrap
1bootstarp布局容器+栅格系统的使用 1.101-移动web-bootstrap中的布局容器 1.container和container-fluid的区别? a) container ...