当前位置: 首页 > article >正文

三个线程按顺序交替打印 A B C

方法一:ReentrantLock+Condition

public static void method1() {
        ReentrantLock lock = new ReentrantLock();
        Condition condA = lock.newCondition();
        Condition condB = lock.newCondition();
        Condition condC = lock.newCondition();

        new Thread(() -> {
            while (true) {
                lock.lock();
                try {
                    condB.signal();
                    System.out.println("A");
                    Thread.sleep(1000);
                    condA.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } finally {
                    lock.unlock();
                }
            }
        }).start();

        new Thread(() -> {
            while (true) {
                lock.lock();
                try {
                    condC.signal();
                    System.out.println("B");
                    Thread.sleep(1000);
                    condB.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } finally {
                    lock.unlock();
                }
            }
        }).start();

        new Thread(() -> {
            while (true) {
                lock.lock();
                try {
                    condA.signal();
                    System.out.println("C");
                    Thread.sleep(1000);
                    condC.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } finally {
                    lock.unlock();
                }
            }
        }).start();
    }

方法二:synchronized

public static void method2() {

        Object lock = new Object();
        new Thread(() -> {
            while (true) {
                synchronized (lock) {
                    while (state != 1) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.println("A");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    state = 2;
                    lock.notifyAll();
                }
            }
        }).start();

        new Thread(() -> {
            while (true) {
                synchronized (lock) {
                    while (state != 2) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.println("B");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    state = 3;
                    lock.notifyAll();
                }
            }
        }).start();

        new Thread(() -> {
            while (true) {
                synchronized (lock) {
                    while (state != 3) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.println("C");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    state = 1;
                    lock.notifyAll();
                }
            }
        }).start();
    }

方法三:Semaphore

public static void method3() {
        Semaphore semaphoreA = new Semaphore(1);
        Semaphore semaphoreB = new Semaphore(0);
        Semaphore semaphoreC = new Semaphore(0);

        new Thread(() -> {
            while (true){
                try {
                    semaphoreA.acquire();
                    System.out.println("A");
                    Thread.sleep(1000);
                    semaphoreB.release();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();

        new Thread(() -> {
            while (true){
                try {
                    semaphoreB.acquire();
                    System.out.println("B");
                    Thread.sleep(1000);
                    semaphoreC.release();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();

        new Thread(() -> {
            while (true){
                try {
                    semaphoreC.acquire();
                    System.out.println("C");
                    Thread.sleep(1000);
                    semaphoreA.release();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();
    }

http://www.kler.cn/a/590987.html

相关文章:

  • 【玩转正则表达式】Python、Go、Java正则表达式解释器的差异解析(附示例)
  • GitHub Copilot两期连看:开发流程全览及 Copilot 在 SQL 开发中的妙用
  • 【QT:多线程、锁】
  • OceanBase 读写分离最佳实践
  • MySQL原理:逻辑架构
  • 开源模型中的 Function Call 方案深度剖析
  • qwen2.5-vl复现日志
  • Certbot实现SSL免费证书自动续签(CentOS 7 + nginx/apache)
  • Python刷题:流程控制(上)
  • Pytest项目_day01(HTTP接口)
  • C++八大常见的设计模式的实现与实践指南
  • 服务器防火墙根据什么特征来过滤数据包?
  • H3C SecPath SysScan-AK 系列漏洞扫描系统
  • vue中根据html动态渲染内容
  • 设备物联网无线交互控制,ESP32无线联动方案,产品智能化响应
  • 基于SpringBoot的Mybatis和纯MyBatis项目搭建的区别
  • docker 安装 nginx 部署Vue前端项目
  • AOP+Nacos实现动态数据源切换
  • 超详细kubernetes部署k8s----一台master和两台node
  • AK 接口