目录

  • 方法一:使用synchronized和wait/notify
  • 方法二:使用`CompletableFuture`实现


方法一:使用synchronized和wait/notify

package com.demo;

import java.util.concurrent.CompletableFuture;

public class PrintABC {

    // 当前状态
    private static String state = "A";

    // 共享锁对象
    private static final Object lock = new Object();

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            synchronized (lock){
                System.out.println("current: A; state: " + state);
                // 等待
                while (!"A".equals(state)){
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                System.out.println("A: " + Thread.currentThread());

                // 修改B状态
                state = "B";

                // 唤醒下一个
                lock.notifyAll();
            }
        });

        Thread t2 = new Thread(() -> {
            synchronized (lock){
                System.out.println("current: B; state: " + state);

                // 等待
                while (!"B".equals(state)){
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                System.out.println("B: " + Thread.currentThread());

                // 修改B状态
                state = "C";

                // 唤醒下一个
                lock.notifyAll();
            }
        });

        Thread t3 = new Thread(() -> {
            synchronized (lock){
                System.out.println("current: C; state: " + state);

                // 等待
                while (!"C".equals(state)){
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                System.out.println("C: " + Thread.currentThread());

                // 修改B状态
                state = "";

                // 唤醒下一个
                lock.notifyAll();
            }
        });

        t1.start();
        t2.start();
        t3.start();
    }
}

输出

current: A; state: A
A: Thread[Thread-0,5,main]
current: C; state: B
current: B; state: B
B: Thread[Thread-1,5,main]
C: Thread[Thread-2,5,main]

方法二:使用CompletableFuture实现

package com.demo;

import java.util.concurrent.CompletableFuture;

public class PrintABC {
    public static void main(String[] args) {
        CompletableFuture<Void> t1 = CompletableFuture.runAsync(() -> {
            System.out.println("A: " + Thread.currentThread());
        });

        CompletableFuture<Void> t2 = t1.thenRunAsync(() -> {
            System.out.println("B: " + Thread.currentThread());
        });

        t2.thenRunAsync(()->{
            System.out.println("C: " + Thread.currentThread());
        });
    }
}

输出

A: Thread[ForkJoinPool.commonPool-worker-1,5,main]
B: Thread[ForkJoinPool.commonPool-worker-1,5,main]
C: Thread[ForkJoinPool.commonPool-worker-2,5,main]

参考

华为OD面试:三个线程交替打印ABC如何实现?面试题:三个线程如何交替打印ABC100次