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

同步模式之顺序控制,如何控制三个线程依次输出: abc abc abc abc abc?

1.通过 synchronized wait notifyAll 控制三个线程 依次输出 abc abc abc abc abc

	private static Thread t1, t2, t3;
	
	// 用于控制当前哪一个线程执行, state = 0时 t1线程执行, state = 1时 t2线程执行,state = 2时 t3线程执行
    private static int state = 0;

	@Test
    public void test3() throws InterruptedException {
        t1 = new Thread(() -> {
            synchronized (lock) {
                for (int i = 0; i < 5; i++) {
                    while (state != 0) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    log.info("---------------" + i + "-------------------");
                    log.info("a");
                    state = 1;
                    lock.notifyAll();
                }
            }
        });

        t2 = new Thread(() -> {
            synchronized (lock) {
                for (int i = 0; i < 5; i++) {
                    while (state != 1) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    log.info("b");
                    state = 2;
                    lock.notifyAll();
                }
            }
        });

        t3 = new Thread(() -> {
            synchronized (lock) {
                for (int i = 0; i < 5; i++) {
                    while (state != 2) {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    log.info("c");
                    state = 0;
                    lock.notifyAll();
                }
            }
        });

        t1.start();
        t2.start();
        t3.start();

        t1.join();
        t2.join();
        t3.join();

    }

2.通过 ReenTrantLock 控制三个线程 依次输出 abc abc abc abc abc

	
	private static Thread t1, t2, t3;

    // 用于控制当前哪一个线程执行, state = 0时 t1线程执行, state = 1时 t2线程执行,state = 2时 t3线程执行
    private static int state = 0;

    private static final Lock reentrantLock = new ReentrantLock();

    private static final Condition[] conditions = new Condition[3];
	
	@Test
    public void test4() throws InterruptedException {
        for (int i = 0; i < 3; i++) {
            conditions[i] = reentrantLock.newCondition();
        }

        Thread t1 = new Thread(() -> {
            reentrantLock.lock();
            try {
                for (int i = 0; i < 5; i++) {
                    while (state != 0) {
                        conditions[0].await();
                    }
                    log.info("---------------" + i + "-------------------");
                    log.info("a");
                    state = 1;
                    conditions[1].signal();
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } finally {
                reentrantLock.unlock();
            }
        });

        Thread t2 = new Thread(() -> {
            reentrantLock.lock();
            try {
                for (int i = 0; i < 5; i++) {
                    while (state != 1) {
                        conditions[1].await();
                    }
                    log.info("b");
                    state = 2;
                    conditions[2].signal();
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } finally {
                reentrantLock.unlock();
            }
        });

        Thread t3 = new Thread(() -> {
            reentrantLock.lock();
            try {
                for (int i = 0; i < 5; i++) {
                    while (state != 2) {
                        conditions[2].await();
                    }
                    log.info("c");
                    state = 0;
                    conditions[0].signal();
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } finally {
                reentrantLock.unlock();
            }
        });

        t1.start();
        t2.start();
        t3.start();

        t1.join();
        t2.join();
        t3.join();

    }

3.通过 park unpark 控制三个线程 依次输出 abc abc abc abc abc

	
	private static Thread t1, t2, t3;

	@Test
    public void test5() throws InterruptedException {

        t1 = new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                log.info("---------------" + i + "-------------------");
                log.info("a");
                LockSupport.unpark(t2);
                LockSupport.park();
            }
        });

        t2 = new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                LockSupport.park();
                log.info("b");
                LockSupport.unpark(t3);
            }
        });

        t3 = new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                LockSupport.park();
                log.info("c");
                LockSupport.unpark(t1);
            }
        });

        t1.start();
        t2.start();
        t3.start();

        t1.join();
        t2.join();
        t3.join();

    }
http://www.dtcms.com/a/60488.html

相关文章:

  • 本地部署Navidrome个人云音乐平台随时随地畅听本地音乐文件
  • 无刷电动智能充气泵方案【天吉智芯】
  • 【软件逆向】QQ 连连看小游戏去广告与一键消除实现
  • 数据量过大的时候导出数据很慢
  • 蓝桥杯备赛-基础训练(四)字符串 day17
  • 初识Spring Batch:开启批处理的高效之旅
  • 软件性能测试与功能测试联系和区别
  • 采用面向对象方式计算三角形面积 - V2.0
  • 【C语言】考研复试上机代码题(基础篇)
  • 经销商管理系统选型解析:8款产品详评
  • 惊喜操作!强制DeepSeek和豆包轻松生成你的专属图片提示词
  • MySQL(社区版)安装过程
  • NAT NAPT
  • RK3568 SD卡调试记录
  • listen EACCES: permission denied 0.0.0.0:811
  • AI Agent开发框架分析:前端视角
  • C++ Qt开发成长之路,从入门到企业级实战项目,保姆级学习路线
  • 机器学习——逻辑回归实战2——预测拖欠款
  • 春风有信 温暖传情---平安养老险陕西分公司开展妇女节特别活动
  • Spring上下文工具类
  • 语文-文章题材
  • 02_LangChain整合DeepSeek提示词工程应用实践
  • 深入理解pytest框架中的conftest.py:使用与作用原理
  • 探索AI对冲基金:开源自动化交易系统的革新之路
  • 配置Hadoop集群
  • Markdown编辑器使用指南(适用于Typora)
  • 牛客周赛 Round 84——小红的陡峭值(四)
  • FPGA入门教程
  • Gini系数的应用 - 指标波动贡献分析
  • 【Matlab仿真】如何解决三相交流信号源输出波形失真问题?