初心

用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使用的更多相关文章

  1. java中的interrupt(),InterruptException和wait(),sleep()

    标题中的几个概念大概设计到线程同步以及线程阻塞这两个概念.线程同步,就是同一时刻,只有一个线程能执行指定的代码:另外一个线程阻塞就是当前线程暂时停在某个位置,等待某个条件成立之后再继续往下面执行.   ...

  2. Java中interrupt的使用

    通常我们会有这样的需求,即停止一个线程.在java的api中有stop.suspend等方法可以达到目的,但由于这些方法在使用上存在不安全性,会带来不好的副作用,不建议被使用.具体原因可以参考Why ...

  3. java中interrupt、join、sleep、notify、notifyAll、wait详解

    首先介绍一下中断概念:举个例子容易理解一点 例子:假如你正在给朋友写信,电话铃响了.这时,你放下手中的笔,去接电话.通话完毕,再继续写信.这个例子就表现了中断及其处理过程:电话铃声使你暂时中止当前的工 ...

  4. java中interrupt,interrupted和isInterrupted的区别

    文章目录 isInterrupted interrupted interrupt java中interrupt,interrupted和isInterrupted的区别 前面的文章我们讲到了调用int ...

  5. java中的锁

    java中有哪些锁 这个问题在我看了一遍<java并发编程>后尽然无法回答,说明自己对于锁的概念了解的不够.于是再次翻看了一下书里的内容,突然有点打开脑门的感觉.看来确实是要学习的最好方式 ...

  6. Java中的多线程你只要看这一篇就够了

    学习Java的同学注意了!!! 学习过程中遇到什么问题或者想获取学习资源的话,欢迎加入Java学习交流群,群号码:279558494 我们一起学Java! 引 如果对什么是线程.什么是进程仍存有疑惑, ...

  7. java中的多线程

    什么是多线程? 首先得知道什么是线程? 线程是一组指令的集合,或者是程序的特殊段,它可以在程序里独立执行.也可以把它理解为代码运行的上下文.所以线程基本上是轻量级的进程,它负责在单个程序里执行多任务. ...

  8. JAVA中关于并发的一些理解

    一,JAVA线程是如何实现的? 同步,涉及到多线程操作,那在JAVA中线程是如何实现的呢? 操作系统中讲到,线程的实现(线程模型)主要有三种方式: ①使用内核线程实现 ②使用用户线程实现 ③使用用户线 ...

  9. Java中的wait和sleep

    sleep()和wait() 首先,Java中的多线程是一种抢占式的机制,而不是分时机制.抢占式的机制是有多个线程处于可运行状态,但是只有一个线程在运行. 这种机制决定了,对于同一对象的多线程访问,必 ...

随机推荐

  1. mysql 模糊查询条件带‘%’问题

  2. 出错with root cause

    [背景:] 我自己写了一个项目,主页可以看到一个数据库里的一个应用的users用户表的所有数据,包括用户的年龄,姓名,出生日期等信息.后来又想再增加一个注册功能,写好了之后进行单元测试,结果就出现了w ...

  3. python 特别的生成器表达式

    Ⅰ起因 学习python的同学通常会遇到这样一道经典生成器测试题: def gen(): for i in range(4): yield i base = gen() for n in (2,10) ...

  4. Hadoop-2.0 目录简介

    Hadoop-2.0 目录简介 一.目录结构 将下载的压缩包解压: 解压后文件夹如下: 二.各文件夹目录结构 1.bin:Hadoop2.0的最基本管理脚本和使用脚本所在目录.这些脚本是sbin目录下 ...

  5. shell 日常技巧

    批量注释: :<<COMMENT code COMMENT 循环: #!/bin/bash  for varible1 in {1..5}  #for varible1 in 1 2 3  ...

  6. Tmux会话的使用

    不想看废话的直接拖到下面看干货部分! 我们管理Linux服务器通常是通过ssh远程连接过去,如果在服务器上执行比较耗时的操作,比如下载安装软件.编译等等,如果需要数个小时来完成这些工作,但是又不得不关 ...

  7. .Net Framework 4.x 程序到底运行在哪个 CLR 版本之上(ZT)

    本文转载   https://walterlv.github.io/dotnet/2017/09/22/dotnet-version.html ,感谢  吕毅 (包含链接: https://walte ...

  8. vue单页应用前进刷新后退不刷新方案探讨

    引言 前端webapp应用为了追求类似于native模式的细致体验,总是在不断的在向native的体验靠拢:比如本文即将要说到的功能,native由于是多页应用,新页面可以启用一个的新的webview ...

  9. FFmpeg开发实战(二):FFmpeg 文件操作

    FFmpeg 提供了丰富的API供我们使用,下面我们来讲述一下文件操作相关的API: FFmpeg 删除文件:avpriv_io_delete() FFmpeg 重命名文件:avpriv_io_mov ...

  10. MySQL InnoDB 索引组织表 & 主键作用

    InnoDB 索引组织表 一.索引组织表定义 在InnoDB存储引擎中,表都是根据主键顺序组织存放的,这种存储方式的表称为索引组织表(index organized table IOT). 在Inno ...