多线程模拟叫号看病

//普通号
public class NormalThread extends Thread{
    private int num=20;

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public NormalThread(String name, int num){
        super(name);
        this.num=num;
    }

    @Override
    public void run() {
        for (int i = 0; i < num; i++) {
            String type=Thread.currentThread().getName();
            System.out.println(type+(i+1)+"号病人正在看病");
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if(i==9){
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
//特需号
public class VipThread extends Thread{
    private int num=10;

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

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

    }
}
//运行
public class Test {
    public static void main(String[] args) {
        VipThread vip = new VipThread("特需号",10);
        NormalThread normal = new NormalThread("普通号",20);
        vip.setPriority(10);//通过设置权重,特需号更容易叫到
        normal.setPriority(1);
        vip.start();
        normal.start();
        if(normal.getNum()==10){//当普通号叫到10号时,停止叫普通号,先让特需号看完
            try {
                vip.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        }
    }

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,模拟看病可以分为以下几个步骤: 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、付费专栏及课程。

余额充值