代碼運行結果差異
代碼運行結果線程顯示混亂,看不出問提出在哪?
package com.imooc;
public class Stage extends Thread {
?
public void run()
{
System.out.println("歡迎觀看隋唐演義");
try {
Thread.sleep(5000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("大幕徐徐拉開");
//舞臺線程休眠
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Thread.sleep(5000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
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 {
Thread.sleep(50);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("雙方激戰正酣,半路殺出個程咬金");
Thread mrCheng = new KeyPersonThread();
mrCheng.setName("程咬金");
System.out.println("程咬金的理想就是結束戰爭,使百姓安居樂業!");
//軍隊停止作戰
armyTaskOfSuiDynasty.Keeprunning = false;
armyTaskOfRevolt.Keeprunning = false;
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mrCheng.start();
try {
mrCheng.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("戰爭結束,人民安居樂業,程先生實現了積極的人生夢想,為人民做出貢獻");
System.out.println("隋唐演義結束,謝謝");
}
public static void main(String[] args) {
new Stage().start();
}
}
2017-05-30
這是運行結果混亂部分的截圖