java - 使用 Java 线程的简单交通灯模拟

标签 java multithreading

如问题所述,我正在尝试对四向交叉路口进行非常非常简单的交通灯模拟。我的问题是,我正在使用两个不同的线程,并试图在 for 循环内使用 wait 和 notify 在这两个线程之间来回跳动。我在下面有一些伪代码。

public class Processor 
{
    public static int CarCounter = 1;

    public void lightOneAndTwo() throws InterruptedException
    {
        synchronized(this)
        {
            for(int carsOne = 0; carsOne < 50; carsOne++)
            {
                //Light One---------------------------->


                //Light Two---------------------------->

                wait();
            }
        }
    }
    public void lightThreeAndFour() throws InterruptedException
    {
        Thread.sleep(1000);

        synchronized(this)
        {
            for(int carsTwo = 0; carsTwo < 50; carsTwo++ )
            {
                //Light Three---------------------------->

                notify();

            }
        }
    }
}

但是当我调用通知时,它不会重新激活第一个线程。我做错了什么还是有更好的方法?

最佳答案

您的程序有多个问题 - 没有条件变量、没有用于检查条件的 while 循环等。以下是一种可能的解决方案。我有四个类(class):

MyLock - (Lock Object)

package com.test;

public class MyLock {

    private volatile boolean condition;

    public MyLock(boolean condition) {
        this.condition = condition;
    }

    public boolean condition() {
        return condition;
    }

    public void flipCondition() {
        condition = !condition;
    }
}

TrafficLightWorkerOne (Traffic Worker)

package com.test;

public class TrafficLightWorkerOne implements Runnable {

    private int cars;
    private MyLock lock;

    public TrafficLightWorkerOne(MyLock lock, int cars) {
        this.lock = lock;
        this.cars = cars;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (lock) {
                while (!lock.condition()) {
                    for (int carsOne = 0; carsOne < cars; carsOne++) {

                        System.out.println(Thread.currentThread().getName()
                                + " car no : " + carsOne);
                        // Light One---------------------------->

                        // Light Two---------------------------->
                    }
                    lock.notifyAll();
                    lock.flipCondition();
                }
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}

TrafficLightWorkerTwo (Another Traffic Worker)

package com.test;

public class TrafficLightWorkerTwo implements Runnable {

    private int cars;
    private MyLock lock;

    public TrafficLightWorkerTwo(MyLock lock, int cars) {
        this.lock = lock;
        this.cars = cars;
    }

    @Override
    public void run() {

        while (true) {
            synchronized (lock) {
                try {
                    while (!lock.condition()) {
                        lock.wait();
                    }
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                for (int carsOne = 0; carsOne < cars; carsOne++) {

                    System.out.println(Thread.currentThread().getName()
                            + " car no : " + carsOne);
                    // Light One---------------------------->

                    // Light Two---------------------------->
                }
                lock.flipCondition();;
                lock.notifyAll();
            }
        }

    }
}

TrafficLightSimulator (Main Class)

package com.test;

public class TrafficLightSimulator {

    public static void main(String[] args) {
        boolean  condition = false; 
        MyLock lock = new MyLock(condition);
        Thread threadOne = new Thread(new TrafficLightWorkerOne(lock, 5), "One");
        Thread threadTwo = new Thread(new TrafficLightWorkerTwo(lock, 4), "Two");

        threadOne.start();
        threadTwo.start();

    }
}

关于java - 使用 Java 线程的简单交通灯模拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30950467/

相关文章:

Java从ListView的多个项目中获取数据

java - 混合使用 Groovy 和 Java?

java - Maven 正在拉取旧版本的 HTMLUnit,即使我指定了最新的(?)

java - 同步方法是否会阻止对象字段被更新?

java - Netbeans Spring Boot Initializr 项目 : Unable to see jsp content in web browser

java - 简单介绍java游戏编程

java - 如何向 Java 线程传递参数?

c - 事件未使用 ResetEvent() 重置

c# - 如何以随机间隔将执行挂起一段随机时间

c++ - 在wxWidgets中创建一个全局的wxCriticalSection变量不好吗?