标签:ati div turn ++ create single count 解决 public
public abstract class EnableCleanSingle<T>{ private volatile T object; private int updateCount; public T getObject() { if(object == null) { synchronized (this){ if(object == null) { createObject(); updateCount++; } } }
//这里在并发的时候,可能会返回null
//解决方法:将整个方法都设为synchronized,但影响性能,这里就不改了
return object;
}
public void cleanObject(){
int old = updateCount;
if(object != null) {
synchronized (this) {
if (updateCount == old) {
object = null;
}
}
}
}
abstract public T createObject();
}
标签:ati div turn ++ create single count 解决 public
原文地址:https://www.cnblogs.com/math-and-it/p/11404054.html