|
| 1 | +package com.prd.concurrent; |
| 2 | + |
| 3 | +import java.util.TreeSet; |
| 4 | +import java.util.concurrent.TimeUnit; |
| 5 | + |
| 6 | +/** |
| 7 | + * 线程停止 |
| 8 | + */ |
| 9 | +public class ThreadStopTest { |
| 10 | + |
| 11 | + public static void main(String[] args) { |
| 12 | +// rigthStopThreadNoBlock(); |
| 13 | +// rigthStopThreadBlock(); |
| 14 | +// rigthStopThreadBlockEveryLoop(); |
| 15 | +// catInterrupt(); |
| 16 | +// theSecondWayToStopThread(); |
| 17 | + wangStopThreadByVolatile(); |
| 18 | + } |
| 19 | + |
| 20 | + |
| 21 | + /** |
| 22 | + * 无sleep或wait等阻塞时停止线程, |
| 23 | + * 需要在线程内部主动判断中断状态 |
| 24 | + */ |
| 25 | + private static void rigthStopThreadNoBlock() { |
| 26 | + Thread test = new Thread(() -> { |
| 27 | + int num = 0; |
| 28 | + // 影响中断 |
| 29 | + while (!Thread.currentThread().isInterrupted() && |
| 30 | + num < Integer.MAX_VALUE) { |
| 31 | + if (num % 10000 == 0) { |
| 32 | + System.out.println(num + "是10000的倍数"); |
| 33 | + } |
| 34 | + num++; |
| 35 | + } |
| 36 | + System.out.println("线程执行完成"); |
| 37 | + }); |
| 38 | + test.start(); |
| 39 | + try { |
| 40 | + TimeUnit.SECONDS.sleep(1); |
| 41 | + } catch (InterruptedException e) { |
| 42 | + e.printStackTrace(); |
| 43 | + } |
| 44 | + test.interrupt(); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * 线程在由sleep,wait,join等造成的阻塞情况下,停止线程时 |
| 49 | + * 阻塞方法会响应interrupt方法,并抛出InterruptedException异常 |
| 50 | + * 结果: |
| 51 | + * 0是100的倍数 |
| 52 | + * java.lang.InterruptedException: sleep interrupted |
| 53 | + * at java.lang.Thread.sleep(Native Method) |
| 54 | + * at com.prd.concurrent.ThreadStopTest.lambda$rigthStopThreadBlock$1(ThreadStopTest.java:56) |
| 55 | + * at java.lang.Thread.run(Thread.java:748) |
| 56 | + */ |
| 57 | + private static void rigthStopThreadBlock() { |
| 58 | + Runnable run = () -> { |
| 59 | + int num = 0; |
| 60 | + try { |
| 61 | + while (num <= 300 |
| 62 | + && !Thread.currentThread().isInterrupted()) { |
| 63 | + if (num % 100 == 0) { |
| 64 | + System.out.println(num + "是100的倍数"); |
| 65 | + } |
| 66 | + num++; |
| 67 | + } |
| 68 | + Thread.sleep(1000); |
| 69 | + } catch (InterruptedException e) { |
| 70 | + e.printStackTrace(); |
| 71 | + } |
| 72 | + }; |
| 73 | + Thread thread = new Thread(run); |
| 74 | + thread.start(); |
| 75 | + try { |
| 76 | + Thread.sleep(500); |
| 77 | + } catch (InterruptedException e) { |
| 78 | + e.printStackTrace(); |
| 79 | + } |
| 80 | + thread.interrupt(); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * 在执行过程中,每次循环中都会调用sleep等阻塞线程,停止线程时. |
| 85 | + * 这里的Thread.currentThread().isInterrupted() 就显得多余, |
| 86 | + * 线程停止是while循环内的sleep响应异常控制的。 |
| 87 | + * 结果: |
| 88 | + * 0是100的倍数 |
| 89 | + * 100是100的倍数 |
| 90 | + * 200是100的倍数 |
| 91 | + * 300是100的倍数 |
| 92 | + * 400是100的倍数 |
| 93 | + * java.lang.InterruptedException: sleep interrupted |
| 94 | + */ |
| 95 | + private static void rigthStopThreadBlockEveryLoop() { |
| 96 | + Runnable run = () -> { |
| 97 | + int num = 0; |
| 98 | + try { |
| 99 | + while (num <= 10000) { |
| 100 | + if (num % 100 == 0) { |
| 101 | + System.out.println(num + "是100的倍数"); |
| 102 | + } |
| 103 | + num++; |
| 104 | + Thread.sleep(10); |
| 105 | + } |
| 106 | + } catch (InterruptedException e) { |
| 107 | + e.printStackTrace(); |
| 108 | + } |
| 109 | + }; |
| 110 | + Thread thread = new Thread(run); |
| 111 | + thread.start(); |
| 112 | + try { |
| 113 | + Thread.sleep(5000); |
| 114 | + } catch (InterruptedException e) { |
| 115 | + e.printStackTrace(); |
| 116 | + } |
| 117 | + thread.interrupt(); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * 如果while或for的循环中有try/catch,会导致中断失效。 |
| 122 | + * 结果: |
| 123 | + * 400是100的倍数 |
| 124 | + * java.lang.InterruptedException: sleep interrupted |
| 125 | + * at java.lang.Thread.sleep(Native Method) |
| 126 | + * at com.prd.concurrent.ThreadStopTest.lambda$catInterrupt$3(ThreadStopTest.java:129) |
| 127 | + * at java.lang.Thread.run(Thread.java:748) |
| 128 | + * 500是100的倍数 |
| 129 | + * 600是100的倍数 |
| 130 | + * <p> |
| 131 | + * try/catch会拦截处理掉InterruptedException,并且中断状态判断失效 |
| 132 | + */ |
| 133 | + private static void catInterrupt() { |
| 134 | + Runnable run = () -> { |
| 135 | + int num = 0; |
| 136 | + while (num <= 10000) { |
| 137 | + // 这里的判断状态无效 |
| 138 | + if (num % 100 == 0 |
| 139 | + && !Thread.currentThread().isInterrupted()) { |
| 140 | + System.out.println(num + "是100的倍数"); |
| 141 | + } |
| 142 | + num++; |
| 143 | + try { |
| 144 | + Thread.sleep(10); |
| 145 | + } catch (InterruptedException e) { |
| 146 | + // 抛出异常后,导致中断状态被清除,故而Thread.currentThread().isInterrupted() |
| 147 | + //失效 |
| 148 | + e.printStackTrace(); |
| 149 | + } |
| 150 | + } |
| 151 | + }; |
| 152 | + Thread thread = new Thread(run); |
| 153 | + thread.start(); |
| 154 | + try { |
| 155 | + Thread.sleep(5000); |
| 156 | + } catch (InterruptedException e) { |
| 157 | + e.printStackTrace(); |
| 158 | + } |
| 159 | + thread.interrupt(); |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * 在生成环境中,中断线程最佳实践:catche了InterruptedException之后的优先选择: |
| 164 | + * 在方法签名中抛出异常。那么在run()就会强制try/catch。 |
| 165 | + * 因为在run方法中只能catch,不能再往外抛出任何异常,因为run方法是重写的,在接口中run方法的定义中本来就没有抛出异常。 |
| 166 | + * 重写方法是不能破坏父类或接口方法的定义的。 |
| 167 | + */ |
| 168 | + private static void theFirstWayToStopThread() { |
| 169 | + Runnable runA = () -> { |
| 170 | + while (true) { |
| 171 | + System.out.println("生产开始"); |
| 172 | + try { |
| 173 | + throwInMethod(); |
| 174 | + } catch (InterruptedException e) { |
| 175 | + // 保存日志,停止程序 |
| 176 | + System.out.println("保存日志"); |
| 177 | + e.printStackTrace(); |
| 178 | + } |
| 179 | + } |
| 180 | + }; |
| 181 | + Thread test1 = new Thread(runA); |
| 182 | + test1.start(); |
| 183 | + try { |
| 184 | + Thread.sleep(1000); |
| 185 | + } catch (InterruptedException e) { |
| 186 | + e.printStackTrace(); |
| 187 | + } |
| 188 | + test1.interrupt(); |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * 实际的生成方法 |
| 193 | + */ |
| 194 | + private static void throwInMethod() throws InterruptedException { |
| 195 | + Thread.sleep(2000); |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * 在生产环境中,中断线程最佳实践2:在catch子句中调用Thread.currentThread().interrupt() |
| 200 | + * 来恢复设置中断状态,以便于在后续的执行中,依然能否检查到刚才发生了中断。 |
| 201 | + */ |
| 202 | + private static void theSecondWayToStopThread() { |
| 203 | + Runnable runA = () -> { |
| 204 | + while (true) { |
| 205 | + System.out.println("生产开始"); |
| 206 | + if (Thread.currentThread().isInterrupted()) { |
| 207 | + System.out.println("Interrupted 程序运行结束"); |
| 208 | + break; |
| 209 | + } |
| 210 | + reInterrupt(); |
| 211 | + } |
| 212 | + }; |
| 213 | + Thread test1 = new Thread(runA); |
| 214 | + test1.start(); |
| 215 | + try { |
| 216 | + Thread.sleep(1000); |
| 217 | + } catch (InterruptedException e) { |
| 218 | + e.printStackTrace(); |
| 219 | + } |
| 220 | + test1.interrupt(); |
| 221 | + } |
| 222 | + |
| 223 | + /** |
| 224 | + * 实际的生成方法 |
| 225 | + */ |
| 226 | + private static void reInterrupt() { |
| 227 | + try { |
| 228 | + Thread.sleep(2000); |
| 229 | + } catch (InterruptedException e) { |
| 230 | + Thread.currentThread().interrupt(); |
| 231 | + e.printStackTrace(); |
| 232 | + } |
| 233 | + } |
| 234 | + |
| 235 | + private volatile static boolean canceled = false; |
| 236 | + |
| 237 | + /** |
| 238 | + * 错误方法:使用volatile变量来控制线程中断. |
| 239 | + * 结论:貌似看起来可行。 |
| 240 | + * 但是如果线程是阻塞状态时,volatile并不会起作用 |
| 241 | + */ |
| 242 | + private static void wangStopThreadByVolatile() { |
| 243 | + new Thread(() -> { |
| 244 | + int num = 0; |
| 245 | + try { |
| 246 | + while (num <= 10000 && !canceled) { |
| 247 | + if (num%100 == 0) { |
| 248 | + System.out.println(num + "是100的倍数"); |
| 249 | + } |
| 250 | + num++; |
| 251 | + // 如果sleep时间过长,在阻塞状态时来改变canceled是无效的 |
| 252 | + // 如果这是wait,则会导致线程长时间阻塞,无法中断了 |
| 253 | + Thread.sleep(10); |
| 254 | + } |
| 255 | + } catch (InterruptedException e) { |
| 256 | + e.printStackTrace(); |
| 257 | + } |
| 258 | + }).start(); |
| 259 | + |
| 260 | + try { |
| 261 | + Thread.sleep(5000); |
| 262 | + } catch (InterruptedException e) { |
| 263 | + e.printStackTrace(); |
| 264 | + } |
| 265 | + canceled = true; |
| 266 | + } |
| 267 | +} |
0 commit comments