编程要求 随便选择两个城市作为预选旅游目标。实现两个独立的线程分别显示10次城市名,每次显示后休眠一段随机时间(1000毫秒以内),哪个先显示完毕,就决定去哪个城市。分别用 Runnable 接口和 Thread 类实现。 完成以上要求后,将代码补充在右侧编辑器中。 测试说明 平台会对你编写的代码进行测试: 测试输入:无; 预期输出: 北京 西安 北京 西安 北京 西安 北京 西安 北京 西安 北京 西安 北京 西安 北京 西安 北京 西安 北京 西安package step1; class Thread1 extends Thread { @Override public void run() {//循环输出10次北京,每次显示后需休眠一段时间(1000毫秒以内) /******** Begin ********/ /******** End ********/ } } class Thread2 implements Runnable { @Override public void run() {//循环输出10次西安,每次显示后需休眠一段时间(1000毫秒以内) /******** Begin ********/ /******** End ********/ } } public class Exe8_3 { public static void main(String[] args) { //创建 Thread1 对象 和 Thread2 对象并启动两个线程 /******** Begin ********/ /******** End ********/ } }
时间: 2025-06-22 09:55:20 浏览: 5
### 使用Thread类和Runnable接口创建线程并决定旅游目标
以下为两种实现方式:一种基于`Thread`类,另一种基于`Runnable`接口。每个线程将输出10次不同城市名称,并在每次输出后随机休眠一段时间。最终根据哪个线程先完成来决定最终选择的城市。
---
#### 基于`Thread`类的实现
```java
public class CityThread extends Thread {
private String cityName;
public CityThread(String name, String cityName) {
super(name);
this.cityName = cityName;
}
@Override
public void run() {
for (int i = 1; i <= 10; i++) {
System.out.println(Thread.currentThread().getName() + " - 第" + i + "次访问 " + cityName);
try {
Thread.sleep((long) (Math.random() * 1000)); // 随机休眠
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(cityName + " 线程已完成!");
}
public static void main(String[] args) throws InterruptedException {
CityThread cityA = new CityThread("线程A", "北京");
CityThread cityB = new CityThread("线程B", "上海");
cityA.start();
cityB.start();
cityA.join(); // 等待线程A完成
cityB.join(); // 等待线程B完成
if (cityA.isAlive() || cityB.isAlive()) {
System.out.println("仍在运行中...");
} else {
String result = cityA.getState() == Thread.State.TERMINATED ? "北京" : "上海";
System.out.println("最终选择的城市是:" + result);
}
}
}
```
---
#### 基于`Runnable`接口的实现
```java
public class CityRunnable implements Runnable {
private String cityName;
public CityRunnable(String cityName) {
this.cityName = cityName;
}
@Override
public void run() {
for (int i = 1; i <= 10; i++) {
System.out.println(Thread.currentThread().getName() + " - 第" + i + "次访问 " + cityName);
try {
Thread.sleep((long) (Math.random() * 1000)); // 随机休眠
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(cityName + " 线程已完成!");
}
public static void main(String[] args) throws InterruptedException {
Thread cityA = new Thread(new CityRunnable("北京"), "线程A");
Thread cityB = new Thread(new CityRunnable("上海"), "线程B");
cityA.start();
cityB.start();
cityA.join(); // 等待线程A完成
cityB.join(); // 等待线程B完成
if (cityA.isAlive() || cityB.isAlive()) {
System.out.println("仍在运行中...");
} else {
String result = cityA.getState() == Thread.State.TERMINATED ? "北京" : "上海";
System.out.println("最终选择的城市是:" + result);
}
}
}
```
---
#### 实现说明
1. **线程命名与输出**
每个线程通过构造函数传入城市名称,并在线程运行时输出该城市名称[^3]。同时,线程名称通过`Thread.currentThread().getName()`获取并打印,以便区分两个线程的执行情况。
2. **随机休眠**
使用`Thread.sleep((long) (Math.random() * 1000))`实现每次输出后的随机休眠,模拟线程执行时间的不同。
3. **线程完成判断**
使用`join()`方法确保主线程等待子线程完成后再进行后续逻辑处理。通过`isAlive()`和`getState()`判断线程状态,最终根据哪个线程先完成来决定旅游目标城市[^4]。
---
###
阅读全文
相关推荐


















