网站建设及推广文案,讯美智能网站建设,设计公司logo设计大全,wordpress redis memcachedjava中的线程中断 1、线程中断 即 线程的取消/关闭的机制2、线程对中断interrupt()的反应2.1、RUNNABLE#xff1a;线程在运行或具备运行条件只是在等待操作系统调度2.2、WAITING/TIMED_WAITING#xff1a;线程在等待某个条件或超时2.3、BLOCKED#xff1a;线程在等待锁线程在运行或具备运行条件只是在等待操作系统调度2.2、WAITING/TIMED_WAITING线程在等待某个条件或超时2.3、BLOCKED线程在等待锁试图进入同步块2.4、NEW/TERMINATED线程还未启动或已结束2.5、IO操作 3、关于中断的经验 1、线程中断 即 线程的取消/关闭的机制
Java中停止一个线程的主要机制是中断中断并不是强迫终止一个线程它是一种 协作机制 是给线程传递一个取消/关闭信号由线程自身来决定如何以及何时退出。
停止线程但这个方法被标记为了过时。
Deprecated
public final void stop()返回对应线程的中断标志位是否为true。
public boolean isInterrupted()
返回当前线程的中断标志位是否为true并清空中断标志位。
public static boolean interrupted()
表示中断对应的线程。
public void interrupt()
2、线程对中断interrupt()的反应
2.1、RUNNABLE线程在运行或具备运行条件只是在等待操作系统调度
举个栗子
public static void main(String[] args) throws InterruptedException {Thread t new Thread(() - {while (!Thread.currentThread().isInterrupted()) {System.out.println(线程t 执行中...);}});t.start();Thread.sleep(1);t.interrupt();System.out.println(main exit);
}RUNNABLE状态的线程t被interrupt()后是否终止中断线程由线程t自身代码逻辑决定。
2.2、WAITING/TIMED_WAITING线程在等待某个条件或超时
线程执行如下方法会进入WAITING状态
public final void join() throws InterruptedException
public final void wait() throws InterruptedException线程执行如下方法会进入TIMED_WAITING状态
public final native void wait(long timeout) throws InterruptedException
public static native void sleep(long millis) throws InterruptedException
public final synchronized void join(long millis) throws InterruptedException举个栗子
public class ThreadInterrupt {public static void main(String[] args) {Thread t new Thread(() - {try {Thread.sleep(10000);} catch (InterruptedException e) {System.out.println(Thread.interrupted() Thread.interrupted());//Thread.interrupted() falseSystem.out.println(Thread.interrupted() Thread.interrupted());//Thread.interrupted() false}});t.start();t.interrupt();}
}捕获到InterruptedException通常表示希望结束该线程线程大概有两种处理方式 向上传递该异常这使得该方法也变成了一个可中断的方法需要调用者进行处理。
有些情况不能向上传递异常比如Thread的run方法它的声明是固定的不能抛出任何受检异常这时应该捕获异常进行合适的清理操作清理后一般应该调用Thread的interrupt方法设置中断标志位使得其他代码有办法知道它发生了中断。
2.3、BLOCKED线程在等待锁试图进入同步块
举个栗子
public class ThreadInterrupt {private final static Object lockObj new Object();private static class MyBlockedThread extends Thread {Overridepublic void run() {System.out.println(MyBlockedThread.run Thread.currentThread().isInterrupted());synchronized (lockObj) {while (!Thread.currentThread().isInterrupted()) {System.out.println(Thread.currentThread().isInterrupted());}}System.out.println(exit);}}public static void main(String[] args) throws InterruptedException {synchronized (lockObj) {MyBlockedThread myBlockedThread new MyBlockedThread();myBlockedThread.start();Thread.sleep(3000);myBlockedThread.interrupt();myBlockedThread.join(); // join方法会等待线程执行完后返回}}
}myBlockedThread.join();该行代码放开注释掉情况下线程一直等待锁 BLOCKED。
com.michael.ThreadInterrupt.MyBlockedThread.run falsemyBlockedThread.join();该行代码注释掉情况下因为主线程不再等待线程myBlockedThread结束释放锁lock后线程myBlockedThread会获得锁然后检测到发生了中断然后程序退出。
com.michael.ThreadInterrupt.MyBlockedThread.run false
exit2.4、NEW/TERMINATED线程还未启动或已结束
举个栗子
public static void main(String[] args) throws InterruptedException {Thread t new Thread(() - {System.out.println(线程t 执行...);});t.interrupt();System.out.println(NEW t.isInterrupted());t.start();Thread.sleep(100);t.interrupt();System.out.println(TERMINATE t.isInterrupted());
} 执行结果
NEW false
线程t 执行...
TERMINATE false2.5、IO操作
如果线程在等待IO操作尤其是网络IO则会有一些特殊的处理。
如果IO通道是可中断的即实现了InterruptibleChannel接口则线程的中断标志位会被设置同时线程会收到异常ClosedByInterruptException。如果线程阻塞于Selector调用则线程的中断标志位会被设置同时阻塞的调用会立即返回。InputStream的read调用该操作是不可中断的如果流中没有数据read会阻塞 (但线程状态依然是RUNNABLE)且不响应interrupt()与synchronized类似调用interrupt()只会设置线程的中断标志而不会真正中断。
3、关于中断的经验
Java中取消/关闭线程技术是中断但它是一种协作机制不会强制终止线程。线程在不同状态和IO操作时对中断的反应有所不同。 作为线程的实现者应该提供明确的取消/关闭方法并用文档描述清楚其行为。 作为线程的调用者应该使用其取消/关闭方法而不是贸然调用interrupt()方法。