public class ABCPrint extends Thread { private final String str[] = {"A", "B", "C"}; private final static AtomicInteger atomCount = new AtomicInteger(); public ABCPrint(String name) { this.setName(name); } @Override public void run() { while (true) { // 循环满2轮退出打印 if (atomCount.get() / 3 == 2) { break; } synchronized (atomCount) { // 顺序打印A、B、C if (str[atomCount.get() % 3].equals(getName())) { atomCount.getAndIncrement();//自增 System.out.println(getName()); //表示一轮打印结束 方便观察打印下分隔符 if ("C".equals(getName())) { System.out.println("================================"); } // 当前线程打印打印完成后唤醒其它线程 atomCount.notifyAll(); } else { // 非顺序线程wait() try { atomCount.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } }} public static void main(String[] args) { new ABCPrint("A").start(); new ABCPrint("B").start(); new ABCPrint("C").start(); }