greenhandc
2015-12-18 11:06
package com.currentcount.base; /** * 搭建了舞台线程 * * */ public class Stage extends Thread { //复写run方法 public void run(){ System.out.println("欢迎观看隋唐演义!"); try { Thread.sleep(5000); } catch (InterruptedException e2) { e2.printStackTrace(); } System.out.println("大幕徐徐拉开"); try { Thread.sleep(5000); } catch (InterruptedException e2) { // TODO Auto-generated catch block e2.printStackTrace(); } System.out.println("话说隋朝末年,各路藩王和隋军杀的是昏天黑地"); ArmyRunnable armyTaskOfSuiDynasty=new ArmyRunnable(); ArmyRunnable armyTaskOfFarmer=new ArmyRunnable(); //使用Runnable创建线程 Thread armyOfSuiDynasty=new Thread(armyTaskOfSuiDynasty,"隋朝军队"); Thread armyOfFarmer=new Thread(armyTaskOfFarmer,"农民军队"); //线程的运行 armyOfSuiDynasty.start(); armyOfFarmer.start(); //让舞台线程休眠,大家能够专心观看军队的厮杀 try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace();} try { armyOfFarmer.join(); } catch (InterruptedException e1) { e1.printStackTrace(); } System.out.println("正当双方激战正酣,半路杀出了个程咬金"); Thread Chengyaojin=new KeypersonThread(); Chengyaojin.setName("程咬金"); System.out.println("程咬金的理想就是结束战斗,使得百姓安居乐业"); //军队停止战斗 armyTaskOfSuiDynasty.keepRunning=false; armyTaskOfFarmer.keepRunning=false; try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } Chengyaojin.start(); //所有线程等待程咬金完成自己的历史使命 try { Chengyaojin.join(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("程咬金完成了自己的理想,结束了战斗,使得人民能够安居乐业"); System.out.println("感谢观看隋唐演义!"); }
Stage的run方法里有armyOfFarmer.join();这句。这句的意思是说农民的run方法执行完了才能往armyOfFarmer.join();这句代码之后运行。但你看看你的ArmyRunnable的run方法里,while (keepRunning)的keepRuning一直是true,while循环没有被终止。所以发生了死锁。
把Stage的run方法里的armyTaskOfFarmer.keepRunning=false;这句代码修改到armyOfFarmer.join();这句代码之前,就不会发生死锁了。
下面是我修改之后的代码
public class Stage extends Thread { //复写run方法 public void run() { System.out.println("欢迎观看隋唐演义!"); try { Thread.sleep(5000); } catch (InterruptedException e2) { e2.printStackTrace(); } System.out.println("大幕徐徐拉开"); try { Thread.sleep(5000); } catch (InterruptedException e2) { // TODO Auto-generated catch block e2.printStackTrace(); } System.out.println("话说隋朝末年,各路藩王和隋军杀的是昏天黑地"); ArmyRunnable armyTaskOfSuiDynasty = new ArmyRunnable(); ArmyRunnable armyTaskOfFarmer = new ArmyRunnable(); //使用Runnable创建线程 armyOfSuiDynasty = new Thread(armyTaskOfSuiDynasty, "隋朝军队"); armyOfFarmer = new Thread(armyTaskOfFarmer, "农民军队"); //线程的运行 armyOfSuiDynasty.start(); armyOfFarmer.start(); //让舞台线程休眠,大家能够专心观看军队的厮杀 try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } try { armyTaskOfFarmer.keepRunning = false;//这行代码提前!!!! armyOfFarmer.join(); } catch (InterruptedException e1) { e1.printStackTrace(); } System.out.println("正当双方激战正酣,半路杀出了个程咬金"); Thread Chengyaojin = new KeypersonThread(); Chengyaojin.setName("程咬金"); System.out.println("程咬金的理想就是结束战斗,使得百姓安居乐业"); //军队停止战斗 armyTaskOfSuiDynasty.keepRunning = false; // armyTaskOfFarmer.keepRunning = false; try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } Chengyaojin.start(); //所有线程等待程咬金完成自己的历史使命 try { Chengyaojin.join(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("程咬金完成了自己的理想,结束了战斗,使得人民能够安居乐业"); System.out.println("感谢观看隋唐演义!"); } }
public class KeypersonThread extends Thread{ public void run(){ System.out.println(Thread.currentThread().getName()+"开始了战斗!"); for(int i=0;i<10;i++){ System.out.println(Thread.currentThread().getName()+"左突右杀,攻击了隋军"); } System.out.println(Thread.currentThread().getName()+"结束了战斗!"); }
没注意还有个这个KeypersonThread...这个里面是什么啊
public class ArmyRunnable implements Runnable { //这里使用volatile保证线程可以正确的读取其他线程写入的值 volatile boolean keepRunning=true ; //军队线程 //用来模拟作战 public void run() { while (keepRunning){ //发动五连击 for(int i=0;i<5;i++){ System.out.println(Thread.currentThread().getName()+"进攻"+i+"次"); //让出处理器的时间,下次谁攻击还不一定 Thread.yield(); } } System.out.println(Thread.currentThread().getName()+"停止攻击"); } }
ArmyRunnable这个Runnable里面写的是什么啊
深入浅出Java多线程
186088 学习 · 464 问题
相似问题