首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

交通信号在Java中的多线程实现

可以通过使用线程和锁机制来实现。下面是一个简单的示例:

代码语言:txt
复制
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class TrafficSignal {
    private final Lock lock = new ReentrantLock();
    private final Condition greenLight = lock.newCondition();
    private final Condition yellowLight = lock.newCondition();
    private final Condition redLight = lock.newCondition();
    private Signal currentSignal = Signal.GREEN;

    public void changeToGreen() {
        lock.lock();
        try {
            while (currentSignal != Signal.YELLOW) {
                greenLight.await();
            }
            currentSignal = Signal.GREEN;
            System.out.println("Green light is on.");
            yellowLight.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void changeToYellow() {
        lock.lock();
        try {
            while (currentSignal != Signal.RED) {
                yellowLight.await();
            }
            currentSignal = Signal.YELLOW;
            System.out.println("Yellow light is on.");
            redLight.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void changeToRed() {
        lock.lock();
        try {
            while (currentSignal != Signal.GREEN) {
                redLight.await();
            }
            currentSignal = Signal.RED;
            System.out.println("Red light is on.");
            greenLight.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    private enum Signal {
        GREEN,
        YELLOW,
        RED
    }
}

在上述示例中,TrafficSignal类使用了ReentrantLock和Condition来实现多线程控制。通过lock和unlock方法来获取和释放锁,使用await和signal方法来实现线程的等待和唤醒。

使用示例:

代码语言:txt
复制
public class Main {
    public static void main(String[] args) {
        TrafficSignal trafficSignal = new TrafficSignal();

        Thread greenThread = new Thread(() -> {
            while (true) {
                trafficSignal.changeToGreen();
                try {
                    Thread.sleep(5000); // 绿灯持续5秒
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        Thread yellowThread = new Thread(() -> {
            while (true) {
                trafficSignal.changeToYellow();
                try {
                    Thread.sleep(2000); // 黄灯持续2秒
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        Thread redThread = new Thread(() -> {
            while (true) {
                trafficSignal.changeToRed();
                try {
                    Thread.sleep(3000); // 红灯持续3秒
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        greenThread.start();
        yellowThread.start();
        redThread.start();
    }
}

在上述示例中,创建了三个线程分别控制交通信号的绿灯、黄灯和红灯。每个线程通过调用TrafficSignal类中的相应方法来改变交通信号的状态,并且设置了不同的持续时间。

这个多线程实现可以用于模拟交通信号灯的运行,可以根据实际需求进行扩展和优化。

腾讯云相关产品和产品介绍链接地址:

  • 腾讯云云服务器(CVM):https://cloud.tencent.com/product/cvm
  • 腾讯云容器服务(TKE):https://cloud.tencent.com/product/tke
  • 腾讯云云原生数据库TDSQL:https://cloud.tencent.com/product/tdsql
  • 腾讯云CDN加速:https://cloud.tencent.com/product/cdn
  • 腾讯云云安全中心:https://cloud.tencent.com/product/ssc
  • 腾讯云人工智能平台(AI Lab):https://cloud.tencent.com/product/ai
  • 腾讯云物联网平台(IoT Hub):https://cloud.tencent.com/product/iothub
  • 腾讯云移动开发平台(MPS):https://cloud.tencent.com/product/mps
  • 腾讯云对象存储(COS):https://cloud.tencent.com/product/cos
  • 腾讯云区块链服务(BCS):https://cloud.tencent.com/product/bcs
  • 腾讯云虚拟现实(VR):https://cloud.tencent.com/product/vr
页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

10分3秒

65-IOC容器在Spring中的实现

16分13秒

06.在ListView中实现.avi

6分31秒

07.在RecyclerView中实现.avi

59分41秒

如何实现产品的“出厂安全”——DevSecOps在云开发运维中的落地实践

33分30秒

Java零基础-299-多态在开发中的作用

13分55秒

day24_集合/09-尚硅谷-Java语言高级-HashMap在JDK7中的底层实现原理

5分47秒

day24_集合/10-尚硅谷-Java语言高级-HashMap在JDK8中的底层实现原理

13分55秒

day24_集合/09-尚硅谷-Java语言高级-HashMap在JDK7中的底层实现原理

5分47秒

day24_集合/10-尚硅谷-Java语言高级-HashMap在JDK8中的底层实现原理

13分55秒

day24_集合/09-尚硅谷-Java语言高级-HashMap在JDK7中的底层实现原理

5分47秒

day24_集合/10-尚硅谷-Java语言高级-HashMap在JDK8中的底层实现原理

25分40秒

基于HTTP请求的多线程实现类

23.9K
领券