//普通号
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();
}
}
}
}
标签:Thread,看病,int,void,num,多线程,public,模拟,String
From: https://blog.csdn.net/qq_61236034/article/details/142093212