public abstract class ObjectCache<T> {
protected volatile T object;
protected int expireAt = 0;
private final ReentrantLock mutex = new ReentrantLock(true);
public abstract T create();
protected abstract int getExpireSeconds();
public T get()
{
if(mutex.isLocked() && this.object != null)
{
return this.object;
}
mutex.lock();
try
{
int unixTimestamp = (int) (System.currentTimeMillis() / 1000L);
if(expireAt < unixTimestamp)
{
this.expireAt = (int) unixTimestamp + getExpireSeconds();
this.object = create();
}
}
finally
{
mutex.unlock();
}
return this.object;
}
}
Does this class cause multithreading problems?