空空酱
2016-08-16 10:32
package com.thread;
/**
* 隋唐演义作战舞台
*/
public class Stage extends Thread {
public void run(){
System.out.println("隋唐演义马上开始,请保持安静");
try {
Thread.sleep(5000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
//创建军队任务
ArmyRunnable armyTaskOfSuiTang = new ArmyRunnable();
ArmyRunnable armyTaskOfFarmmer = new ArmyRunnable();
//使用Runnable接口,创建军队线程
Thread armyOfSuiTang = new Thread(new ArmyRunnable(),"隋军");
Thread armyOfFarmmer = new Thread(new ArmyRunnable(),"农民军");
//启动线程,让军队开始作战
armyOfSuiTang.start();
armyOfFarmmer.start();
//舞台线程休眠
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
//半路杀出个程咬金
System.out.println("正当双方打得正酣,半路杀出来一个程咬金,隋军猝不及防!");
Thread Cheng = new KeyPersonThread();
Cheng.setName("陈咬金");
System.out.println("陈咬金见不得隋军猖狂!");
//停止军队线程作战
armyTaskOfSuiTang.keepRunning = false;
armyTaskOfFarmmer.keepRunning = false;
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//等待关键人物完成任务
Cheng.start();
try {
Cheng.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("战争结束!");
System.out.println("谢谢观众观看,可以离场!");
}
public static void main(String[] args) {
new Stage().start();
}
}
输出效果呢?
深入浅出Java多线程
186088 学习 · 464 问题
相似问题