redisson - 为什么 reddison ExpirationEntry 使用 LinkedHashMap 来保存 Thread?
问题描述
Reddison watchDog 策略使用 timerTask 增加 key 过期时间,同时设置锁成功
private void renewExpiration() {
ExpirationEntry ee = EXPIRATION_RENEWAL_MAP.get(getEntryName());
if (ee == null) {
return;
}
Timeout task = commandExecutor.getConnectionManager().newTimeout(new TimerTask() {
@Override
public void run(Timeout timeout) throws Exception {
ExpirationEntry ent = EXPIRATION_RENEWAL_MAP.get(getEntryName());
if (ent == null) {
return;
}
Long threadId = ent.getFirstThreadId();
if (threadId == null) {
return;
}
RFuture<Boolean> future = renewExpirationAsync(threadId);
......
}
}
首先使用entryName获取 ExpirationEntry 对象。同一个entryName,其他线程取不到key,因为locked(ps:同一个线程可以get和counter++),那么为什么只有一个线程的时候用LinkedHashMap来保存线程呢?
公共静态类 ExpirationEntry {
private final Map<Long, Integer> threadIds = new LinkedHashMap<>();
private volatile Timeout timeout;
public ExpirationEntry() {
super();
}
public synchronized void addThreadId(long threadId) {
Integer counter = threadIds.get(threadId);
if (counter == null) {
counter = 1;
} else {
counter++;
}
threadIds.put(threadId, counter);
}
}
也许就像这样:
public static class ExpirationEntry {
private volatile Timeout timeout;
private Long threadId;
private int counter;
public synchronized void addThread(Long threadId) {
if (this.threadId.equals(threadId)) {
counter ++;
}
}
}
我将不胜感激,谢谢
解决方案
推荐阅读
- javascript - 如何使用 async/await 一一进行异步调用?
- python - 如何优化 for 循环以使用 2-D 数组中的值填充 3-D np 数组?
- rust - 展平大数组常量会减少 L1 缓存未命中的数量吗?
- tailwind-css - 你能把 Tailwind 和 vanilla CSS 混合起来吗?
- regex - LEX中匹配不以换行符开头的字符串的正则表达式是什么
- codeigniter-3 - 使用 codeignitor 将图像上传到重定向的文件夹
- powershell - Powershell合并来自不同文件的2列,没有关键字段
- r - 绘制具有已知误差的时间序列 (ggplot2)
- java - 响应从 servlet 重定向到 jsp 后,jsp 页面中的关闭按钮不起作用
- python - 从 pandas 日期时间列中提取唯一的月度周期