seamew的妙妙屋seamew的妙妙屋
首页
  • kafka监控
  • my-spring
  • Gitee
  • Github
首页
  • kafka监控
  • my-spring
  • Gitee
  • Github
  • BUG

    • BeanUtils报错
    • Thread的名字问题
  • JAVA

    • Threadlocal
    • 线程池源码分析
  • JS进阶

    • JS开发技巧
  • linux

    • centos防火墙
    • vagrant
    • 正则表达式
  • springboot进阶学习

    • @Configuration和@Bean注解
    • springboot接收参数详解
    • springboot配置多数据源
    • 事务导致多数据源切换失败
  • spring进阶学习

    • aop创建代理基本流程
    • 三级缓存解决循环依赖
  • 云原生

    • 自动化部署
  • 前端开发

    • vue-computed计算属性引发的BUG
  • 大数据

    • kafka事务
    • zookeeper
  • 算法

    • 动态规划

      • 动态规划基础理论
      • 抛骰子和为k的概率
    • 回溯算法

      • used数组是局部还是全局
      • 最优解快速返回
    • 图论

      • dfs简介
    • 多线程

      • 打印ABC
    • 贪心

      • 小于n的最大数字

三个线程轮流打印ABC,打印N次

synchronized锁同步

public void print(String str, int target) throws InterruptedException {
    for (int i = 0; i < n; i++) {
        synchronized (obj) {
            while (state % 3 != target) {
                try {
                    obj.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            state++;
            System.out.print(str);
            obj.notifyAll();
        }
    }
}

lock锁同步

public void print(String str, int target) throws InterruptedException {
    for (int i = 0; i < n; ) {
        try {
            lock.lock();
            while (state % 3 == target) {
                System.out.print(str);
                state++;
                i++;
            }
        } finally {
            lock.unlock();
        }
    }
}
上次更新: 2026/3/21 07:25