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

使用java多线程依次打印ABC(多种方式)

文章目录

  • 1. 使用synchronized
  • 2. 使用信号量
  • 3. 使用ReentrantLock + Condition
  • 参考链接

我在这里写了一个辅助函数,方便查看结果。

public class SleepUtils {
    public static final void second(long seconds)
    {
        try {
            Thread.sleep(seconds * 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

1. 使用synchronized

/*
 * @Description: 文件描述
 * @Version: 1.0
 * @Author: YueXuanzi
 * @Date: 2024-10-16 19:59:48
 * @LastEditors: YueXuanzi
 * @LastEditTime: 2024-10-17 09:17:39
 * @心得体会: 无
 */
public class SwapABC {
    static Object lock = new Object();
    static Object lock2 = new Object();
    static Object lock3 = new Object();

    public static void soutA(){
        while(true){
            synchronized(lock){
                synchronized(lock2){
                    System.out.println("A");
                    SleepUtils.second(1);
                    lock2.notify();
                    // System.out.println("唤醒B");
                }
                try {
                    // System.out.println("A准备等待");
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public static void soutB(){
        while(true){
            synchronized(lock2){
                synchronized(lock3){
                    System.out.println("B");
                    SleepUtils.second(1);
                    lock3.notify();
                    // System.out.println("唤醒C");
                }
                try {
                    // System.out.println("B准备等待");
                    lock2.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }   
        
    }
    public static void soutC(){
        while(true){
            synchronized(lock3){
                synchronized(lock){
                    System.out.println("C");
                    SleepUtils.second(1);
                    lock.notify();
                    // System.out.println("唤醒A");
                }
                try {
                    // System.out.println("C准备等待");
                    lock3.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        new Thread(()->{soutA();}).start();
        SleepUtils.second(1);
        new Thread(()->{soutB();}).start();
        SleepUtils.second(1);
        new Thread(()->{soutC();}).start();
    }
}

2. 使用信号量

import java.util.concurrent.Semaphore;

public class SwapABCOne {
    private final Semaphore A = new Semaphore(1);

    private final Semaphore B = new Semaphore(0);

    private final Semaphore C = new Semaphore(0);

    public void printA() {
        try {
            A.acquire();
            System.out.println("A");
            B.release();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void printB(){
        try {
            B.acquire();
            System.out.println("B");
            C.release();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void printC(){
        try {
            C.acquire();
            System.out.println("C");
            A.release();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        SwapABCOne swapABCOne = new SwapABCOne();
        new Thread(() -> {
            while(true) {
                swapABCOne.printA();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }).start();

        new Thread(() -> {
            while(true) {
                swapABCOne.printB();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(() -> {
            while(true) {
                swapABCOne.printC();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }).start(); 
    }
}

3. 使用ReentrantLock + Condition

这里就与之前的类似了,我觉得没必要去写了

ReentrantLock lock = new ReentrantLock();
Condition A = lock.newCondition();
A.await();
A.signal();
lock.lock();
lock.unlock();

参考链接

多线程循环打印ABC


http://www.kler.cn/news/350913.html

相关文章:

  • 在线课程管理系统(系统的基础功能,如教师上传课程资料、布置作业,学生提交作业和查看成绩等。)
  • vue导出pdf(页面截图的形式)
  • 循环双链表,将L改造为L=(a1,a3,…,an,a4,a2)
  • 飞睿智能超宽带UWB音频传输模块,超低延迟数据传输,实时音频声音更纯净
  • PyTorch模型参数初始化
  • 【Java】使用iText依赖生成PDF文件
  • 大贤3D家谱-视图操作
  • 网络资源模板--Android Studio 实现记事本App
  • 2025推荐选题|基于MVC的农业病虫害防治平台的设计与实现
  • MongoDB oplog 详解
  • Shiro(认证Authentication)
  • [含文档+PPT+源码等]精品基于Python实现的flask美甲线上预约系统
  • ubuntu20.04运行ORB_SLAM2
  • 010_django基于spark的电力能耗数据分析系统的设计与实现2024_s120960s
  • FPGA实现PCIE采集电脑端视频转SFP光口UDP输出,基于XDMA+GTX架构,提供4套工程源码和技术支持
  • Python中的HTTP高手:如何玩转requests模块
  • 高级java每日一道面试题-2024年10月13日-数据库篇[Redis篇]-怎么保证缓存和数据库数据的一致性?
  • golang从http请求中读取xml格式的body,并转成json
  • [LeetCode] 733. 图像渲染
  • el-date-picker选择时间后标准时间少1小时问题
  • HTML 标签简写及全称
  • 编写自定义dbt通用数据测试
  • 2.Node.js 缓冲器(Buffer)
  • Excel:vba实现批量修改文件名
  • 【pytorch】昇思大模型配置python的conda版本
  • Elasticsearch:Redact(编辑) processor