1 package test; public class AThread extends Thread { String name; public AThread(String name) { this.name=name; } static Object printer=new Object(); public void run() { synchronized(printer) { int i=1; while(i<=5) { System.out.println(name+"执行步骤"+i); i++; } } } public static void main(String[] args) { AThread thread1=new AThread("线程1"); AThread thread2=new AThread("线程2"); thread1.start(); thread2.start(); } } 运行结果: 线程1执行步骤1 线程1执行步骤2 线程1执行步骤3 线程1执行步骤4 线程1执行步骤5 线程2执行步骤1 线程2执行步骤2 线程2执行步骤3 线程2执行步骤4 线程2执行步骤5 2去掉 static Object printer=new Object(); 和synchronized(printer){}即可 运行结果: 线程1执行步骤1 线程2执行步骤1 线程1执行步骤2 线程2执行步骤2 线程1执行步骤3 线程2执行步骤3 线程1执行步骤4 线程2执行步骤4 线程1执行步骤5 线程2执行步骤5