参考博客:经典线程面试题-两个线程交替打印_xiaoxiaoyunlu的博客-CSDN博客_两个线程交替打印
public static int i = 1;
public volatile static int j = 1;
public volatile static boolean flag = false;
public static int count = 0;
public static Lock lock = new ReentrantLock();
public static Condition conditionA = lock.newCondition();
public static Condition conditionB = lock.newCondition();
private static CountDownLatch latch = new CountDownLatch(2);
private static AtomicInteger numA = new AtomicInteger();
private static AtomicInteger numB = new AtomicInteger();
private static byte[] block = new byte[0];
(1)使用 synchronized 和 wait notifyAll 来实现
public static void test6() {
new Thread(() -> {
while (i < 10) {
synchronized (block) {
if (flag) {
try {
b