Hi I am doing a project and I have reached a part where I am very stuck. I have tried to search for ways to learn how to write the while loop for a busy wait but I haven't found anything and my code just runs as an infinite loop. Can someone help explain to me how a busy waiting loop should work and help me break out of this infinite loop?
The project wants the following to happen: In the morning, after the student wakes up (it will take a random time) he will head to the bathroom to get ready for a new school day. If the bathroom is already taken, the student takes a break (use yield()) and later on he will wait (use busy waiting) for the bathroom to become available. Students will use the bathroom in a First Come First Serve basis (you can use a Boolean array/vector for having them released in order).
public class Student implements Runnable
{
private Random rn = new Random();
private String threadNum;
private volatile boolean bathroomFull = false;
private static long time = System.currentTimeMillis();
private Thread t;
public Student(String studentID)
{
threadNum = studentID;
t = new Thread(this, "Student Thread #"+threadNum);
System.out.println("thread created = " + t);
// this will call run() function
t.start();
}
public void run()
{
int waitTime = rn.nextInt(4000 - 2000 + 1)+2000;
System.out.println( "the current time is " + (System.currentTimeMillis() - time) + "and the wait time is: " +waitTime );
//Student wakes up after random time
while((System.currentTimeMillis()-time) < waitTime)
{
// System.out.println("the remaining sleep time is " + (System.currentTimeMillis()-time));
;
}
int a = rn.nextInt(4000 - 2000 + 1)+2000;
try
{
//System.out.println("I'm going to sleep for " +a + " milliseconds");
Thread.sleep(a);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
//this is the busy wait loop where is the bathroom is full then a thread will yield until it is available
int l = rn.nextInt(10 - 1)+1;
bathroomFull = true;
while(bathroomFull)
{
for(int j = 0; j < l; j++)
{
System.out.println("i am in the bathroom for " + l + "minutes " + Thread.currentThread());
}
Thread.yield();
bathroomFull = false;
//exitBathroom();
}
bathroomFull = true;
This is my main method which allows the user to specify how many student threads they want. And yes i don't understand how to implement the change of the value so that the busy wait while loop can be broken.
public static void main(String args[])
{
int numberOfStudents;
numberOfStudents = Integer.parseInt(JOptionPane.showInputDialog("How many students are there in the university? "));
// System.out.println("there are " + numberOfStudents);
for(int i = 0; i < numberOfStudents; i++)
{
new Student(String.valueOf(i+1));
}
new Teacher();
}