1

一座只有一条车道的桥连接了两个城市。两个城镇的汽车都使用那座桥从一个城市到另一个城市。如果来自两个方向的汽车同时在桥上,则可能会阻塞桥。为了解决这个问题,人们在桥的两侧放置了两个信号灯(绿灯和红灯)(一个信号灯是绿灯,另一个信号灯是红灯)。经过一段时间后,灯会改变它们的值(具体值没有给出)。一辆车过桥后,它会在那个城市停留一段时间,然后回来等等......编写一个描述给定情况并禁止死锁的程序。

这是来自控制台:

The green light is on on the right.
Lights are changing.
Car 4 wait on the semaphore.

我认为这是问题所在,左侧灯也有问题。为什么灯不经常变化,有没有办法解决这个问题?另外,Car 4 是有问题的,它为什么要等待信号量呢?这是我在这个网站上的第一个问题,如果没有很好地放在一起,请见谅。如果有人能让我知道这是否是一个好方法,我将不胜感激。提前感谢。

当我运行程序时

public class Car extends Thread{

    int id;
    int direction;
    Bridge b;
    
    public Car(int id, int dir, Bridge b) {
        this.id = id;
        this.direction = dir;
        this.b = b;
    }
    
    public void crossOver() {
        int city1 = 1, city2 = 2;
        if (this.direction == 2) {
            city1 = 2;
            city2 = 1;
        } 
        try {
            System.out.println("Car "+((Car)Thread.currentThread()).id+" is crossing over the bridge"
                    + " from city "+city1+" in city "+city2+"."
                    );
            sleep(500);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public void run() {
        while(true) {
            try {
                sleep(1);
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            b.waitB(this.direction);
            crossOver();
            int city = 1;
            if(this.direction == 1)
                city = 2;
            System.out.println("Car "+this.id+" is in the city "+ city);
            try {
                sleep(500);
            } catch (InterruptedException e) {
            }
            if (this.direction == 1) this.direction++;
            else this.direction--;
       }
    }
}

public class Bridge extends Thread{

Semaphore s1, s2;
    
    public Bridge(Semaphore s1,Semaphore s2) {
        this.s1 = s1;
        this.s2 = s2;
    }
    
    public synchronized void turnOnSemaphoreLeft() {
        s1.color = 1;
        s2.color = 0;
        System.out.println("The green light is on on the left.");
        notifyAll();
         
    }
    public synchronized void turnOnSemaphoreRight() {
        s2.color = 1;
        s1.color = 0;
        System.out.println("The green light is on on the right.");
        notifyAll();
    }
    
    public void run() {
        while(true) {
              turnOnSemaphoreLeft();
              System.out.println("Lights are changing.");
              try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
              
           
              turnOnSemaphoreRight();
              System.out.println("Lights are changing.");
              try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    
    public synchronized void waitB(int direction) {
        if (direction == 1) {
            if (s1.color == 0) {
                try {
                    System.out.println("Car "+((Car)Thread.currentThread()).id+" wait on the semaphore.");
                    wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } 
            }
        }
        else if (direction == 2) {
            if (s2.color == 0) {
                try {
                    System.out.println("Car "+((Car)Thread.currentThread()).id+" wait on the semaphore.");
                    wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    
}

public class Semaphore () {

    int color; //if color = 1 than is green light, if color = 0 than is red light 
}

public class Main() {
     public static void main(String[] args) {
        Semaphore s1 = new Semaphore();
        Semaphore s2 = new Semaphore();
        
        Bridge m = new Bridge(s1,s2);
        
        Car a1 = new Car(1, 1, m);
        Car a2 = new Car(2, 2, m);
        Car a3 = new Car(3, 2, m);
        Car a4 = new Car(4, 1, m);
        Car a5 = new Car(4, 2, m);
        Car a6 = new Car(4, 1, m);
        Car a7 = new Car(4, 2, m);
        
        m.start();
        
        a1.start();
        a2.start();
        a3.start();
        a4.start();
        a5.start();
        a6.start();
        a7.start();


     }
}
4

0 回答 0