初心

用interrupt中断程序

初步实现

  1. public class InterruptionInJava implements Runnable{
  2. @Override
  3. public void run() {
  4. while (true) {
  5. if (Thread.currentThread().isInterrupted()) {
  6. System.out.println("Yes!! I'm Interupted, but I'm still running");
  7. } else {
  8.  
  9. }
  10. }
  11. // System.out.println("Oh, myGod!");
  12. }
  13.  
  14. public static void main(String[] args) {
  15. Thread thread = new Thread(new InterruptionInJava(), "testThread");
  16. thread.start();
  17. try {
  18. Thread.sleep(1000);
  19. } catch (InterruptedException e) {
  20.  
  21. }
  22. System.out.println("Begin Interupt...");
  23. thread.interrupt();
  24. System.out.println("End Interupt...");
  25. }
  26. }

输出

  1. Yes!! I'm Interupted, but I'm still running
  2. Yes!! I'm Interupted, but I'm still running
  3. Yes!! I'm Interupted, but I'm still running
  4. Yes!! I'm Interupted, but I'm still running
  5. Yes!! I'm Interupted, but I'm still running
  6. Yes!! I'm Interupted, but I'm still running
  7. Yes!! I'm Interupted, but I'm still running
  8. .....  

问题:虽然是被中断状态,但实际并未中断

interrupt说明

在java中主要有3个相关方法,interrupt(),isInterrupted()和interrupted()。

  • interrupt(),在一个线程中调用另一个线程的interrupt()方法,即会向那个线程发出信号——线程中断状态已被设置。至于那个线程何去何从,由具体的代码实现决定。
  • isInterrupted(),用来判断当前线程的中断状态(true or false)。
  • interrupted()是个Thread的static方法,用来恢复中断状态(!!!)

解决不中断问题

处于被中断状态时,return 或 bread 中断线程

  1. public class InterruptionInJava implements Runnable{
  2. @Override
  3. public void run() {
  4. while (true) {
  5. if (Thread.currentThread().isInterrupted()) {
  6. System.out.println("Yes!! I'm Interupted, but I'm still running");
  7. return;
  8. } else {
  9.  
  10. }
  11. }
  12. }
  13.  
  14. public static void main(String[] args) {
  15. Thread thread = new Thread(new InterruptionInJava(), "testThread");
  16. thread.start();
  17. try {
  18. Thread.sleep(1000);
  19. } catch (InterruptedException e) {
  20.  
  21. }
  22. System.out.println("Begin Interupt...");
  23. thread.interrupt();
  24. System.out.println("End Interupt...");
  25. }
  26. }

等价开关

  1. public class InterruptionInJava2 implements Runnable{
  2. private volatile static boolean on = false;
  3.  
  4. @Override
  5. public void run() {
  6. while (!on) {
  7. try {
  8. System.out.println("begin Sleep");
  9. Thread.sleep(10000000);
  10. System.out.println("end Sleep");
  11. } catch (InterruptedException e) {
  12. e.printStackTrace();
  13. }
  14. System.out.println("Oh, myGod!");
  15. }
  16. }
  17.  
  18. public static void main(String[] args) {
  19. Thread thread = new Thread(new InterruptionInJava2(), "testThread");
  20. thread.start();
  21. try {
  22. System.out.println("main Begin sleep");
  23. Thread.sleep(5000);
  24. System.out.println("main End sleep");
  25. } catch (InterruptedException e) {
  26.  
  27. }
  28. InterruptionInJava2.on = true;
  29. }
  30. }

使用静态变量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. selenium之复选框操作

    HTML源码: <!DOCTYPE html> <div lang="en"></div></div> <head> & ...

  2. Codeforces Round #548 (Div. 2) F splay(新坑) + 思维

    https://codeforces.com/contest/1139/problem/F 题意 有m个人,n道菜,每道菜有\(p_i\),\(s_i\),\(b_i\),每个人有\(inc_j\), ...

  3. s6-8 TCP 拥塞控制

    TCP 拥塞控制  虽然网络层也试图管理拥塞,但是,大多数繁重的任务是由TCP来完成的,因为针对拥塞的真正解决方案是减慢数据率  分组守恒:当有一个老的分组离开之后才允许新的分组注入网络  TC ...

  4. Linux环境下Redis集群实践

    环境:centos 7 一.编译及安装redis源码 源码地址:redis版本发布列表 cd redis-3.2.8 sudo make && make install 二.创建节点 ...

  5. PIO学习

    边沿捕获 PIO可以对输入进行边沿捕获,它可以捕获上升沿.下降沿和双沿,当检测到边沿时PIO会把它存在edgecapture 寄存器之内: 打开Synchronously capture 时,会生成一 ...

  6. 单点登陆cas

    1.TGC:Ticket-granting cookie,存放用户身份认证凭证的cookie,在浏览器和CAS Server间通讯时使用,是CAS Server用来明确用户身份的凭证.TGT封装了TG ...

  7. asp.net 抽象方法和虚方法的用法区别,用Global类重写Application_BeginRequest等方法为例子

    不废话,直接贴代码 public abstract class LogNetGlobal : System.Web.HttpApplication { protected void Applicati ...

  8. 20155205 郝博雅 Exp6 信息搜集与漏洞扫描

    20155205 郝博雅 Exp6 信息搜集与漏洞扫描 一.实践内容 (1)各种搜索技巧的应用 (2)DNS IP注册信息的查询 (3)基本的扫描技术:主机发现.端口扫描.OS及服务版本探测.具体服务 ...

  9. [swarthmore cs75] Compiler 4 – Diamondback

    课程回顾 Swarthmore学院16年开的编译系统课,总共10次大作业.本随笔记录了相关的课堂笔记以及第6次大作业. 函数声明 增加函数声明.函数调用的抽象语法:在转换成anf之前还要检查函数声明和 ...

  10. mac 命令行安装软件

    第一步需要在mac上安装brew工具 ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/mas ...