一道多线程通信实例分析
生活随笔
收集整理的這篇文章主要介紹了
一道多线程通信实例分析
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
程序如下:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | public?static?void?main(String[]?args)?throws??Exception{ ????final?List?list?=?new?ArrayList(); ????final?Object?lock?=?new?Object(); ????Thread?t1?=?new?Thread(new?Runnable()?{ ????????@Override ????????public?void?run()?{ ????????????synchronized?(lock){ ????????????????for(int?i?=?0?;?i?<?10?;?i++){ ????????????????????list.add(i); ????????????????????if(list.size()?==?5){ ????????????????????????lock.notify(); ????????????????????????System.out.println(Thread.currentThread().getName()?+?"發(fā)出通知!"); ????????????????????} ????????????????} ????????????} ????????????System.out.println(Thread.currentThread().getName()?+?"execute?over!"); ????????} ????}); ????Thread?t2?=?new?Thread(new?Runnable()?{ ????????@Override ????????public?void?run()?{ ????????????synchronized?(lock){ ????????????????if(list.size()?!=?5){ ????????????????????try?{ ????????????????????????lock.wait(); ????????????????????}?catch?(InterruptedException?e)?{ ????????????????????????e.printStackTrace(); ????????????????????} ????????????????} ????????????????System.out.println(Thread.currentThread().getName()?+?"?收到通知!"); ????????????} ????????????System.out.println(Thread.currentThread().getName()?+?"execute?over!"); ????????} ????}); ????t2.start(); ????Thread.sleep(1000); ????t1.start(); } |
分析:
程序的意圖本是利用多線程之間的通信,利用wait/notify實現(xiàn),可是運行的結(jié)果是雖然線程T1發(fā)出了通知,但是線程T2并沒有立即收到通知進行執(zhí)行,這是為什么呢??因為只有線程T1執(zhí)行完畢釋放了鎖,T2才能執(zhí)行,那么也就是說wait/notify并不是實時的(wait釋放了鎖,而notify沒有釋放鎖導致的),那么線程之間實時的通信該怎么做呢?可以利用CountDownLatch來實現(xiàn)。
對程序的改進:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | public?static?void?main(String[]?args)?throws??Exception{ ????final?List?list?=?new?ArrayList(); ????final?Object?lock?=?new?Object(); ????final?CountDownLatch?countDownLatch?=?new?CountDownLatch(1); ????Thread?t1?=?new?Thread(new?Runnable()?{ ????????@Override ????????public?void?run()?{ ????????????????for(int?i?=?0?;?i?<?10?;?i++){ ????????????????????list.add(i); ????????????????????if(list.size()?==?5){ ????????????????????????countDownLatch.countDown(); ????????????????????????System.out.println(Thread.currentThread().getName()?+?"發(fā)出通知!"); ????????????????????} ????????????????} ????????????System.out.println(Thread.currentThread().getName()?+?"execute?over!"); ????????} ????}); ????Thread?t2?=?new?Thread(new?Runnable()?{ ????????@Override ????????public?void?run()?{ ????????????????if(list.size()?!=?5){ ????????????????????try?{ ????????????????????????countDownLatch.await(); ????????????????????}?catch?(InterruptedException?e)?{ ????????????????????????e.printStackTrace(); ????????????????????} ????????????????System.out.println(Thread.currentThread().getName()?+?"?收到通知!"); ????????????} ????????????System.out.println(Thread.currentThread().getName()?+?"execute?over!"); ????????} ????}); ????t2.start(); ????Thread.sleep(1000); ????t1.start(); } |
本文轉(zhuǎn)自zfz_linux_boy 51CTO博客,原文鏈接:http://blog.51cto.com/zhangfengzhe/1875221,如需轉(zhuǎn)載請自行聯(lián)系原作者
總結(jié)
以上是生活随笔為你收集整理的一道多线程通信实例分析的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 学计算机的事物多线程看不懂,看不懂CPU
- 下一篇: (三)SpringBoot之配置文件详解