多线程练习:模拟叫号看病

题目:模拟叫号看病

需求说明

1、某科室一天需看普通号50个,特需号10个,

2、特需号看病时间是普通号的2倍

3、开始时普通号和特需号并行叫号,叫到特需号的概率比普通号高

4、当普通号叫完第10号时,要求先看完 全部特需号,再看普通号

5、使用多线程模拟这一过程

解题代码:

public class Demo3 implements Runnable{

    @Override
    public void run() {
        for(int i=0;i<10;i++) {
            String name= Thread.currentThread().getName();
            System.out.println(name+(i+1)+"号病人在看病!");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class Demo3Test{
    public static void main(String[] args) {
        Demo3 demo3 = new Demo3();
        Thread vip = new Thread(demo3, "特需号:");
        vip.setPriority(10);
        vip.start();
        for (int i = 0; i < 50; i++) {
            if (i == 10) {
                try {
                    vip.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            String name = Thread.currentThread().getName();
            System.out.println("普通号:"+(i + 1) + "号病人在看病!");
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

 

  • 12
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,模拟看病可以分为以下几个步骤: 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` 异常并打印停止信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值