package org.example; public class Main { // 计数器 private static int count 0; public static void main(String[] args) throws InterruptedException { // 线程 1 Thread thread1 new Thread(() - { while (count 100) { // 尝试获取锁 synchronized (Main.class) { // Double Check if (count 100 count % 2 0) { System.out.println(线程 1 打印 (count 1)); count; } } } }); // 线程 2 Thread thread2 new Thread(() - { while (count 100) { // 尝试获取锁 synchronized (Main.class) { // Double Check if (count 100 count % 2 ! 0) { System.out.println(线程 2 打印 (count 1)); count; } } } }); // 启动线程 thread1.start(); thread2.start(); // 等待线程打印输出完成 Thread.sleep(10000); } }