这里我们通过实例来学习一下java多线程中关于interrupt方法的一些知识。执者失之。我想当一个诗人的时候,我就失去了诗,我想当一个人的时候,我就失去了我自己。在你什么也不想要的时候,一切如期而来。

java多线程中的interrupt实例

一、java中的interrupt的使用

public class InterruptTest {
public static void main(String[] args) {
MyThread1 myThread1 = new MyThread1();
myThread1.start(); myThread1.interrupt();
System.out.println("isInterrupted is " + myThread1.isInterrupted()); // true
System.out.println("isInterrupted is " + myThread1.interrupted()); // false
} static class MyThread1 extends Thread {
@Override
public void run() {
try {
         // The interrupted status of the thread is cleared by this method
System.out.println("in isInterrupted is " + Thread.interrupted()); // true
System.out.println("isInterrupted is " + Thread.currentThread().isInterrupted()); // false
TimeUnit.SECONDS.sleep(5);
System.out.println("my thread 1");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

如果注释掉代码:System.out.println("in isInterrupted is " + Thread.interrupted());则打印的结果如下:

isInterrupted is true
isInterrupted is false
isInterrupted is true
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at java.lang.Thread.sleep(Thread.java:340)
at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:360)
at com.linux.huhx.thread2.InterruptTest$MyThread1.run(InterruptTest.java:21)

二、多线程中的中断停止线程运行

public class InterruptTest {
public static void main(String[] args) throws Exception {
MyThread1 myThread1 = new MyThread1();
myThread1.start(); myThread1.interrupt();
System.out.println("isInterrupted is " + myThread1.isInterrupted()); // true
System.out.println("isInterrupted is " + myThread1.interrupted()); // false
} static class MyThread1 extends Thread {
@Override
public void run() {
while (true) {
if (Thread.interrupted()) {
System.out.println("break");
break;
}
System.out.println("count " + Thread.currentThread().isInterrupted()); // count false
}
}
}
}

一次的运行结果如下:

isInterrupted is true
isInterrupted is false
break

友情链接

java基础---->多线程之interrupt(九)的更多相关文章

  1. java基础---->多线程之Runnable(一)

    java线程的创建有两种方式,这里我们通过简单的实例来学习一下.一切都明明白白,但我们仍匆匆错过,因为你相信命运,因为我怀疑生活. java中多线程的创建 一.通过继承Thread类来创建多线程 pu ...

  2. java基础---->多线程之synchronized(六)

    这里学习一下java多线程中的关于synchronized的用法.我来不及认真地年轻,待明白过来时,只能选择认真地老去. synchronized的简单实例 一. synchronized在方法上的使 ...

  3. java基础---->多线程之wait和notify(八)

    这里学习一下java多线程中的关于wait方法和notify方法的用法.命运不是风,来回吹,命运是大地,走到哪你都在命运中. wait和notify方法的使用 一.wait与notify的简单实例 i ...

  4. java基础---->多线程之ThreadLocal(七)

    这里学习一下java多线程中的关于ThreadLocal的用法.人时已尽,人世还长,我在中间,应该休息. ThreadLocal的简单实例 一.ThreadLocal的简单使用 package com ...

  5. java基础---->多线程之yield(三)

    yield方法的作用是放弃当前的CPU资源,将它让给其它的任务去占用CPU执行时间.但放弃的时间不确定,有可能刚刚放弃,马上又获得CPU时间片.今天我们通过实例来学习一下yield()方法的使用.最是 ...

  6. java基础---->多线程之Daemon(五)

    在java线程中有两种线程,一种是用户线程,另一种是守护线程.守护线程是一种特殊的线程,当进程中不存在非守护线程了,则守护线程自动销毁.今天我们通过实例来学习一下java中关于守护线程的知识.我是个平 ...

  7. java基础---->多线程之priority(四)

    线程的priority能告诉调度程序其重要性如何,今天我们通过实例来学习一下java多线程中的关于优先级的知识.我从没被谁知道,所以也没被谁忘记.在别人的回忆中生活,并不是我的目的. java多线程的 ...

  8. Java多线程之interrupt()的深度研究

    近期学习Java多线程的中断机制,网上的帖子说得很浅,并没深究其原理.看了Java源码,对Java的中断机制有了略深入的理解,在这篇文章中向感兴趣的网友分享下.这篇文章主要通过一个典型例子对中断机制进 ...

  9. 多线程之interrupt

    1.interrupt()作为中断程序,并不会直接终止运行,而是设置中断状态,由线程自己处理中断.可以选择终止线程.等待新任务或继续执行. 2.interrupt()经常用于中断处于堵塞状态的的线程, ...

随机推荐

  1. Java中 堆 栈,常量池等概念解析(转载)

    1.寄存器:最快的存储区, 由编译器根据需求进行分配,我们在程序中无法控制. 2. 栈:存放基本类型的变量数据和对象的引用,但对象本身不存放在栈中,而是存放在堆(new 出来的对象)或者常量池中(字符 ...

  2. SpringAOP 通知(advice)

      @Aspect @Order(1) public class AopOne { /** * 目标方法执行之前 * @param joinPoint */ @Before("executi ...

  3. Golang 类型转换整理

    1.整形到字符串: var i int = 1 var s string s = strconv.Itoa(i) 或者 s = FormatInt(int64(i), 10) 2.字符串到整形 var ...

  4. poj3208 Apocalypse Someday 数位dp+二分 求第K(K <= 5*107)个有连续3个6的数。

    /** 题目:poj3208 Apocalypse Someday 链接:http://poj.org/problem?id=3208 题意:求第K(K <= 5*107)个有连续3个6的数. ...

  5. 定制LNMP的RPM包

    自动化部署必备技能—定制化RPM包 回顾下安装软件的三种方式: 1.编译安装软件,优点是可以定制化安装目录.按需开启功能等,缺点是需要查找并实验出适合的编译参数,诸如MySQL之类的软件编译耗时过长. ...

  6. json数据 提示框flash.now[:notice] flash.now[:alert]

    实现json.做出提示框 1.在controller中使用flash.now[:alert] = "str"方法来做print def topodata #@vnic = Vnic ...

  7. TCP协议的问题

    Server端接收到Client端信息后不会返回给Client端 // TCPEchoServer.cpp : 定义控制台应用程序的入口点.// #include "stdafx.h&quo ...

  8. EasyUI 搜索框

    1.用法 (1).从标记创建.把 'easyui-searchbox' class 加入到 <input> 标记. <script type="text/javascrip ...

  9. centos 部署 SparkR

    ---恢复内容开始--- 环境配置—— 操作系统:CentOS 6.5 JDK版本:1.7.0_67 Hadoop集群版本:CDH 5.3.0 安装过程—— 1.(1)安装R yum install ...

  10. C++类的成员函数的形参列表后面的const

    看到(C++ Primer)类的成员函数这里,突然对成员函数形参列表后面的const感到迷惑. 因为书中开始说是修饰隐含形参this的,然后又说是声明该函数是只读的. 大为不解! 翻资料.找人讨论.. ...