只有四個類,前面三個介紹執行緒的休眠喚醒以及結束、最後乙個類是乙個單獨的小例子、建議先看最後的在看前面的
package com.imooc.concurrent.base;
/** * 隋唐演義大戲舞台
*/public class stage extends thread catch (interruptedexception e1)
system.out.println("大幕徐徐拉開");
try catch (interruptedexception e1)
system.out.println("話說隋朝末年,隋軍與農民起義軍殺得昏天黑地...");
armyrunnable armytaskofsuidynasty = new armyrunnable();
armyrunnable armytaskofrevolt = new armyrunnable();
//使用runnable介面建立執行緒
thread armyofsuidynasty = new thread(armytaskofsuidynasty,"隋軍");
thread armyofrevolt = new thread(armytaskofrevolt,"農民起義軍");
//啟動執行緒,讓軍隊開始作戰
armyofsuidynasty.start();
armyofrevolt.start();
//舞台執行緒休眠,大家專心**軍隊廝殺
try catch (interruptedexception e)
system.out.println("正當雙方激戰正酣,半路殺出了個程咬金");
thread mrcheng = new keypersonthread();
mrcheng.setname("程咬金");
system.out.println("程咬金的理想就是結束戰爭,使百姓安居樂業!");
//停止軍隊作戰
//停止執行緒的方法
armytaskofsuidynasty.keeprunning = false;
armytaskofrevolt.keeprunning = false;
try catch (interruptedexception e)
/** 歷史大戲留給關鍵人物
*/mrcheng.start();
//萬眾矚目,所有執行緒等待程先生完成歷史使命
try catch (interruptedexception e)
system.out.println("戰爭結束,人民安居樂業,程先生實現了積極的人生夢想,為人民作出了貢獻!");
system.out.println("謝謝**隋唐演義,再見!");
}public static void main(string args)
}
package com.imooc.concurrent.base;
//軍隊執行緒
//模擬作戰雙方的行為
public class armyrunnable implements runnable
} system.out.println(thread.currentthread().getname()+"結束了戰鬥!");
}}
package com.imooc.concurrent.base;
public class keypersonthread extends thread
system.out.println(thread.currentthread().getname()+"結束了戰鬥!");
}}
建議先從下面的這個開始看。
package com.imooc.concurrent;
public class actor extends thread
if(count%10== 0) catch (interruptedexception e)
}} system.out.println(getname()+"的演出結束了!"); }
public static void main(string args)
}class actress implements runnable
if(count%10== 0) catch (interruptedexception e)
}} system.out.println(thread.currentthread().getname()+"的演出結束了!");
}}
乙個簡單的多執行緒
頭部 type tmythread class tthread protected procedure execute override end procedure tmythread.execute begin coinitialize nil 如果不加這句,會提示未呼叫coinitialize ...
多執行緒案例 Java
1 購票 不安全策略 片段 public class main catch exception e new thread r start new thread r start new thread r start new thread r start 顯示結果 賣出了一張票,還剩下6張票 賣出了一張...
乙個簡單的多執行緒socket
碼了2個小時多,終於弄完了。用的編輯器是qt creator 突然發現在qt creator上,也支援這些底層的c語言。在linux上除錯無誤。寫這個,也算是自己讀書的記錄吧!include include include include include include include includ...