Thread thread4 = new Thread(){
public void run(){
try {
//synchronized(this){
//this.wait(10000);
System.out.println("start A4");
System.out.println("A4 interrupt "+currentThread().isInterrupted());
currentThread().sleep(100000);
long time= System.currentTimeMillis();
while(System.currentTimeMillis()-time<20000)
{
}
System.out.println("end A4");
//}
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("B4");
System.out.println("B4 interrupt "+currentThread().isInterrupted());
}
}
};
thread4.start();
//主线程休眠 start
try {
Thread.sleep(5000l);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println("B5");
}
////主线程休眠 end
System.out.println("interrupt...start");
thread4.interrupt();
System.out.println("interrupt...end");
如上代码,interrupt的作用就是设置打断状态true或者false;
如果调用过thread4.interrupt(); 则再调用sleep join wait就会抛出InterruptedException异常,并且再次把interrupt状态设置为false;如果没有调用sleep join wait,那么我们可以发现在thread4.interrupt()之后currentThread().isInterrupted()为true。
第二种情况 当被block之后,我发现也会抛出InterruptedException,代码如下
Thread thread4 = new Thread(){
public void run(){
try {
//synchronized(this){
//this.wait(10000);
System.out.println("start A4");
System.out.println("A4 interrupt "+currentThread().isInterrupted());
BlockingQueue<String> q= new ArrayBlockingQueue<String>(2);
q.put("d");
q.put("d");
q.put("d");
System.out.println("end A4");
//}
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("B4");
System.out.println("B4 interrupt "+currentThread().isInterrupted());
}
}
};
thread4.start();
// try {
// Thread.sleep(5000l);
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// System.out.println("B5");
// }
System.out.println("interrupt...start");
thread4.interrupt();
System.out.println("interrupt...end");
分享到:
相关推荐
Thread.interrupt()方法的使用以及使用它退出线程
线程的thread.interrupt()方法是中断线程,将会设置该线程的中断状态位,即设置为true,中断的结果线程是死亡、还是等待新的任务或是继续运行至下一步,就取决于这个程序本身。线程会不时地检测这个中断标示位,以...
主要介绍了Java线程Thread之interrupt中断解析,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
interrupt(),在一个线程中调用另一个线程的interrupt()方法,即会向那个线程发出信号——线程中断状态已被设置。至于那个线程何去何从,由具体的代码实现决定。 isInterrupted(),用来判断当前线程的中断状态...
一般来说,支持线程中断的方法(也就是线程中断后会抛出InterruptedException的方法),阻塞函数,如:Thread.sleep、Thread.jo
本人利用Thread.Abort()与Thread.Interrupt()可以引起目标线程异常的特点,开发了一种不使用已过时的方法来变相从外部控制线程的挂起与恢复的技术,原理简单,方便理解。 相关技术参考:...
tx_interrupt_control. 互斥锁相关API tx_mutex_create, tx_mutex_delete, tx_mutex_get, tx_mutex_info_get, tx_mutex_prioritize, tx_mutex_put. 队列相关API tx_queue_create, tx_queue_delete, tx_queue_...
线程的基本概念、线程类、任务类、线程优先级、sleep()方法(休眠)、yield()方法(礼让)、join方法(合并)、interrupt()方法...在Java中,可以使用Thread类的interrupt()方法实现线程的中断。(run方法执行完毕)
This paper presents information about how thread scheduling, thread context, and a processor’s current interrupt request level (IRQL) affect the operation of kernel-mode drivers for the Microsoft® ...
Programming In Lua(Lua 脚本语言入门教程 中文版).pdf
显示线程中断行为的简单 Java 测试运行:mvn clean test 进行演示
interrupt字面上是中断的意思,但在Java里Thread.interrupt()方法实际上通过某种方式通知线程,并不会直接中止该线程
•Thread safe (although the asynchronous interface means that you usually won't need to thread) •Lightweight with lean API •Compatible with libusb-0.1 through the libusb-compat-0.1 translation layer...
使用 interrupt()方法中断线程。 第一种:使用标志位终止线程 使用标志位终止线程就是定义一个boolean型的标志位 ,在线程的run方法中根据这个标志位是为true还是为false来判断是否终止,这种情况多用于while循环中...
listener.interrupt(); listener = null; dataOut.close(); socket.close(); } catch (IOException ignored) { } } } public void run() { String message = ""; try { handlers.addElement(this); ...
Thread.currentThread().interrupt(); } } }); } Or the same thing using guice: public void startGuiceServer() { final NiftyBootstrap bootstrap = Guice.createInjector( Stage.PRODUCTION, new ...
本文介绍了在编写一个测试程序时,程序需要暂停一段时间,于是调用Thread.sleep()。但是编译器或IDE报错说没有处理检查到的InterruptedException。对于InterruptedException,一种常见的处理方式是“生吞” 它,然后...
线程中断规则:对线程interrupt()方法的调用先行发生于被中断线程的代码检测到中断事件的发生; g.线程终结规则:线程中所有的操作都先行发生于线程的终止检测,我们可以通过Thread.join()方法结束、Thread.isAlive...
主要介绍了java多线程Thread的实现方法代码详解,涉及start(),run(),stop(),interrupt(),isInterrupted(),join()和join(long millis)等方法的介绍,具有一定借鉴价值,需要的朋友可以了解下。