我有这个分配给我的电气工程类,我们必须模拟疫苗接种center.One线程分配一些预约id的患者的过程,另一个线程“疫苗”的患者返回一个message.The两个线程同时工作与患者被分配一个预约id和医生检查他们从患者列表中删除相同的time.The患者和医生类实现了接口runnable,所以他们是运行在单独的threads.The类约会是一个管理器,它控制线程何时启动。
import java.util.ArrayList;
import java.util.Random;
public class Patient implements Runnable {
private static int currentid;
private static ArrayList<Patient>patients = new ArrayList<Patient>();
private String name;
private int id;
private int sleeptime;
Random r = new Random();
public Patient()
{
this.sleeptime = r.nextInt(99);
patients.add(this);
}
public Patient(String name)
{
this.name =name;
this.sleeptime = r.nextInt(99);
patients.add(this);
}
public static ArrayList<Patient>getpatients()
{
return patients;
}
public String getname()
{
return name;
}
public void setname(String name)
{
this.name = name;
}
public int getid()
{
return id;
}
public void setid(int id)
{
this.id = id;
}
public int getsleeptime()
{
return sleeptime;
}
public void run()
{
try
{
Thread.sleep(sleeptime);
currentid++;
setid(currentid);
System.out.println("Patient:"+getname()+" with appointment id:"+getid()+" Responded time:"+getsleeptime());
}
catch(Exception e)
{
}
}
}
import java.util.ArrayList;
import java.util.Random;
public class Doctor implements Runnable
{
private static int currentid;
private static ArrayList<Doctor>doctors = new ArrayList<Doctor>();
private String name;
private int id;
private int sleeptime;
Random r = new Random();
public Doctor()
{
this.sleeptime= r.nextInt(999);
currentid++;
doctors.add(this);
}
public Doctor(String name)
{
this.sleeptime= r.nextInt(999);
this.name = name;
currentid++;
this.id = currentid;
doctors.add(this);
}
public static ArrayList<Doctor>getdoctors()
{
return doctors;
}
public String getname()
{
return name;
}
public void setname(String name)
{
this.name =name;
}
public int getid()
{
return id;
}
private int getsleeptime()
{
return sleeptime;
}
public void run()
{
try
{
Thread.sleep(sleeptime);
vaccinate(Patient.getpatients());
}
catch(Exception e)
{
}
}
private void vaccinate(ArrayList<Patient>patients)
{
while(patients.size()>=0)
{
if(patients.get(0).getid()!=0)
{
System.out.println("Doctor:"+getname()+" with id:"+"Vaccinated patient with name:"+patients.get(0).getname()+" with appointment id:"+patients.get(0).getid()+" Response time:"+getsleeptime());
patients.remove(0);
}
else
{
System.out.println("Patient("+patients.get(0).getname()+") hasnt received a appointment id yet!");
}
}
}
}
import java.util.ArrayList;
public class Appointments
{
private ArrayList<Patient>patients;
private ArrayList<Doctor>doctors;
public Appointments()
{
patients = Patient.getpatients();
doctors = Doctor.getdoctors();
}
public void createAppointments()
{
for(Patient p:patients)
{
Thread t = new Thread(p);
t.start();
}
}
public void endAppointments()
{
for(Doctor d:doctors)
{
Thread t = new Thread(d);
t.start();
}
}
}
在main中,我有这样的代码:
Patient p1 = new Patient("Mike Brown");
Patient p2 = new Patient("Scottie Pippen");
Patient p3 = new Patient("Michael Jordan");
Patient p4 = new Patient("LeBron James");
Patient p5 = new Patient("Moses Malone");
Patient p6 = new Patient("A");
Patient p7 = new Patient("B");
Patient p8 = new Patient("C");
Patient p9 = new Patient("D");
Patient p10 = new Patient("E");
Patient p11 = new Patient("F");
Patient p12 = new Patient("G");
Patient p13 = new Patient("H");
Patient p14 = new Patient("I");
Patient p15 = new Patient("J");
Patient p16 = new Patient("K");
Patient p17 = new Patient("L");
Patient p18 = new Patient("M");
Patient p19 = new Patient("N");
Patient p20 = new Patient("O");
Doctor d1 = new Doctor("Doctor1");
Doctor d2 = new Doctor("Doctor2");
Appointments app = new Appointments();
app.createAppointments();
app.endAppointments();
每次我运行程序时,我都会得到一个不同的结果,比如expected.However,有时我会在输出中得到这样的结果:
Patient(J) hasnt received a appointment id yet!
Patient(J) hasnt received a appointment id yet!
Doctor:Doctor2 with id:Vaccinated patient with name:J with appointment id:19 Response time:89
Patient:J with appointment id:19 Responded time:96
这对我来说没有任何意义,为什么疫苗接种消息在预约id分配之前打印,我该如何解决这个问题?
发布于 2021-06-11 02:47:27
您的问题是,当patients method run
正在运行时,doctors vaccinate
开始运行,而vaccinate
恰好在run
之前完成,所以您会得到这种输出。这应该是有时会发生的,为了防止这种情况发生,你可以寻找像上面提到的here这样的解决方案。
https://stackoverflow.com/questions/67926148
复制相似问题