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

三个线程按顺序交替打印 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.dtcms.com/a/75963.html

相关文章:

  • Power Apps 技术分享:使用控件的相对布局
  • 组态王Kingview配置为OPCUA服务器的一些问题处理
  • [快乐学坊management_1] With Cursor | Mysql设计 | 服务接口设计与开发
  • PyTorch 深度学习实战(17):Asynchronous Advantage Actor-Critic (A3C) 算法与并行训练
  • ABeam 德硕 | 在华外企ESG议题选择指南(5)—— 国际与国内ESG议题选择研究:SASB可持续会计准则解读
  • Unity 云渲染本地部署方案
  • 检查 YAML 文件格式是否正确的命令 yamllint
  • Vala编程语言教程-数据类型
  • 【iOS】SwiftUI 路由管理(NavigationStack)
  • 深入理解 Linux 的 top 命令:实时监控系统性能
  • Unity 解决TMP_Text 文字显示异常的问题
  • 手势调控屏幕亮度:Python + OpenCV + Mediapipe 打造智能交互体验
  • 记事本(基于JAVAGUI界面)
  • 一次模拟Windows挖矿病毒应急响应的流程及思路
  • Linux系统管理与编程05:网络管理番外篇
  • 一篇最全Python 爬虫超详细讲解(零基础入门,适合小白)
  • AUTOSAR Communication Services - COM:(一)COM相关功能、API整理与序列图
  • 文献阅读篇#1:C会/期刊的改进YOLO论文应放弃即插即用,至少要学会简单融合拼接(1)
  • 逐光之路:我在特种设备作业考试中的成长蜕变
  • Joker靶机实战攻略
  • 逻辑派G1 6层高速板学习
  • 连接不上雷电模拟器,adb连接不上问题的解决办法
  • Billu_b0x靶机实战攻略
  • JS逆向案例-HIKVISION-视频监控的前端密码加密分析
  • 分享一个项目中遇到的一个算法题
  • SPI 总线协议
  • Pytest项目_day01(HTTP接口)
  • 微服务即时通信系统---(五)框架学习
  • 【Spring】第四弹:基于XML文件注入Bean对象
  • IDEA2022自动构建注册表没有compiler.automake.allow.when.app.running选项解决方法