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中的多线程是一种抢占式的机制,而不是分时机制.抢占式的机制是有多个线程处于可运行状态,但是只有一个线程在运行. 这种机制决定了,对于同一对象的多线程访问,必 ...
随机推荐
- selenium之复选框操作
HTML源码: <!DOCTYPE html> <div lang="en"></div></div> <head> & ...
- Codeforces Round #548 (Div. 2) F splay(新坑) + 思维
https://codeforces.com/contest/1139/problem/F 题意 有m个人,n道菜,每道菜有\(p_i\),\(s_i\),\(b_i\),每个人有\(inc_j\), ...
- s6-8 TCP 拥塞控制
TCP 拥塞控制 虽然网络层也试图管理拥塞,但是,大多数繁重的任务是由TCP来完成的,因为针对拥塞的真正解决方案是减慢数据率 分组守恒:当有一个老的分组离开之后才允许新的分组注入网络 TC ...
- Linux环境下Redis集群实践
环境:centos 7 一.编译及安装redis源码 源码地址:redis版本发布列表 cd redis-3.2.8 sudo make && make install 二.创建节点 ...
- PIO学习
边沿捕获 PIO可以对输入进行边沿捕获,它可以捕获上升沿.下降沿和双沿,当检测到边沿时PIO会把它存在edgecapture 寄存器之内: 打开Synchronously capture 时,会生成一 ...
- 单点登陆cas
1.TGC:Ticket-granting cookie,存放用户身份认证凭证的cookie,在浏览器和CAS Server间通讯时使用,是CAS Server用来明确用户身份的凭证.TGT封装了TG ...
- asp.net 抽象方法和虚方法的用法区别,用Global类重写Application_BeginRequest等方法为例子
不废话,直接贴代码 public abstract class LogNetGlobal : System.Web.HttpApplication { protected void Applicati ...
- 20155205 郝博雅 Exp6 信息搜集与漏洞扫描
20155205 郝博雅 Exp6 信息搜集与漏洞扫描 一.实践内容 (1)各种搜索技巧的应用 (2)DNS IP注册信息的查询 (3)基本的扫描技术:主机发现.端口扫描.OS及服务版本探测.具体服务 ...
- [swarthmore cs75] Compiler 4 – Diamondback
课程回顾 Swarthmore学院16年开的编译系统课,总共10次大作业.本随笔记录了相关的课堂笔记以及第6次大作业. 函数声明 增加函数声明.函数调用的抽象语法:在转换成anf之前还要检查函数声明和 ...
- mac 命令行安装软件
第一步需要在mac上安装brew工具 ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/mas ...