我创建了一个用作缓存提供者的类。它使用一个Map,时间戳映射条目,它产生一个Thread,每隔一段时间执行一次清理。该类用于Web应用程序。这个Web应用程序有一个问题,POST需要30秒。我将问题追溯到这个缓存类,消除了它解决了问题。这段代码中的错误在哪里?
我已尽全力在本课中找到错误,但我不能。请帮我在这里。 假设用户类是某种描述用户的POJO。
public class UserStore implements Thread.UncaughtExceptionHandler {
private static volatile UserStore instance;
private static Thread cleanUpThread;
private static Map<String, TimeStampedToken<User>> tokenMap = new HashMap<String, TimeStampedToken<User>>();
public static UserStore getInstance() {
if (instance == null) {
synchronized(UserStore.class) {
if (instance == null) {
instance = new UserStore();
cleanUpThread = new Thread(new CleanUpWorker());
cleanUpThread.setUncaughtExceptionHandler(instance);
cleanUpThread.start();
}
}
}
return instance;
}
public void uncaughtException(Thread thread, Throwable throwable) {
if (throwable instanceof ThreadDeath) {
cleanUpThread = new Thread(new CleanUpWorker());
cleanUpThread.setUncaughtExceptionHandler(this);
cleanUpThread.start();
throw (ThreadDeath)throwable;
}
}
private static class CleanUpWorker implements Runnable {
private static final long CLEANUP_CYCLE_MS = 300000;
private static final long OBJECT_LIVE_TIME = 299900;
public void run() {
long sleepRemaining;
long sleepStart = System.currentTimeMillis();
sleepRemaining = CLEANUP_CYCLE_MS;
while (true) {
try {
sleepStart = System.currentTimeMillis();
Thread.sleep(sleepRemaining);
cleanUp();
sleepRemaining = CLEANUP_CYCLE_MS;
} catch (InterruptedException e) {
sleepRemaining = System.currentTimeMillis() - sleepStart;
}
}
}
private void cleanUp() {
Long currentTime = System.currentTimeMillis();
synchronized(tokenMap) {
for (String user : tokenMap.keySet()) {
TimeStampedToken<User> tok = tokenMap.get(user);
if (tok.accessed + OBJECT_LIVE_TIME < currentTime) {
tokenMap.remove(user);
}
}
}
}
}
public void addToken(User tok) {
synchronized(tokenMap) {
tokenMap.put(tok.getUserId(), new TimeStampedToken<User>(tok));
}
}
public User getToken(String userId) {
synchronized(tokenMap) {
TimeStampedToken<User> user = tokenMap.get(userId);
if (user != null) {
user.accessed = System.currentTimeMillis();
return user.payload;
} else {
return null;
}
}
}
private static class TimeStampedToken<E> {
public TimeStampedToken(E payload) {
this.payload = payload;
}
public long accessed = System.currentTimeMillis();
public E payload;
}
}
究竟是什么*问题,你的缓存类是做什么的?它工作不正确(功能上)?或者它比你想要的慢(导致POST花费太长时间)?如果是这样,哪种方法调用花费太多时间? – ArjunShankar 2011-12-21 11:19:39
为什么只有在明确告知停止时才重新启动线程? – 2011-12-21 11:19:53
我看起来像你试图实现一个有效期限的缓存。有很多简单的方法来实现这一点,或者你可以使用已经这样做的库。 – 2011-12-21 11:21:15