1
我有這個簡單的Java程序,利用監視器,讓客戶進入登機區。我想我把wait()和notify()語句放在導致程序死鎖的錯誤位置,但是,我無法自己弄清楚它。以下是我寫的代碼。簡單的程序使用Java監視器
public class AdultCouple extends Thread
{
private boolean onRide = false;
private int ID;
AdultCouple(int ID)
{
this.ID = ID;
}
public synchronized void getIn()
{
while (!Main.isDoorOpen)
{
try
{
wait();
}
catch (InterruptedException ex)
{
}
System.out.println("Go through");
}
System.out.println("Couple " + ID + " get in boarding area.");
onRide = true;
Main.peopleInBoardingArea++;
notify();
}
public void run()
{
getIn();
}
}
public class Area extends Thread
{
Area()
{
}
public synchronized void openDoor()
{
while (Main.peopleInBoardingArea != 0)
{
try
{
wait();
}
catch (InterruptedException ex)
{
}
}
System.out.println("Area opens");
Main.isDoorOpen = true;
notifyAll();
}
public synchronized void closeDoor()
{
}
public void run()
{
openDoor();
}
}
public class ParentKid extends Thread
{
private boolean onRide = false;
private int ID;
ParentKid(int ID)
{
this.ID = ID;
}
public synchronized void getIn()
{
while (!Main.isDoorOpen)
{
try
{
wait();
}
catch (InterruptedException ex)
{
}
System.out.println("Go through");
}
System.out.println("Couple " + ID + " get in boarding area.");
onRide = true;
Main.peopleInBoardingArea++;
notify();
}
public void run()
{
getIn();
}
}
public class Main
{
public static boolean isDoorOpen = false;
public static int peopleInBoardingArea = 0;
public static void main(String args[])
{
Thread t3 = new Area();
Thread t1 = new ParentKid(1);
Thread t2 = new AdultCouple(2);
t1.start();
t2.start();
t3.start();
try
{
t1.join();
t2.join();
t3.join();
}
catch (InterruptedException ex)
{
}
}
}
一個問題是,您正在使用對「Main」類的靜態字段的非同步訪問。即使將它們更改爲'volatile'也不適用於'++'操作符。 – Andreas