V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
wayn111
V2EX  ›  程序员

三个线程交替打印 ABC100 次问题思考

  •  
  •   wayn111 ·
    wayn111 · 2022-08-21 20:20:34 +08:00 · 1084 次点击
    这是一个创建于 606 天前的主题,其中的信息可能已经有所发展或是发生改变。

    如题:使用三个线程交替打印 ABC ,直至 100 次代码实战

    方法一:

    使用notify()、wait()方法

    public class PrintAbc {
    
        /**
         * 唤醒线程的状态值 state: threadA = 0, threadB = 1, threadC =2,
         */
        int state = 0;
    
        /**
         * 循环技术,初始值 0
         */
        int count = 0;
    
        public void print(PrintAbc printAbc) {
            Thread threadA = new Thread(() -> {
                extracted(printAbc, "A", 0, 1);
            });
    
            Thread threadB = new Thread(() -> {
                extracted(printAbc, "B", 1, 2);
            });
    
            Thread threadC = new Thread(() -> {
                extracted(printAbc, "C", 2, 0);
            });
    
            threadC.start();
            threadB.start();
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            threadA.start();
        }
    
        /**
         * 交替打印 abc ,直至 100 次
         *
         * @param printAbc  锁对象
         * @param a         打印的字母, 对应 A 、B 、C
         * @param needState 当前线程对应的 state 状态值
         * @param nextState 唤醒下一个线程所需 state 状态值
         */
        private void extracted(PrintAbc printAbc, String a, int needState, int nextState) {
            while (true) {
                synchronized (printAbc) {
                    if (count >= 100) {
                        break;
                    }
                    if (printAbc.count < 100 && printAbc.state == needState) {
                        System.out.println(a);
                        printAbc.state = nextState;
                        printAbc.count++;
                        printAbc.notifyAll();
                    } else {
                        try {
                            printAbc.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
        }
    
        public static void main(String[] args) {
            PrintAbc printAbc = new PrintAbc();
            printAbc.print(printAbc);
        }
    }
    

    上诉代码使用notify(),wait(),进行线程间的条件唤醒,state的初始状态是 0 ,对应线程 A ,所以第一次打印字母也一定是 A

    方法二

    使用ReentrantLock 的Condition条件

    public class PrintAbcByCondition {
    
        /**
         * 循环计数初始值 0
         */
        static int count = 0;
    
        public void print() {
            ReentrantLock reentrantLock = new ReentrantLock();
            Condition conditionA = reentrantLock.newCondition();
            Condition conditionB = reentrantLock.newCondition();
            Condition conditionC = reentrantLock.newCondition();
            Thread threadA = new Thread(() -> {
                while (true) {
                    try {
                        reentrantLock.lock();
                        // threadA 进来打印 A 然后唤醒 threadB
                        if (count < 100) {
                            System.out.println("A");
                            count++;
                            conditionB.signal();
                        }
                        conditionA.await();
    
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    } finally {
                        reentrantLock.unlock();
                    }
                }
            });
            Thread threadB = new Thread(() -> {
                while (true) {
                    try {
                        reentrantLock.lock();
                        // threadB 进来就阻塞等待 threadA 使用完毕
                        conditionB.await();
                        if (count < 100) {
                            System.out.println("B");
                            count++;
                            conditionC.signal();
                        }
    
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    } finally {
                        reentrantLock.unlock();
                    }
                }
            });
            Thread threadC = new Thread(() -> {
                while (true) {
                    try {
                        reentrantLock.lock();
                        // threadC 进来就阻塞等待 threadB 使用完毕
                        conditionC.await();
                        if (count < 100) {
                            System.out.println("C");
                            count++;
                            conditionA.signal();
                        }
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    } finally {
                        reentrantLock.unlock();
                    }
                }
            });
    
            threadC.start();
            threadB.start();
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            threadA.start();
        }
    
        public static void main(String[] args) {
            new PrintAbcByCondition().print();
        }
    }
    

    使用ReentrantLock 的Condition条件,很容易能实现三个线程之间的交替打印,需要注意的一点就是线程 A 是需要第一个执行,可以看到代码里threadA在等待 1 秒后在执行,也能确保是第一个进行打印,原因如下:

    线程 B 和线程 C 中任意一个线程拿到锁都需要等待条件成立,线程 C 依赖线程 B ,而线程 B 依赖线程 A ,所以他们会一直阻塞直至线程 A 执行

    方法三:使用SemaphoretryAcquire()方法实现线程间的加锁操作

    public class PrintAbcBySemaphore {
    
        /**
         * 循环计数初始值 0
         */
        static int count = 0;
        // 定义三个线程对应的信号量 A 、B 、C ,默认给 A 线程一个许可证
        private Semaphore semaphoreA = new Semaphore(1);
        private Semaphore semaphoreB = new Semaphore(0);
        private Semaphore semaphoreC = new Semaphore(0);
    
        public void print() {
            Thread threadA = new Thread(() -> {
                extracted(semaphoreA, "A", semaphoreB);
            });
            Thread threadB = new Thread(() -> {
                extracted(semaphoreB, "B", semaphoreC);
            });
    
    
            Thread threadC = new Thread(() -> {
                extracted(semaphoreC, "C", semaphoreA);
            });
    
            threadB.start();
            threadC.start();
            try {
                Thread.sleep(2000L);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
    
            threadA.start();
    
        }
    
        private void extracted(Semaphore needSemaphore, String A, Semaphore nextSemaphore) {
            while (true) {
                // 当前线程的信号量加锁
                if (needSemaphore.tryAcquire(1)) {
                    if (count < 100) {
                        System.out.println(A);
                        count++;
                        // 给下一个待执行线程的信号量加一个许可证
                        nextSemaphore.release();
                    } else {
                        // 执行 100 次后,退出线程
                        nextSemaphore.release();
                        break;
                    }
                }
            }
        }
    
        public static void main(String[] args) {
            new PrintAbcBySemaphore().print();
        }
    }
    

    使用SemaphoretryAcquire()方法实现线程间的加锁操作,通过release()方法给下一个待执行线程的信号量添加一个许可证,由此保证三个线程顺序执行,初始化 A 线程信号量时添加一个默认许可证,让 A 线程第一个执行

    上诉三个方法中,核心问题就是如何实现线程间的条件唤醒,如方法一,我们可以自定义state状态变量来与各个线程绑定,每个线程都有自己对应的state状态,当state变量当前值与线程自身期望的state值相同才唤醒当前线程。也可以使用jucReentrantLock 的提供的Condition条件完成线程间的条件唤醒,最后我们使用Semaphore信号量可以优雅的实现了线程间的同步与唤醒

    至此,三个线程交替打印 ABC100 次的实现方法介绍完毕

    ufan0
        1
    ufan0  
       2022-08-22 11:15:25 +08:00
    github 主页个人站点未正常工作。
    Vaspike
        2
    Vaspike  
       2022-08-22 14:40:42 +08:00
    提个小建议,方法内的注释最好是单行注释,虽然无伤大雅
    wayn111
        3
    wayn111  
    OP
       2022-08-22 20:44:29 +08:00 via Android
    @Vaspike 下次注意
    wayn111
        4
    wayn111  
    OP
       2022-08-22 20:46:08 +08:00 via Android
    @ufan0 我看了正常呀
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1220 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 18:07 · PVG 02:07 · LAX 11:07 · JFK 14:07
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.