关于sleep();
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();} 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 static void main(String []args){ new Stage().start(); }
让舞台休眠,大家看厮杀部分,为什么要写成Thread.sleep(); 按照字面的理解不是Stage.sleep()l;么
还是说两种方法都可以?
真心求教!