码迷,mamicode.com
首页 > 编程语言 > 详细

[Java Basics] multi-threading

时间:2014-08-19 23:46:45      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:des   java   os   io   strong   ar   cti   ad   

1, Interrupt

Interruption in Java is not pre-emptive. Put another way both threads have to cooperate in order to process the interrupt properly. If the target thread does not poll the interrupted status the interrupt is effectively ignored.

Polling occurs via the Thread.interrupted() method which returns the current thread‘s interrupted status AND clears that interrupt flag. Usually the thread might then do something such as throw InterruptedException.

Some API methods have built in interrupt handling. Of the top of my head this includes.

  • Object.wait()/Thread.sleep()
  • Most java.util.concurrent structures
  • Java NIO (but not java.io) and it does NOT use InterruptedException, instead using ClosedByInterruptException.

Interrupts are not explicitly handled by synchronization - synchronous blocks only gaurantee that, while executing, the block cannot be reentered by another thread.

总结: 对于像sleep()这样的方法,会自己throw InterrupttedException,程序会退出。

或者自己调用Thread.interrupted()方法检测Interrupted status并进行处理。

对于一般的synchronized method,不会自动处理Interrupt并退出。

[Java Basics] multi-threading,布布扣,bubuko.com

[Java Basics] multi-threading

标签:des   java   os   io   strong   ar   cti   ad   

原文地址:http://www.cnblogs.com/chayu3/p/3923470.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!