Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a multithreaded Java program for patient room access control in a hospital

ID: 3820355 • Letter: W

Question

Write a multithreaded Java program for patient room access control in a hospital. The controller must obey the following simple rules:

1. Doctors can enter the room one at a time, and can enter only if no visitors are in the room.

2. A visitor can only enter the room if no doctors are in the room and if a maximum of 3 other visitors are in the room (four in total)

. 3. When a doctor arrives, he must wait for all the current visitors in the room to leave the room.

4. When a doctor arrives, no more visitors are allowed in, until a waiting doctor enters and leaves the room

. 5. When a doctor leaves the room, only one waiting doctor or up to four waiting visitors can enter the room, depending on who comes first.

You shall do this by creating and synchronizing the following threads:

1. A random number less than 1500 of threads representing visitors who wish to visit the patient in his room.

2. A random number less than 750 of threads representing doctors who want to see the patient in his room.

3. One display thread which watches the room status and periodically prints out the current number of doctors and visitors in the patient room.

Requirements:

• Use a Java monitor for both synchronization and mutual exclusion

• Implement mutual exclusion where appropriate

• Avoid busy-waiting, starvation and deadlocks

• Each doctor and visitor thread must pause for a random period (up to 1s) before entering the room.

• Each doctor and visitor thread must pause for a random period (up to 1s) before exiting the room.

• The display thread shall continue working until it is interrupted by the user.

The output from the display thread must clearly show that your controller is working as specified above.

Study the output of your program carefully to check it is operating and synchronizing properly, without any deadlock or starvation.

Explanation / Answer

public class demo {

public static void main (String args[])

{
Hospital h1= new Hospital("xyz");
int choice=0;
Doctor o = new Doctor("zzz","Surgeon");
Doctor o1 = new Doctor("zzzzzz","Opthalmologist");
Doctor o2 = new Doctor("zzzzzzz","ENT");
System.out.println("Press 1 to add doctor Press 2 to show doctors. Press 3 to add patient 4 Assign doctor to patients 5 Doctor and their patients ");
Scanner cin = new Scanner (System.in);
choice = cin.nextInt();
switch (choice)
{
case 1: h1.addDoctor(o);
h1.addDoctor(o1);
h1.addDoctor(o2);
// break;
case 2: { System.out.println(h1.showDoctors());

}
case 3: {   
Patient p4= new Patient ("Je ",15,"Male","heart patient");
h1.addPatient(p);
h1.addPatient(p1);
h1.addPatient(p2);
h1.addPatient(p3);
h1.addPatient(p4);
h1.addPatient(p5);
System.out.println(h1.showPatients());
}   
case 4: {
h1.assignDoctor();
}

case 5: {
System.out.println(" "+ ""+o2.getDoctorName()+""+o2.getDoctorPatientList());
}
}
}}

public class Hospital {

List <Doctor> doctorList = new ArrayList<Doctor>();
List <Patient> patientList = new ArrayList<Patient>();
String hospitalName;
void addDoctor(Doctor o)
{
doctorList.add(o);

}
void addPatient(Patient o)
{
patientList.add(o);
}
Hospital(String name)
{
this.hospitalName=name;
}

public List<Doctor> showDoctors()
{
return doctorList;
}
public List<Patient> showPatients()
{
return patientList;
}

public void assignDoctor()
{
for (Patient x: patientList)
{ for (Doctor y: doctorList)
{ if (x.getDisease().equals("eye"))
{
if (y.getDoctorspeciality().equals("Opthalmologist"))
{
y.addPatientsToDoctor(x);
}
}
if (x.getDisease().equals("heart patient"))
{
if (y.getDoctorspeciality().equals("Surgeon"))
{
y.addPatientsToDoctor(x);
}
}

if (x.getDisease().equals("earnose"))
{
if (y.getDoctorspeciality().equals("ENT"))
{
y.addPatientsToDoctor(x);
}
}

}
}

}


}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote