1

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

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();
4

2 回答 2

0

好的,这是线索:其他线程被锁定在 toLand.await() 语句的 Wants_to_land 方法中。toLand.signal() 永远不会发生,因为 if(wantsToLand) 和 if(wantsToStart) 总是错误的(你不会在任何地方改变它)。考虑刷新同步逻辑,看看是否有帮助。

于 2013-11-04T00:54:37.203 回答
0

你的wants_to_landandwants_to_start是布尔值,但它们应该是整数。否则,您将如何跟踪希望降落的飞机数量?

另外,我怀疑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 循环中等待(),因为在某些情况下线程可能会被意外唤醒。

在等待条件时,通常允许发生“虚假唤醒”,作为对底层平台语义的让步。这对大多数应用程序几乎没有实际影响,因为应始终在循环中等待条件,测试正在等待的状态谓词。

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

于 2013-11-04T01:04:17.780 回答