当前位置: 首页 > 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();
    }

相关文章:

  • 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靶机实战攻略
  • 夜读丨母亲为燕子打开家门
  • 为什么越来越多景区,把C位留给了书店?
  • 德国总理默茨发表首份政府声明:将提升国防能力,全力发展经济
  • 特朗普访问卡塔尔,两国签署多项合作协议
  • 阿尔巴尼亚执政党连续第四次赢得议会选举,反对党此前雇用特朗普竞选经理
  • 王毅谈中拉论坛第四届部长级会议重要共识