慕盖茨7610259
2016-02-26 20:49
public class ArmyRunnable implements Runnable { volatile boolean keepRunning=true; @Override public void run() { while(keepRunning){ for(int i=1;1<5;i++){ System.out.println(Thread.currentThread().getName()+"进攻了对方["+i+"]"); Thread.yield(); } } System.out.println(Thread.currentThread().getName()+"进攻结束"); } } public class Stager extends Thread { public void run(){ ArmyRunnable Dynasty =new ArmyRunnable(); ArmyRunnable Revolt=new ArmyRunnable(); Thread armyDynasty=new Thread(Dynasty,"隋军"); Thread armyRevolt=new Thread(Revolt,"农民军"); armyDynasty.start(); armyRevolt.start(); try { Thread.sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } Dynasty.keepRunning=false; Revolt.keepRunning=false; try { armyRevolt.join(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("当双方激战正酣,半路杀出个程咬金"); Thread mrchen=new KeyPersonThread(); mrchen.setName("程咬金"); System.out.println("程咬金的理想是结束战争,使百姓安居乐业"); Dynasty.keepRunning=false; Revolt.keepRunning=false; try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } mrchen.start(); try { mrchen.join(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("程咬金实现了他的梦想"); } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub new Stager().start(); } }
你ArmyRunnable这个类里,for循环i<5,写成1<5,成死循环了
深入浅出Java多线程
186088 学习 · 464 问题
相似问题