跨桥-java多线程问题

跨桥-java多线程问题,java,multithreading,Java,Multithreading,一座只有一条车道的桥连接两个城市。两个城镇的汽车都使用这座桥从一个城市到另一个城市。如果来自两个方向的车辆同时在桥上,桥可能会被阻塞。为了解决这个问题,人们在桥的两侧放置了两个信号灯(绿灯和红灯)(当一个信号灯上有绿灯,另一个信号灯上有红灯)。经过一段时间后,灯光会更改其值(未给出具体值)。一辆汽车过桥后,它会在那个城市停留一段时间,然后再回来,依此类推。。。编写一个描述给定情况并禁止死锁的程序 这是从控制台: The green light is on on the right. Lights

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

这是从控制台:

The green light is on on the right.
Lights are changing.
Car 4 wait on the semaphore.
我想这就是问题所在,左灯也有问题。为什么灯光不经常改变,有没有办法解决这个问题?另外,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();


     }
}