Code below是板缺例如,从"Programming Concurrency on the JVM: Mastering Synchronization, STM, and Actors" book同步
我不明白为什么笔者同步stopEnergySource方法,刚刚取消ScheduledFuture任务,这代表由replenishTask变量?没有其他方法使用这个变量。 是否需要实践来同步Future.cancel调用或它只需要ScheduledFuture?
public class EnergySource {
//...
private static final ScheduledExecutorService replenishTimer =
Executors.newScheduledThreadPool(10);
private ScheduledFuture<?> replenishTask;
private EnergySource() {}
private void init() {
replenishTask = replenishTimer.scheduleAtFixedRate(new Runnable() {
public void run() { replenish(); }
}, 0, 1, TimeUnit.SECONDS);
}
public static EnergySource create() {
final EnergySource energySource = new EnergySource();
energySource.init();
return energySource;
}
public long getUnitsAvailable() {
//...
}
public long getUsageCount() {
//...
}
public boolean useEnergy(final long units) {
//...
}
public synchronized void stopEnergySource() { // what for **synchronized** is?
replenishTask.cancel(false);
}
private void replenish() {
//...
}
}
这没什么意义...... – assylias
'ScheduledFuture'不需要'synchronized'关键字。更糟糕的是,如果这是该类中唯一的“同步”代码,那么这种同步没有任何效果,这使得它更具误导性。 – Holger
查看完整的代码后,我可以告诉你,'replenish()'方法内AtomicLong的用法也被破坏了(“check-then-act”反模式)。 – Holger