gpt4 book ai didi

java - 机场同步 - 线程卡住

转载 作者:行者123 更新时间:2023-12-01 04:12:45 25 4
gpt4 key购买 nike

我想对机场监视器进行编码(试图到达的飞机、试图起飞的飞机等),但我遇到了一些问题。似乎只有一个线程在工作,其他线程卡在某处。有人可以看看这段代码并提供帮助吗?

public class Lotniskowiec {

public int K=5;
public int N = 10;

final Lock lock = new ReentrantLock();
final Condition toStart = lock.newCondition();
final Condition toLand= lock.newCondition();
boolean wantsToStart;
boolean wantsToLand;
int atAirport= 0;
boolean free= true;

private void free_landing_area(){

lock.lock();
if(atAirport< K){
if(wantsToLand){
toLand.signal();
}else toStart.signal();
}
else{
if(wantsToStart){
toStart.signal();
} else if (atAirport< N){
toLand.signal();
}
}
lock.unlock();
}

public void wants_to_start(){

lock.lock();
if(!free){
lock.unlock();
try {
toStart.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
lock.lock();
free=false;
lock.unlock();


}

public void started(){

lock.lock();
atAirport-=1;
free=true;

free_landing_area();
lock.unlock();

}

public void wants_to_land(){

lock.lock();
if(!free|| atAirport==N){
lock.unlock();
try {
toLand.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
lock.lock();
free=false;
lock.unlock();
}

public void landed(){

lock.lock();
atAirport+=1;
free=true;

free_landing_area();
lock.unlock();
}
}

对变量名称感到抱歉;)

线程:

public class Samolot implements Runnable{

Random random = new Random();
Lotniskowiec lotniskowiec = new Lotniskowiec();
int id;

public Samolot(int id, Lotniskowiec lotniskowiec){
this.id=id;
this.lotniskowiec=lotniskowiec;
}

@Override
public void run() {
while(true){
try {
Thread.sleep(random.nextInt(1000));
Lotniskowiec.wants_to_land();
System.out.println(id + " chce ladowac");
Thread.sleep(random.nextInt(1000));
Lotniskowiec.landed();
System.out.println(id + " wyladowal");
Thread.sleep(random.nextInt(1000));
Lotniskowiec.wants_to_start();
System.out.println(id + " chce startowac");
Thread.sleep(random.nextInt(1000));
Lotniskowiec.started();
System.out.println(id + " wystartowal");
} catch (InterruptedException e) {
e.printStackTrace();
}

}
}



}

启动线程:

Samolot r = new Samolot(1,lotniskowiec);
Thread t = new Thread(r);
t.start();

最佳答案

您的 wants_to_landwants_to_start 是 boolean 值,但它们应该是整数。否则你如何能够跟踪有多少架飞机希望降落?

此外,我怀疑在飞机调用的每个方法的开头使用 lock.lock() ,在每个方法的末尾使用 lock.unlock() 就足够了它。在您当前的代码中,平面获取锁的次数多于释放锁的次数,从而有效地从其他平面窃取锁。

此外,它可能有助于使锁公平。

所以

    public static int K = 5;
public static int N = 10;

final static Lock lock = new ReentrantLock(true);
final static Condition toStart = lock.newCondition();
final static Condition toLand = lock.newCondition();
static int wantsToStart = 0;
static int wantsToLand = 0;
static int atAirport = 0;
static boolean free = true;

private static void free_landing_area() {
if (atAirport < K) {
if (wantsToLand > 0) {
toLand.signal();
} else {
toStart.signal();
}
} else {
if (wantsToStart > 0) {
toStart.signal();
} else if (atAirport < N) {
toLand.signal();
}
}
}

public static void wants_to_start() {
lock.lock();
if (!free) {
try {
wantsToStart++;
toStart.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
free = false;
lock.unlock();
}

public static void started() {
lock.lock();
atAirport -= 1;
free = true;
wantsToStart--;
free_landing_area();
lock.unlock();
}

public static void wants_to_land() {
lock.lock();
if (!free || atAirport == N) {
try {
wantsToLand++;
toLand.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
free = false;
lock.unlock();
}

public static void landed() {
lock.lock();
atAirport += 1;
free = true;
wantsToLand--;
free_landing_area();
lock.unlock();
}

最后,我相信您实际上需要在 while 循环中等待(),因为在某些情况下线程可能会被意外唤醒。

When waiting upon a Condition, a "spurious wakeup" is permitted to occur, in general, as a concession to the underlying platform semantics. This has little practical impact on most application programs as a Condition should always be waited upon in a loop, testing the state predicate that is being waited for.

所以上面的代码还没有完成。但这并不是让你的线程卡住的原因。

关于java - 机场同步 - 线程卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19759874/

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