生產者消費者(解決綫程同步問題)
2017-06-28 20:21
225 查看
這届上幹貨,初學者可以瞭解下!
class Info { private String name; private String description; public synchronized void set(String name, String description) { this.name = name; try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } this.description = description; } public synchronized void get() { System.out.println(this.name + "---------->" + this.description); } } class Pr implements Runnable { private Info info; public Pr(Info info) { this.info = info; } @Override public void run() { for (int i = 0; i < 50; i++) { if (i % 2 == 0) { this.info.set("TOM", ":BOY"); } else { this.info.set("MARY", ":GIRY"); } } } } class Cr implements Runnable { private Info info; public Cr(Info info) { this.info = info; } @Override public void run() { for (int i = 0; i < 50; i++) { this.info.get(); } } } public class Demo { public static void main(String[] args) { Info info = new Info(); Pr pr = new Pr(info); Cr cr = new Cr(info); new Thread(pr).start(); new Thread(cr).start(); } }
相关文章推荐
- 解決Android系統設定Exchange同步郵件的問題
- 教您如何解決 Samsung Galaxy S2 無法與 Samsung Kies 同步軟體連線的問題
- 溝通是為了解決未來的問題
- [three.js] 解決貼圖無法重複的問題 Solving with Texture RepeatWrapping Fail Issue
- 解決 /usr/bin/ld: cannot find -lxxx 問題
- [CSS Hack]解決IE6、IE7、IE8、Firefox的瀏覽器相容性問題!
- IE ,解決SCRIPT不相容問題
- 有關NHibernate問題﹐尋求解決方法
- 繁體網站上發布新聞﹐亂碼的問題,不能發布簡體中心的解決方案
- Server 2005 中CET結合ROW_NUMBER()函數解決取得特定筆數資料的問題
- git的“Bad file number的問題解決辦法
- [ 筆記 ] 用 Python 訓練一個神經元 (解決異或問題, 梯度下降法, Delta 學習規則)
- 簡單粗暴解決 Zsh Home 和 End 按鍵問題
- HTML 資訊汲取(下篇) - TagSoup 輸出 namespace 問題的解決方案
- 環境變量的配置問題(老話重談目的解決實際問題)
- IE8中调试VS2003一般問題解決
- 如何解決在VS中文版應用中無法添加XtraReport 問題
- 解決eclipse啟動不了問題
- 解決GHOST光盤不能遠程桌面問題
- 水晶報表中2個問題的解決方法