王小小- 2021-07-07 20:21 采纳率: 100%
浏览 87
已采纳

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

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

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

  • 写回答

2条回答 默认 最新

  • 走一步-再走一步 2021-07-07 21:49
    关注
    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也跳出循环了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。