try {
//条件不满足, 将当前线程放入Wait Set
lockObj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.print(Thread.currentThread().getName());
energyBoxes[from] -= amount;
System.out.printf("从%d转移%10.2f单位能量到%d", from, amount, to);
energyBoxes[to] += amount;
System.out.printf(" 能量总和:%10.2f%n", getTotalEnergies());
//唤醒所有在lockObj对象上等待的线程
lockObj.notifyAll();条件不满足时也就是其它线程还没有将数据写回(这里的lockObj是EnergySystem的final成员,用来标记EnergySystem,lockObj.wait()也就相当于EnergySystem实例.wait()),等待写入之后(也就是满足条件),执行逻辑代码,在唤醒其它线程(有可能还没有写入数据的)。可以回头看争用条件那节,有助于理解,我专门截了图。