gpt4 book ai didi

java - 使用 Java Monitor 的简单程序

转载 作者:太空宇宙 更新时间:2023-11-04 11:24:52 25 4
gpt4 key购买 nike

我有一个简单的 Java 程序,它利用监视器让顾客进入登机区。我认为我将 wait() 和 notification() 语句放置在错误的位置,导致程序死锁,但是,我自己无法弄清楚。下面是我编写的代码。

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)
{

}
}
}

最佳答案

问题在于您在不同的对象上进行同步。当你写这样的东西时

public synchronized foo() {...}

您在该对象上进行同步。在您的情况下,您在当前线程上同步,这没有任何意义。您应该在同一对象上同步。

public class Main {

public static Object lock = new Object();
...
}

public class AdultCouple extends Thread
{
private boolean onRide = false;
private int ID;

AdultCouple(int ID)
{
this.ID = ID;
}

public void getIn()
{
synchronized (Main.lock) {
while (!Main.isDoorOpen) {
try {
Main.lock.wait();
} catch (InterruptedException ex) {

}
System.out.println("Go through");
}
System.out.println("Couple " + ID + " get in boarding area.");
onRide = true;
Main.peopleInBoardingArea++;
Main.lock.notifyAll();
}
}

public void run()
{
getIn();
}
}

public class Area extends Thread
{

Area()
{
}

public void openDoor()
{
synchronized (Main.lock) {
while (Main.peopleInBoardingArea != 0) {
try {
Main.lock.wait();
} catch (InterruptedException ex) {
}
}
System.out.println("Area opens");
Main.isDoorOpen = true;
Main.lock.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 void getIn()
{
synchronized (Main.lock) {
while (!Main.isDoorOpen) {
try {
Main.lock.wait();
} catch (InterruptedException ex) {

}
System.out.println("Go through");
}
System.out.println("Kid " + ID + " get in boarding area.");
onRide = true;
Main.peopleInBoardingArea++;
Main.lock.notifyAll();
}
}


public void run()
{
getIn();
}

}

也不要使用notify(),而是使用notifyAll()。我看不出有任何理由使用它

关于java - 使用 Java Monitor 的简单程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44491394/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com