1 PackageXiancheng;2 ImportJava.util.*;3 Public classTest1extendsThread {4 /*choose from two cities as a pre-selected destination. Two separate threads are implemented to display 10 city names, each of which sleeps for a random time (within 1000ms) after each display .5 * Whichever is displayed first, decide which city to go to. Implemented using the Runnable interface and the thread class, respectively. 6 */7 String City;8 Public voidRun ()9 {Ten City (); One } A - Private voidCity () { - //TODO Auto-generated method stubs theRandom r=NewRandom (); - intS=r.nextint (1000); - for(inti=0;i<=10;i++) - { +System.out.println (Thread.CurrentThread (). GetName () + "," + "Here I come! "); - Try + { A Thread.Sleep (s); at - } - Catch(Exception e) - { - e.printstacktrace (); - } in } - to } + - Public Static voidMain (string[] args) { the //TODO Auto-generated method stubs *Test1 t1=NewTest1 (); $T1.setname ("Lijiang, Yunnan");Panax Notoginseng T1.start (); -Test1 t11=NewTest1 (); theT11.setname ("Wuzhen"); + T11.start (); A the } + -}
Multithreading-tourist city-extends Thread