使用Java多线程模拟叫号看病
时间: 2023-07-22 17:03:29 浏览: 82
好的,模拟叫号看病可以分为以下几个步骤:
1. 定义一个病人类,包含病人的编号、姓名等信息;
2. 定义一个医生类,包含医生的编号、姓名等信息;
3. 定义一个叫号机类,用于生成病人编号,并将病人编号加入队列中;
4. 定义一个看病过程类,用于模拟医生叫号、病人排队等过程。
下面是Java多线程模拟叫号看病的示例代码:
```java
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class Hospital {
// 病人队列
private BlockingQueue<Patient> patientQueue = new ArrayBlockingQueue<>(10);
// 叫号机
private CallingMachine callingMachine = new CallingMachine(patientQueue);
// 看病过程
private TreatmentProcess treatmentProcess = new TreatmentProcess(patientQueue);
// 启动叫号机和看病过程
public void start() {
callingMachine.start();
treatmentProcess.start();
}
// 停止叫号机和看病过程
public void stop() {
callingMachine.interrupt();
treatmentProcess.interrupt();
}
public static void main(String[] args) {
Hospital hospital = new Hospital();
hospital.start();
}
}
// 病人类
class Patient {
private int id;
private String name;
public Patient(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
}
// 医生类
class Doctor {
private int id;
private String name;
public Doctor(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
}
// 叫号机类
class CallingMachine extends Thread {
private BlockingQueue<Patient> patientQueue;
public CallingMachine(BlockingQueue<Patient> patientQueue) {
this.patientQueue = patientQueue;
}
@Override
public void run() {
int count = 1;
while (!Thread.currentThread().isInterrupted()) {
try {
// 生成病人编号
Patient patient = new Patient(count, "Patient" + count);
// 将病人加入队列
patientQueue.put(patient);
System.out.println("Calling " + patient.getName() + ", No." + patient.getId());
count++;
Thread.sleep(1000); // 模拟每秒钟叫一次号
} catch (InterruptedException e) {
System.out.println("Calling machine is stopped.");
break;
}
}
}
}
// 看病过程类
class TreatmentProcess extends Thread {
private BlockingQueue<Patient> patientQueue;
public TreatmentProcess(BlockingQueue<Patient> patientQueue) {
this.patientQueue = patientQueue;
}
@Override
public void run() {
while (!Thread.currentThread().isInterrupted()) {
try {
// 从队列中取出病人
Patient patient = patientQueue.take();
System.out.println("Doctor is treating " + patient.getName() + ", No." + patient.getId());
Thread.sleep(3000); // 模拟每个病人看病需要3秒钟
} catch (InterruptedException e) {
System.out.println("Treatment process is stopped.");
break;
}
}
}
}
```
在这个示例中,我们定义了一个 `Hospital` 类作为程序的入口,其中包含一个病人队列、一个叫号机和一个看病过程。在 `start()` 方法中启动叫号机和看病过程,`stop()` 方法中停止叫号机和看病过程。`main()` 方法中创建 `Hospital` 对象并启动程序。
在病人类 `Patient` 中,我们定义了病人的编号和姓名。在医生类 `Doctor` 中,我们定义了医生的编号和姓名。在叫号机类 `CallingMachine` 中,我们使用 `BlockingQueue` 存储病人队列,使用 `Thread.sleep()` 模拟每秒钟叫一次号。在看病过程类 `TreatmentProcess` 中,我们从队列中取出病人,并使用 `Thread.sleep()` 模拟每个病人看病需要3秒钟。
在 `run()` 方法中,我们使用 `Thread.currentThread().isInterrupted()` 判断线程是否被打断,如果被打断则停止程序。在 `catch` 块中捕获 `InterruptedException` 异常并打印停止信息。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)