以下是使用Java编程实现该需求的示例代码:\n\njava\nimport java.util.concurrent.ExecutorService;\nimport java.util.concurrent.Executors;\n\nclass Doctor implements Runnable {\n private String type;\n private int count;\n\n public Doctor(String type, int count) {\n this.type = type;\n this.count = count;\n }\n\n @Override\n public void run() {\n for (int i = 1; i <= count; i++) {\n System.out.println(type + "号叫到第" + i + "个病人");\n try {\n Thread.sleep(1000); // 模拟看病时间\n } catch (InterruptedException e) {\n e.printStackTrace();\n }\n }\n }\n}\n\npublic class Hospital {\n public static void main(String[] args) {\n ExecutorService executorService = Executors.newFixedThreadPool(2);\n\n // 创建普通号和特需号医生线程\n Doctor normalDoctor = new Doctor("普通", 20);\n Doctor vipDoctor = new Doctor("特需", 10);\n\n // 提交医生线程任务\n executorService.submit(normalDoctor);\n executorService.submit(vipDoctor);\n\n // 关闭线程池\n executorService.shutdown();\n }\n}\n\n\n上述代码中,我们使用了ExecutorService和ThreadPoolExecutor来创建线程池,分别提交了普通号医生和特需号医生的任务。通过Thread.sleep(1000)来模拟看病时间,并输出每个医生叫到的病人号码。\n\n需要注意的是,由于特需号的看病时间是普通号的2倍,所以在实际运行中,特需号医生可能在普通号医生叫到第10个病人之前就已经叫完了所有病人。


原文地址: https://www.cveoy.top/t/topic/pP86 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录