如题:使用三个线程交替打印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执行

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

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