嗨,我正在做一个项目,但我已经到了一个非常困难的地方。我试图寻找方法来学习如何为繁忙的等待编写 while 循环,但我没有找到任何东西,我的代码只是作为无限循环运行。有人可以帮我解释一下繁忙的等待循环应该如何工作并帮助我摆脱这个无限循环吗?
该项目希望发生以下情况:早上,学生醒来后(需要随机时间),他将前往洗手间为新的上学日做准备。如果卫生间已经被占用,学生会休息一下(使用 yield()),稍后他将等待(使用忙等待)等待卫生间可用。学生将按照先到先得的原则使用浴室(您可以使用布尔数组/向量让它们按顺序释放)。
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;
这是我的主要方法,它允许用户指定他们想要多少个学生线程。是的,我不明白如何实现值的更改,以便可以打破繁忙的等待循环。
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();
}