java 进程同步练习

描述:信号量机制实现 十字路口绿灯交替。车辆只有在绿灯,且前车通过路口后,才能通行。

我对多线程操作不熟,出现了很多问题,不清楚怎么让多个方法同时运行,下面是我的尝试

public class Lamp implements Runnable{
    //东西路灯
    public synchronized void lampWE() throws InterruptedException {
        while(Main.lightWE) {
            Main.lightWE = false;
            System.out.println("---------------");
            System.out.println("  东 西 路 绿 灯  \n");
            Thread.sleep(5000);
            Main.lightSN = true;
            System.out.println("\n  东 西 路 红 灯  ");
            System.out.println("---------------");
        }
    }
    //南北路灯
    public synchronized void lampSN() throws InterruptedException {
        while(Main.lightSN) {
            Main.lightSN = false;
            System.out.println("---------------");
            System.out.println("  南 北 路 绿 灯  \n");
            Thread.sleep(5000);
            Main.lightWE = true;
            System.out.println("\n  南 北 路 红 灯  ");
            System.out.println("---------------");
        }
    }
    
    public void run() {
        try {
                lampWE();
                lampSN();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }//run
}


public class Car implements Runnable{    
    //东西方向车辆
    public synchronized void carRunWE() throws InterruptedException {
        while(Main.carWE && Main.lightWE) {
            Main.carWE = false;
            Thread.sleep(1000);
            Main.carWE = true;
            System.out.println("   东西车辆通过  ");
            }
    }
    //南北方向车辆
    public synchronized void carRunSN() throws InterruptedException {
        while(Main.lightSN && Main.carSN) {
            Main.carSN = false;
            Thread.sleep(1000);
            Main.carSN = true;
            System.out.println("   南北车辆通过  ");}
    }
    
    public void run() {
        try {
                carRunWE();
                carRunSN();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }//run
}


public class Main {
    
    static boolean lightWE = true;
    static boolean lightSN = false;
    static boolean carWE = true;
    static boolean carSN = true;
        
    public static void main(String args[]){
        new Thread(new Lamp()).start();
        new Thread(new Car()).start();
    }
}

出现的问题:

img

运行会中断,没有小车通过。

期望的结果是:小车在绿灯亮时显示通行。

while(Main.lightWE) {
 
            System.out.println("---------------");
            System.out.println("  东 西 路 绿 灯  \n");
            Thread.sleep(5000);
            Main.lightWE = false;
            Main.lightSN = true;
            System.out.println("\n  东 西 路 红 灯  ");
            System.out.println("---------------");
        }

当Main.lightWE为false的时候就跳出循环了
while(Main.lightSN) 同理为false也跳出循环了

 //东西路灯
    public synchronized void lampWE() throws InterruptedException {
        while(Main.lightWE) {

            System.out.println("---------------");
            System.out.println("  东 西 路 绿 灯  \n");
            Thread.sleep(5000);
            Main.lightWE = false;
            Main.lightSN = true;
            System.out.println("\n  东 西 路 红 灯  ");
            System.out.println("---------------");
        }
    }
    //南北路灯
    public synchronized void lampSN() throws InterruptedException {
        while(Main.lightSN) {

            System.out.println("---------------");
            System.out.println("  南 北 路 绿 灯  \n");
            Thread.sleep(5000);
            Main.lightSN = false;
            Main.lightWE = true;
            System.out.println("\n  南 北 路 红 灯  ");
            System.out.println("---------------");
        }
    }

img

img