以下下内容均来自网络,只是整理一下;:)
曾经在http://www.iteye.com/topic/1116679,回帖很多,前段时间一直比较忙,没整理。。。;
1) 介绍一下java线程一共有几个状态;
此图来之core java
顺便说下,new一个线程出来后,调用start 方法才是处于runnable ,而不是的run()方法线;
值得注意的是: 线程的可运行状态并不代表线程一定在运行(runnable != running ) 。 大家都知道:所有现代桌面和服务器操作系统都使用了抢占式的线程调度策略 。一旦线程开始执行,并不是总是保持持续运行状态的。当系统分给它的时间片(非常小的运行时间单位)用完以后,不管程序有没有执行完,线程被强制放弃CPU,进入就绪状态,直到下次被调度后开始继续执行。也就是说, Runnable可运行状态的线程处于两种可能的情况下:(1)占用CPU运行中,(2)等待调度的就绪状态。 这里要声明一下:处于等待调度的就绪状态线程和处于阻塞的线程是完全不同的。就绪的线程是因为时间片用完而放弃CPU,其随时都有可能再次获得CPU而运行,这一切取决于分时OS的线程调度策略。(http://hxraid.iteye.com/blog/429005)
2)一下内容参考了http://fujohnwang.github.com/statics/52jump/TerminateJavaThreadGracefully.html
文中提到比较和谐的结束一个线程 代码如下
1.继承thread类
public class GracefulTerminationSupportThread extends Thread { protected volatile boolean running = true; @Override public void run() { while (running) { // do something as per specific situations } } public void terminate() { running = false; interrupt(); } public static void main(String[] args) { GracefulTerminationSupportThread t = new GracefulTerminationSupportThread(); t.start(); // do other things t.terminate(); } }2 实现runnable 写法
public class TerminalSignalSupportRunnable implements Runnable { protected volatile boolean running = true; public void run(){ while(running){ ... } } public void terminate(Thread threadHandle) { running = false; threadHandle.interrupt(); } }若把terminate 方法里的 threadHandle.interput()换成如下代码,则不会正常终止,为啥会这样,请见作者博文
Thread.currentThread().interrupt();3)线程中断 —— interrupt();
api 1.6 对中断是这么解释:
Unless the current thread is interrupting itself, which is always permitted, thecheckAccess
method of this thread is invoked, which may cause aSecurityException
to be thrown.
If this thread is blocked in an invocation of thewait()
, wait(long)
, or wait(long, int)
methods of the Object
class, or of the join()
, join(long)
, join(long, int)
, sleep(long)
, or sleep(long, int)
, methods of this class, then its interrupt status will be cleared and it will receive anInterruptedException
.
If this thread is blocked in an I/O operation upon aninterruptible channel
then the channel will be closed, the thread's interrupt status will be set, and the thread will receive a
ClosedByInterruptException
.
If this thread is blocked in a Selector
then the thread's interrupt status will be set and it will return immediately from the selection operation, possibly with a non-zero value, just as if the selector'swakeup
method were invoked.
If none of the previous conditions hold then this thread's interrupt status will be set.
Interrupting a thread that is not alive need not have any effect.
1.当线程不是处于api 提出中的三个if 状态,调用 interrupt() 方法,会改变线程的中断标志。
2.当线程处于三个if状态,调用 interrupt() 方法,只把线程提早的结束阻塞状态(处于阻塞状态的线程,没有获得cpu资源),让线程继续运行,他的中断标志不变,
3. 线程结束运行后的,他的中断标志也是false
4.没有任何语言方面的需求要求一个被中断的程序应该终止。中断一个线程只是为了引起该线程的注意,被中断线程可以决定如何应对中断
接下让我们看下如下程序的运行结果:
public class A extends Thread {
public A(){
}
public void run(){
while(!Thread.currentThread().isInterrupted()){
System.out.println("A 在中断前:"+Thread.currentThread().isInterrupted());
/*try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println("--A 在中断异常---:"+isInterrupted());
interrupt();
System.out.println("######A 异常后继续中断---:"+isInterrupted());
e.printStackTrace();
}*/
}
}
}
}
去掉A中的注释,然后在运行
---------------------------------------------------------------------------------------------------------------------------------
以下是一个回帖的内容摘要,这兄弟 ,功底深厚
/** */
在上一篇Ibm社区关于