Java多线程——模拟看病叫号

需求说明:

某科室一天需看普通号50个,特需号10个
特需号看病时间是普通号的2倍
开始时普通号和特需号并行叫号,叫到特需号的概率比普通号高
当普通号叫完第10号时,要求先看完全部特需号,再看普通号
使用多线程模拟这一过程

思路:

这题如果把专家号和普通号都写为子线程就难了,

把专家号写成子线程,把普通号写成主线程这样就简单了,因为子线程和主线程是并行的,所以不会有任何的影响。

主线程:

package KB;

public class kbTest {
    public static void main(String[] args) {
        // 专家号和普通号两个线程同时执行,普通号是主线程,专家号是子线程
        Lookbing lookbing = new Lookbing();
        Thread thread1 = new Thread(lookbing);

        // 叫到特需号的概率比普通号高
        thread1.setPriority(Thread.MAX_PRIORITY);
        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
        thread1.start();

        // 20个普通号
        for (int i = 1; i <= 50; i++) {
            System.out.println("普通号:"+i+"号病人正在看病");
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            // 当普通号叫完第10号时,要求先看完全部特需号,再看普通号
            if(i==10){
                //
                try {
                    thread1.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

子线程:

public class Lookbing implements Runnable{
    @Override
    public void run() {
        for (int i = 1; i <= 10; i++) {
            System.out.println("专家号:"+i+"号病人正在看病");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}

好的,模拟看病可以分为以下几个步骤: 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` 异常并打印停止信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值