为什么舞台线程不休眠,军队线程也能进行下去呢 好奇怪,而且没看法停止军队线程
轮流运行,舞台线程只是会占用一部分其他线程的运行时,但不是全部占用,其他线程还是有机会运行的。
public class Stage extends Thread {
public void run() {
System.out.println("舞台线程");
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("大幕徐徐拉开");
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ArmyOfSuiDynasty armyOfDynasty = new ArmyOfSuiDynasty("朝廷军");
ArmyOfSuiDynasty armyOfFamer = new ArmyOfSuiDynasty("农民军");
// 用Runnable接口创建线程
Thread armyTaskOfSuiDynasty = new Thread(armyOfDynasty, "朝廷军线程");
Thread armyTaskOfFamer = new Thread(armyOfFamer, "农民军线程");
// 启动线程,让军队开始作战
armyTaskOfSuiDynasty.start();
armyTaskOfFamer.start();
// //让主线程进入休眠状态,让出处理时间
// try {
//
// Thread.sleep(1);
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// 将作战指令设为停止
armyOfDynasty.keepRunning = false;
armyOfFamer.keepRunning = false;
try {
armyTaskOfFamer.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
Stage s = new Stage();
s.start();
}
}