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

多线程交替打印ABC方法整理

面试常考题,整理几种常见实现,侵删

1. 使用wait()和notify()
public class PrintABCUsingWaitNotify {

    /**
     * 实现步骤:
     * 定义一个共享对象用来同步。
     * 使用wait()让线程进入等待状态。
     * 使用notify()唤醒下一个线程。
     */

    private final Object lock = new Object();
    private int state = 0;  // 0:A 1:B 2:C

    @Test
    public void testPrintABC() {
        PrintABCUsingWaitNotify task = new PrintABCUsingWaitNotify();

        Thread threadA = new Thread(task::printA);
        Thread threadB = new Thread(task::printB);
        Thread threadC = new Thread(task::printC);

        threadA.start();
        threadB.start();
        threadC.start();
    }

    public void printA() {
        synchronized (lock) {
            for (int i = 0; i < 10; i++) {
                while (state != 0) {
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.print("A");
                state = 1;
                lock.notifyAll();
            }
        }
    }

    public void printB() {
        synchronized (lock) {
            for (int i = 0; i < 10; i++) {
                while (state != 1) {
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.print("B");
                state = 2;
                lock.notifyAll();
            }
        }
    }

    public void printC() {
        synchronized (lock) {
            for (int i = 0; i < 10; i++) {
                while (state != 2) {
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.print("C");
                state = 0;
                lock.notifyAll();
            }
        }
    }

}
2. 使用ReentrantLock和Condition
public class PrintABCUsingReentrantLock {

    /**
     * 实现步骤
     * 定义一个ReentrantLock和多个Condition。
     * 每个线程等待相应的Condition,当符合条件时打印字符并唤醒下一个线程。
     */

    private final Lock lock = new ReentrantLock();
    private final Condition conditionA = lock.newCondition();
    private final Condition conditionB = lock.newCondition();
    private final Condition conditionC = lock.newCondition();
    private int state = 0;  // 0:A 1:B 2:C

    @Test
    public void testPrintABC() throws InterruptedException {
        PrintABCUsingReentrantLock task = new PrintABCUsingReentrantLock();

        Thread threadA = new Thread(task::printA);
        Thread threadB = new Thread(task::printB);
        Thread threadC = new Thread(task::printC);

        threadA.start();
        threadB.start();
        threadC.start();
    }

    public void printA() {
        lock.lock();
        try {
            for (int i = 0; i < 10; i++) {
                while (state != 0) {
                    conditionA.await();
                }
                System.out.print("A");
                state = 1;
                conditionB.signal();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void printB() {
        lock.lock();
        try {
            for (int i = 0; i < 10; i++) {
                while (state != 1) {
                    conditionB.await();
                }
                System.out.print("B");
                state = 2;
                conditionC.signal();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void printC() {
        lock.lock();
        try {
            for (int i = 0; i < 10; i++) {
                while (state != 2) {
                    conditionC.await();
                }
                System.out.print("C");
                state = 0;
                conditionA.signal();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

}
3. ReentrantLock和Condition的另一种写法
public class SyncPrinter implements Runnable {

    private final int PRINT_COUNT = 10;
    private final ReentrantLock reentrantLock;
    private final Condition thisCondition;
    private final Condition nextCondition;
    private final char printChar;

    public SyncPrinter(ReentrantLock reentrantLock, Condition thisCondition, Condition nextCondition, char printChar) {
        this.reentrantLock = reentrantLock;
        this.thisCondition = thisCondition;
        this.nextCondition = nextCondition;
        this.printChar = printChar;
    }

    @Override
    public void run() {
        reentrantLock.lock();
        try {
            for (int i = 0; i < PRINT_COUNT; i++) {
                System.out.println(printChar);
                nextCondition.signal();
                if (i < PRINT_COUNT - 1) {
                    try {
                        thisCondition.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        } finally {
            reentrantLock.unlock();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        ReentrantLock lock = new ReentrantLock();
        Condition conditionA = lock.newCondition();
        Condition conditionB = lock.newCondition();
        Condition conditionC = lock.newCondition();
        Thread printA = new Thread(new SyncPrinter(lock, conditionA, conditionB, 'A'));
        Thread printB = new Thread(new SyncPrinter(lock, conditionB, conditionC, 'B'));
        Thread printC = new Thread(new SyncPrinter(lock, conditionC, conditionA, 'C'));
        printA.start();
        Thread.sleep(100);
        printB.start();
        Thread.sleep(100);
        printC.start();
    }

}
4. 使用信号量Semaphore
public class PrintABCUsingSemaphore {

    /**
     * 实现步骤
     * 定义三个信号量semA、semB、semC。
     * 每个线程在自己的信号量上等待,打印完成后释放下一个线程的信号量。
     */

    private final Semaphore semA = new Semaphore(1);
    private final Semaphore semB = new Semaphore(0);
    private final Semaphore semC = new Semaphore(0);

    @Test
    public void testPrintABC() throws InterruptedException {
        PrintABCUsingSemaphore task = new PrintABCUsingSemaphore();

        Thread threadA = new Thread(task::printA);
        Thread threadB = new Thread(task::printB);
        Thread threadC = new Thread(task::printC);

        threadA.start();
        threadB.start();
        threadC.start();
    }

    public void printA() {
        try {
            for (int i = 0; i < 10; i++) {
                semA.acquire();
                System.out.print("A");
                semB.release();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void printB() {
        try {
            for (int i = 0; i < 10; i++) {
                semB.acquire();
                System.out.print("B");
                semC.release();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    public void printC() {
        try {
            for (int i = 0; i < 10; i++) {
                semC.acquire();
                System.out.print("C");
                semA.release();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

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

相关文章:

  • 前端入门之CSS
  • rknn_convert的使用方法
  • WebRTC源码解析:Android如何渲染画面
  • Redis是什么?架构是怎么样的?
  • MySQL客户端工具-图形化工具-DataGrip 安装与使用
  • 《AI大模型应知应会100篇》加餐篇:LlamaIndex 与 LangChain 的无缝集成
  • 3.9/Q2,Charls最新文章解读
  • 合并两个有序数组(Java实现)
  • Vue2 过滤器 Filters
  • Blender运行python脚本?
  • 42.评论日记
  • 2874. 有序三元组中的最大值 II
  • 全星 研发项目管理APQP 软件:驱动汽车及制造业研发升级的数字化引擎
  • 【笔记】如何使得docker desktop下载至D盘而不是C盘?
  • django REST framework(DRF)教程
  • 一文读懂数据仓库:从概念到技术落地
  • 蓝桥杯Java B组省赛真题题型近6年统计分类
  • 7-5 表格输出
  • 高速电路 PCB 设计要点一
  • 2010-2020年 省级、城市数字经济创新创业-分项指数得分与排名 -社科数据
  • vue3 中引入tinymce富文本
  • 让AI再次伟大-MCP-Server开发指南
  • LeetCode Hot100 刷题笔记(7)—— 贪心
  • WPS JS宏编程教程(从基础到进阶)-- 第三部分:JS宏编程语言开发基础
  • Linux线程概念与控制:【线程概念(页表)】【Linux线程控制】【线程ID及进程地址空间布局】【线程封装】
  • 32f4,串口1,usart.c.h2025
  • EIP-712:类型化结构化数据的哈希与签名
  • 【行测】判断推理:图形推理
  • System.arraycopy()
  • SD 重温学习笔记