b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

interrupt_lwkhehe的专栏

电脑杂谈  发布时间:2016-05-19 05:00:52  来源:网络整理

你是否正在寻找关于interrupt的内容?让我把最棒的东西奉献给你:

interrupt()详解

1.interrupt()设置中断标志位为中断,。

1.1如果此时线程要进入阻塞(入执行sleep()),那么将发生中断异常

1.2如果此时线程正在阻塞中(正在sleep()中),那么将发生中断异常

除了以上两种情况,线程将不会中断,而是继续正常执行

此时可以利用Thread.interrupted()判断是否中断而离开run()

Runnable1.java

package threed; import java.util.concurrent.TimeUnit; public class Runnable1 implements Runnable { static int id = 0; boolean runned = false; boolean sleeped = false; private volatile double b; public Runnable1() { id++; } @Override public void run() { System.out.println(this + " thread run"); synchronized (this) { runned = true; } for (int i = 1; i < 10; i++) { b += (Math.PI + Math.E) / (double) i; Thread.yield(); } System.out.println(this + " thread will sleeping"); try { synchronized (this) { sleeped = true; } TimeUnit.SECONDS.sleep(50); System.out.println(this + " thread awaik"); } catch (interruptedException e) { e.printStackTrace(); } } public synchronized boolean getSleeped() { return sleeped; } public synchronized boolean getRunned() { return runned; } public String toString() { return "id:" + id; } }
TestThread1.java

package threed; public class TestThread1 { public static void main(String[] args) { Runnable1 target = new Runnable1(); Thread t = new Thread(target); t.start(); while (true) { if (target.getRunned() && !target.getSleeped()) { System.out.println("main call interrupt"); t.interrupt(); System.out.println("main call interrupt end"); break; } } } }
执行后输入入下

id:1 thread run//线程1执行
main call interrupt//man线程调用线程1的interrupt开始
main call interrupt end//man线程调用线程1的interrupt结束
id:1 thread will sleeping//但是线程1仍然继续执行
java.lang.InterruptedException: sleep interrupted//线程1调用sleep时抛出异常
at java.lang.Thread.sleep(Native Method)
at java.lang.Thread.sleep(Unknown Source)
at java.util.concurrent.TimeUnit.sleep(Unknown Source)
at threed.Runnable1.run(Runnable1.java:37)
at java.lang.Thread.run(Unknown Source)

说明:

1.调用interrupt,线程将不会中断,而是继续正常执行

2.线程已经被调用了interrupt,如果此时线程要进入阻塞(入执行sleep()),那么将发生中断异常


将上面的TestThread1.java修改如下

package threed; public class TestThread1 { public static void main(String[] args) { Runnable1 target = new Runnable1(); Thread t = new Thread(target); t.start(); while (true) { if (target.getRunned() && target.getSleeped()) { System.out.println("main call interrupt"); t.interrupt(); System.out.println("main call interrupt end"); break; } } } } 执行后输出如下

id:1 thread run
id:1 thread will sleeping//已经进入睡眠
main call interrupt
main call interrupt end
java.lang.InterruptedException: sleep interrupted//main线程调用线程1的interrupt,线程1马上就抛出了异常
at java.lang.Thread.sleep(Native Method)
at java.lang.Thread.sleep(Unknown Source)
at java.util.concurrent.TimeUnit.sleep(Unknown Source)
at threed.Runnable1.run(Runnable1.java:36)
at java.lang.Thread.run(Unknown Source)

说明:

1.调用interrupt,如果此时线程正在阻塞中(正在sleep()中),那么将发生中断异常


以上就是关于interrupt的全部内容,相信你一定会非常满意。


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-5466-1.html

    相关阅读
      发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

      • 间嶋里美
        间嶋里美

        一如既往风格独特

      热点图片
      拼命载入中...