1.补充知识
说ThreadPoolExecutor之前先要说先补充知识,关于Thread的interrupt相关操作。
还有一篇博客比较言简意赅,interrupt、interrupted 、isInterrupted 区别
interrupt对于处于sleep,wait状态的线程会进行interrupt并抛出InterruptedException,同时擦除中断标志位,也就是说,这时候如果再用isInterrupted或者静态interrupted方法获取中断标志位的时候,得到的是false。
interrupt对于正常运行的线程起不到中断作用,只是把该线程的中断标志位设置为true了,线程会继续正常运行。
Thread静态interrupted方法判断标志位的时候也会清除标志位。也就是说一个线程被中断了,在线程中用interrupted判断时返回true,再次调用该方法判断会返回false。
Thread的成员函数isInterrupted判断标志位的时候不会清除标志位,如果返回true,那么无论执行多少次都会返回true(期间没有其他清除标志位的操作的话)。
2.处于sleep状态下的打断
public static void test1() throws Exception {
final Thread thread = new Thread(new Runnable() {
@Over