|
| 1 | +package com.brianway.learning.java.concurrent; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStream; |
| 5 | +import java.util.concurrent.ExecutorService; |
| 6 | +import java.util.concurrent.Executors; |
| 7 | +import java.util.concurrent.Future; |
| 8 | +import java.util.concurrent.TimeUnit; |
| 9 | + |
| 10 | +/** |
| 11 | + * Interrupting a blocked thread. |
| 12 | + * |
| 13 | + * |
| 14 | + */ |
| 15 | +public class Interrupting { |
| 16 | + private static ExecutorService exec = |
| 17 | + Executors.newCachedThreadPool(); |
| 18 | + |
| 19 | + static void test(Runnable r) throws InterruptedException { |
| 20 | + Future<?> f = exec.submit(r); |
| 21 | + TimeUnit.MILLISECONDS.sleep(100); |
| 22 | + System.out.println("Interrupting " + r.getClass().getSimpleName()); |
| 23 | + f.cancel(true); // Interrupts if running |
| 24 | + System.out.println("Interrupt sent to " + r.getClass().getSimpleName()); |
| 25 | + } |
| 26 | + |
| 27 | + public static void main(String[] args) throws Exception { |
| 28 | + test(new SleepBlocked()); |
| 29 | + test(new IOBlocked(System.in)); |
| 30 | + test(new SynchronizedBlocked()); |
| 31 | + TimeUnit.SECONDS.sleep(3); |
| 32 | + System.out.println("Aborting with System.exit(0)"); |
| 33 | + System.exit(0); // ... since last 2 interrupts failed |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * 可中断的阻塞示例 |
| 39 | + */ |
| 40 | +class SleepBlocked implements Runnable { |
| 41 | + public void run() { |
| 42 | + try { |
| 43 | + TimeUnit.SECONDS.sleep(100); |
| 44 | + } catch (InterruptedException e) { |
| 45 | + System.out.println("InterruptedException"); |
| 46 | + } |
| 47 | + System.out.println("Exiting SleepBlocked.run()"); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +class IOBlocked implements Runnable { |
| 52 | + private InputStream in; |
| 53 | + |
| 54 | + public IOBlocked(InputStream is) { |
| 55 | + in = is; |
| 56 | + } |
| 57 | + |
| 58 | + public void run() { |
| 59 | + try { |
| 60 | + System.out.println("Waiting for read():"); |
| 61 | + in.read(); |
| 62 | + } catch (IOException e) { |
| 63 | + if (Thread.currentThread().isInterrupted()) { |
| 64 | + System.out.println("Interrupted from blocked I/O"); |
| 65 | + } else { |
| 66 | + throw new RuntimeException(e); |
| 67 | + } |
| 68 | + } |
| 69 | + System.out.println("Exiting IOBlocked.run()"); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +class SynchronizedBlocked implements Runnable { |
| 74 | + public synchronized void f() { |
| 75 | + while (true) // Never releases lock |
| 76 | + Thread.yield(); |
| 77 | + } |
| 78 | + |
| 79 | + public SynchronizedBlocked() { |
| 80 | + new Thread() { |
| 81 | + public void run() { |
| 82 | + f(); // Lock acquired by this thread |
| 83 | + } |
| 84 | + }.start(); |
| 85 | + } |
| 86 | + |
| 87 | + public void run() { |
| 88 | + System.out.println("Trying to call f()"); |
| 89 | + f(); |
| 90 | + System.out.println("Exiting SynchronizedBlocked.run()"); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +/* Output: (95% match) |
| 95 | +Interrupting SleepBlocked |
| 96 | +Interrupt sent to SleepBlocked |
| 97 | +InterruptedException |
| 98 | +Exiting SleepBlocked.run() |
| 99 | +Waiting for read(): |
| 100 | +Interrupting IOBlocked |
| 101 | +Interrupt sent to IOBlocked |
| 102 | +Trying to call f() |
| 103 | +Interrupting SynchronizedBlocked |
| 104 | +Interrupt sent to SynchronizedBlocked |
| 105 | +Aborting with System.exit(0) |
| 106 | +*///:~ |
0 commit comments