synchronized (lockObject) { while (energy > energyBoxes[from]) { try { lockObject.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(Thread.currentThread().getName()); energyBoxes[from] -= energy; System.out.printf("从%d转移了%10.2f单位能量到%d%n", from, energy, to); energyBoxes[to] += energy; System.out.printf("能量总和为%10.2f%n", getTotalEnergies()); lockObject.notifyAll(); }