这里我们通过实例来学习一下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. init进程接管孤儿进程的验证

      #include <stdio.h> #include <unistd.h> #include <sys/types.h> #include<stdlib ...

  2. iOS边练边学--CALayer,非根层隐式动画,钟表练习

    一.CALayer UIView之所以能显示在屏幕上,完全是因为他内部的一个图层 在创建UIView对象时,UIView内部会自动创建一个图层(即CALayer对象),通过UIView的layer属性 ...

  3. openfire url get提交 中文乱码问题

    原因是它只接受url编码后的中文 如:%E7%BC%96%E7%A0%81%E5%90%8E%E7%9A%84%E4%B8%AD%E6%96%87 会自动转变为:http://127.0.0.1:90 ...

  4. 利用百度地图API根据地址查询经纬度

    传上来只是为了记录下三种jsonp方式,$.get(url, callback)方式不行,会出错 -- 必须指明返回类型为”json”才行. 或者使用$.getJSON()或者$.ajax({}). ...

  5. e667. 在给定图像中创建缓冲图像

    An Image object cannot be converted to a BufferedImage object. The closest equivalent is to create a ...

  6. java---final、finally、finalize的区别

    Java finalize方法使用 标签: javaappletobjectwizardjvm工作 2011-08-21 11:37 48403人阅读 评论(5) 收藏 举报  分类: Java(96 ...

  7. CentOS下 Uptime 命令

    对于一些人来说系统运行了多久是无关紧要的,但是对于服务器管理员来说,这是相当重要的信息.服务器在运行重要应用的时候,必须尽量保证长时间的稳定运行,有时候甚至要求零宕机.那么我们怎么才能知道服务器运行了 ...

  8. js for form

    //表单填充   表单填充        , formDataLoad: function (domId, obj) {            for (var property in obj) {  ...

  9. html中可以自定义属性,,,妈的竟然才知道..

    html中可以自定义属性,,,妈的竟然才知道.. <input userinfo="没见过帅哥呀" />

  10. Winform appconfig修改后的更新问题