choose from two cities as a pre-selected destination. The implementation of two separate threads display 10 city names, each time after each display sleep a random time (within 1000ms), which first displayed, decided to go to which city. implemented using the Runnable interface and the thread class, respectively.
1 ImportJava.util.*;;2 3 Public classLy_threadextendsThread {4 Public voidrun () {5Random r=NewRandom ();6 7 for(inti=1;i<=10;i++){8 9 Try{Ten intA=r.nextint (1000); One Thread.Sleep (a); ASystem.out.print (A + "seconds, go"); -System.out.println ( This. GetName ()); -}Catch(Exception e) { the e.printstacktrace (); - } - - } + } - Public Static voidmain (String [] args) { +Ly_thread t1=NewLy_thread (); AT1.setname ("Chongqing"); at T1.start (); - -Ly_thread t2=NewLy_thread (); -T2.setname ("Shanghai"); - T2.start (); - } in - to + -}
Run:
Thread 1-thread