一起来打游戏
class Player extends Thread { private String name; private int sec; public Player(String name,int sec) { this.name=name; this.sec=sec; } public void run() { System.out.println("玩家"+name+"出发.."); for(int a=0;a<30;a+=1) { System.out.print("→"); try{ Thread.sleep(100); } catch(Exception e){ e.printStackTrace(); } } System.out.println(); //yield(); try{ Thread.sleep(sec*1000); } catch(Exception e){ e.printStackTrace(); } System.out.println("玩家"+name+"经过"+sec+"秒后抵达战场"); } } class ThreadDoudizhu { public static void main (String[] args) throws Exception { Player p1=new Player("haha",1); Player p2=new Player("taotao",4); Player p3=new Player("xixi",6); Player p4=new Player("maomao",2); p1.start(); p2.start(); p3.start(); p4.start(); p1.join(); p2.join(); p3.join(); p4.join(); System.out.println(); System.out.println("德玛西亚!!!!"); } }
时间: 2024-10-11 23:09:03