public class SleepInterrupt extends Object implements Runnable { @Override public void run() { System.out.println("in run() - enter normally"); try { System.out.println("in run() - about to sleep for 20 seconds"); Thread.sleep(20000); System.out.println("in run() - woke up"); } catch (InterruptedException e) { System.out.println("in run() - interrupted while sleeping"); /** * 处理完中断异常后,返回run()方法入口, * 若是没有return,线程不会实际被中断,它会继续打印下面的信息 */ return; } System.out.println("in run() - leaving normally"); } }
public class InterruptDemo { public static void main(String[] args) { SleepInterrupt sleepInterrupt = new SleepInterrupt(); Thread thread = new Thread(sleepInterrupt); thread.start(); // 主线程休眠2秒,从而确保刚才启动的线程有机会执行一段时间 try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("in main() - interrupting other thread"); /** * 当一个线程运行时,另外一个线程能够调用对应的Thread对象的interrupt()方法来中断它, * 该方法只是在目标线程中设置一个标志,表示它已经被中断,并当即返回 */ thread.interrupt(); System.out.println("in main() - leaving"); } }
public class PendingInterrupt extends Object { public static void main(String[] args) { /** * 若是输入了参数,则在main线程中中断当前线程(亦即main线程),除了将中断标志设置为true外,线程被中断了,可是main线程 * 仍然运行,main线程继续监视实时时钟,并进入try块,一旦调用sleep()方法,它就会注意到待决中断的存在,并抛出 * InterruptException. */ if (args.length > 0) { Thread.currentThread().interrupt(); } // 获取当前时间 long startTime = System.currentTimeMillis(); /** * 若是线程在调用sleep()方法前被中断,那么该中断称为待决中断,它会在刚调用sleep()方法时, * 当即抛出InterruptedException异常 */ try { Thread.sleep(2000); System.out.println("[PendingInterrupt] was NOT interrupted"); } catch (InterruptedException e) { System.out.println("[PendingInterrupt] was interrupted"); } // 计算中间代码执行的时间 System.out.println("[PendingInterrupt] elapsedTime=" + (System.currentTimeMillis() - startTime)); } }
/** * 使用Thread对象上调用isInterrupted()方法来检查任何线程的中断状态 * 线程一旦被中断,isInterrupted()方法便会返回true,而一旦sleep()方法抛出异常,它将清空中断标志, * 此时isInterrupted()方法将返回false */ public class InterruptCheck extends Object { public static void main(String[] args) { Thread thread = Thread.currentThread(); System.out.println("[InterruptCheck] Point A: thread.isInterrupted()= " + thread.isInterrupted()); thread.interrupt(); System.out.println("[InterruptCheck] Point B: thread.isInterrupted()= " + thread.isInterrupted()); try { Thread.sleep(2000); System.out.println("[InterruptCheck] was NOT interrupted"); } catch (InterruptedException e) { System.out.println("[InterruptCheck] was interrupted"); } System.out.println("[InterruptCheck] Point C: thread.isInterrupted()= " + thread.isInterrupted()); } }
/** * 使用Thread.interrupted()方法判断中断状态(并隐式重置为false) * 因为它是静态方法、所以不能在特定的线程上使用,而只能报告调用它的线程的中断状态,若是线程被中断, * 并且中断状态尚不清楚,那么,这个方法返回true。与isInterrupted不一样,它将自动重置中断状态为false. * */ public class InterruptReset extends Object { public static void main(String[] args) { System.out.println("[InterruptReset] Point X: Thread.interrupted()= " + Thread.interrupted()); Thread.currentThread().interrupt(); System.out.println("[InterruptReset] Point Y: Thread.interrupted()= " + Thread.interrupted()); System.out.println("[InterruptReset] Point Z: Thread.interrupted()= " + Thread.interrupted()); } }