|
1 | 1 | package thread; |
2 | 2 |
|
3 | | -import java.util.concurrent.TimeUnit; |
4 | | - |
| 3 | +import lombok.extern.slf4j.Slf4j; |
5 | 4 | import org.junit.Test; |
6 | 5 | import thread.ThreadStatusTransfer.Notify; |
7 | 6 | import thread.ThreadStatusTransfer.Wait; |
| 7 | +import thread.ThreadStatusTransfer.BlockMarkWait; |
| 8 | + |
| 9 | +import java.util.concurrent.TimeUnit; |
8 | 10 |
|
9 | 11 | /** |
10 | 12 | * @author kuangcp on 2019-04-22 9:40 AM |
11 | 13 | */ |
| 14 | +@Slf4j |
12 | 15 | public class ThreadStatusTransferTest { |
13 | 16 |
|
14 | 17 | @Test |
15 | 18 | public void testMain() throws InterruptedException { |
16 | | - Thread waitThread = new Thread(new Wait(), "WaitThread"); |
| 19 | + Thread waitThread = new Thread(new Wait(), "Waiter"); |
17 | 20 | waitThread.start(); |
18 | 21 |
|
19 | | - TimeUnit.SECONDS.sleep(100); |
| 22 | + TimeUnit.SECONDS.sleep(10); |
20 | 23 |
|
21 | | - Thread notifyThread = new Thread(new Notify(), "NotifyThread"); |
| 24 | + Thread notifyThread = new Thread(new Notify(), "Notify"); |
22 | 25 | notifyThread.start(); |
23 | 26 | } |
| 27 | + |
| 28 | + /** |
| 29 | + * 效果为 三个线程进入waiting状态,等notify后三个线程抢锁依次执行 |
| 30 | + */ |
| 31 | + @Test |
| 32 | + public void testMultipleWait() throws Exception { |
| 33 | + Object lock = new Object(); |
| 34 | + |
| 35 | + Runnable taskHandler = () -> { |
| 36 | + synchronized (lock) { |
| 37 | + try { |
| 38 | + log.info("start wait"); |
| 39 | + lock.wait(); |
| 40 | + TimeUnit.SECONDS.sleep(1); |
| 41 | + } catch (InterruptedException e) { |
| 42 | + log.error(e.getMessage(), e); |
| 43 | + } |
| 44 | + log.info("finish"); |
| 45 | + } |
| 46 | + }; |
| 47 | + new Thread(taskHandler).start(); |
| 48 | + new Thread(taskHandler).start(); |
| 49 | + new Thread(taskHandler).start(); |
| 50 | + |
| 51 | + TimeUnit.SECONDS.sleep(1); |
| 52 | + new Thread(() -> { |
| 53 | + synchronized (lock) { |
| 54 | + log.info("hold lock. notify"); |
| 55 | + lock.notifyAll(); |
| 56 | + } |
| 57 | + synchronized (lock) { |
| 58 | + log.info("try lock"); |
| 59 | + } |
| 60 | + }).start(); |
| 61 | + |
| 62 | + Thread.currentThread().join(5000); |
| 63 | + log.info("finish All"); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * 响应中断的方法: 线程进入等待或是超时等待的状态后,调用interrupt方法都是会响应中断的, |
| 68 | + * 即:Object.wait()、Thread.join、Thread.sleep、LockSupport.park的有参和无参方法。 |
| 69 | + * <p> |
| 70 | + * 不响应中断的方法:线程进入阻塞状态后,是不响应中断的,等待进入synchronized的方法或是代码块,都是会被阻塞的,此时不会响应中断, |
| 71 | + * 另外还有一个不响应中断的,那就是阻塞在ReentrantLock.lock方法里面的线程,也是不响应中断的, |
| 72 | + * 如果想要响应中断,可以使用ReentrantLock.lockInterruptibly方法。 |
| 73 | + */ |
| 74 | + @Test |
| 75 | + public void testBlockInterrupt() throws Exception { |
| 76 | + TimeUnit.SECONDS.sleep(6); |
| 77 | + log.info("start"); |
| 78 | + Object lock = new Object(); |
| 79 | + |
| 80 | + Runnable taskHandler = () -> { |
| 81 | + synchronized (lock) { |
| 82 | + try { |
| 83 | + log.info("start wait"); |
| 84 | + lock.wait(); |
| 85 | + log.info("get lock"); |
| 86 | + TimeUnit.SECONDS.sleep(1); |
| 87 | + } catch (InterruptedException e) { |
| 88 | + log.error(e.getMessage(), e); |
| 89 | + } |
| 90 | + log.info("finish"); |
| 91 | + } |
| 92 | + }; |
| 93 | + Thread a = new Thread(taskHandler); |
| 94 | + Thread b = new Thread(taskHandler); |
| 95 | + Thread c = new Thread(taskHandler); |
| 96 | + |
| 97 | + a.start(); |
| 98 | + b.start(); |
| 99 | + c.start(); |
| 100 | + |
| 101 | + TimeUnit.SECONDS.sleep(1); |
| 102 | + new Thread(() -> { |
| 103 | + synchronized (lock) { |
| 104 | + try { |
| 105 | + log.info("hold lock. start notify"); |
| 106 | + TimeUnit.MILLISECONDS.sleep(200); |
| 107 | + lock.notifyAll(); |
| 108 | + log.info("finish notify"); |
| 109 | + } catch (Exception e) { |
| 110 | + log.error("", e); |
| 111 | + } |
| 112 | + } |
| 113 | + synchronized (lock) { |
| 114 | + log.info("try lock"); |
| 115 | + } |
| 116 | + }).start(); |
| 117 | + |
| 118 | + Thread kill = new Thread(() -> { |
| 119 | + try { |
| 120 | + TimeUnit.MILLISECONDS.sleep(500); |
| 121 | + // 由于中断了所有线程,包括了处于 timed_waiting 的线程也被中断,锁也因此释放,blocked的线程也因此拿到了锁后执行sleep方法抛出中断异常 |
| 122 | + // 时间间隔短没法明确感受到 blocked 线程在发出中断信号的那一刻没作出响应 |
| 123 | + a.interrupt(); |
| 124 | + b.interrupt(); |
| 125 | + c.interrupt(); |
| 126 | + log.info("finish all interrupt"); |
| 127 | + } catch (Exception e) { |
| 128 | + log.error("", e); |
| 129 | + } |
| 130 | + }); |
| 131 | + kill.start(); |
| 132 | + |
| 133 | + Thread.currentThread().join(10000); |
| 134 | + log.info("exit"); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + public void testBlockInterrupt2() throws Exception { |
| 139 | + TimeUnit.SECONDS.sleep(6); |
| 140 | + log.info("start"); |
| 141 | + Object lock = new Object(); |
| 142 | + |
| 143 | + BlockMarkWait a = new BlockMarkWait(lock); |
| 144 | + BlockMarkWait b = new BlockMarkWait(lock); |
| 145 | + BlockMarkWait c = new BlockMarkWait(lock); |
| 146 | + |
| 147 | + a.start(); |
| 148 | + b.start(); |
| 149 | + c.start(); |
| 150 | + |
| 151 | + TimeUnit.SECONDS.sleep(1); |
| 152 | + new Thread(() -> { |
| 153 | + synchronized (lock) { |
| 154 | + try { |
| 155 | + log.info("hold lock. start notify"); |
| 156 | + TimeUnit.MILLISECONDS.sleep(200); |
| 157 | + lock.notifyAll(); |
| 158 | + log.info("finish notify"); |
| 159 | + } catch (Exception e) { |
| 160 | + log.error("", e); |
| 161 | + } |
| 162 | + } |
| 163 | + synchronized (lock) { |
| 164 | + log.info("try lock"); |
| 165 | + } |
| 166 | + }).start(); |
| 167 | + |
| 168 | + Thread kill = new Thread(() -> { |
| 169 | + try { |
| 170 | + TimeUnit.MILLISECONDS.sleep(500); |
| 171 | + // 加上判断后,只中断blocked的线程,timed_waiting线程不动,就能看到在interrupt的那一刻,blocked线程都是没反应的 |
| 172 | + // 只有等waiting线程释放锁后(间隔700ms左右 1000-(500-200))blocked线程获得锁 执行sleep方法时才报错。 |
| 173 | + a.tryInterrupt(); |
| 174 | + b.tryInterrupt(); |
| 175 | + c.tryInterrupt(); |
| 176 | + log.info("finish all interrupt"); |
| 177 | + } catch (Exception e) { |
| 178 | + log.error("", e); |
| 179 | + } |
| 180 | + }); |
| 181 | + kill.start(); |
| 182 | + |
| 183 | + Thread.currentThread().join(10000); |
| 184 | + log.info("exit"); |
| 185 | + } |
24 | 186 | } |
0 commit comments