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

2022-08-21 20:20:34 +08:00
 wayn111

如题:使用三个线程交替打印 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 次的实现方法介绍完毕

1091 次点击
所在节点    程序员
4 条回复
ufan0
2022-08-22 11:15:25 +08:00
github 主页个人站点未正常工作。
Vaspike
2022-08-22 14:40:42 +08:00
提个小建议,方法内的注释最好是单行注释,虽然无伤大雅
wayn111
2022-08-22 20:44:29 +08:00
@Vaspike 下次注意
wayn111
2022-08-22 20:46:08 +08:00
@ufan0 我看了正常呀

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/874385

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX