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