Traditional Culture Encyclopedia - Travel guide - Randomly select two cities as pre-selected tourism targets. Two independent threads display the city name 10 times respectively, and sleep randomly for a period of time after each display (

Randomly select two cities as pre-selected tourism targets. Two independent threads display the city name 10 times respectively, and sleep randomly for a period of time after each display (

Slight changes, as follows:

Public class Testcity{

Public static void main (strinargs []) {

City 1 thread 1 = new city 1 ("Shanghai");

City c2 = New City 2 ("Fuzhou");

Thread thread2 = new thread (C2);

thread 1 . start();

thread 2 . start();

}

}

Class City 1 extended thread {

Private string name;

//construction method

Public city 1 (string name) {

this.name = name

}

Run method of publicovoid run () {//thread.

int I = 1;

While (I< 1 1){

System.out.println ("This is" +i+ "the time I want to go"+name);

Try {

thread . sleep((int)(math . random()* 1000));

}catch (exception e){}

i++;

}

System.out.println ("I decided to go"+name);

system . exit(0);

}

}

City2 class implements Runnable{

Private string name;

//construction method

Public City 2 (String Name) {

this.name = name

}

Public invalid operation () {

int I = 1;

While (I< 1 1){

System.out.println ("This is" +i+ "the time I want to go"+name);

Try {

thread . sleep((int)(math . random()* 1000));

}catch (exception e){}

i++;

}

System.out.println ("I decided to go"+name);

system . exit(0);

}

}