/*** Eclipse Class Decompiler plugin, copyright (c) 2016 Chen Chao (cnfree2000@hotmail.com) ***/
package com.alibaba.jstorm.utils; import com.alibaba.jstorm.callback.AsyncLoopRunnable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.atomic.AtomicBoolean; public class TimeCacheMap<K, V> implements TimeOutMap<K, V> {
private static final int DEFAULT_NUM_BUCKETS = 3;
private LinkedList<HashMap<K, V>> _buckets;
private final Object _lock;
private Thread _cleaner;
private ExpiredCallback _callback; public TimeCacheMap(int expirationSecs, int numBuckets, ExpiredCallback<K, V> callback) {
this._lock = new Object(); if (numBuckets < 2) {
throw new IllegalArgumentException("numBuckets must be >= 2");
}
this._buckets = new LinkedList();
for (int i = 0; i < numBuckets; ++i) {
this._buckets.add(new HashMap());
} this._callback = callback;
long expirationMillis = expirationSecs * 1000L;
long sleepTime = expirationMillis / (numBuckets - 1);
this._cleaner = new Thread(new Runnable(sleepTime) {
public void run() {
while (!(AsyncLoopRunnable.getShutdown().get())) {
Map dead = null;
JStormUtils.sleepMs(this.val$sleepTime);
synchronized (TimeCacheMap.this._lock) {
dead = (Map) TimeCacheMap.this._buckets.removeLast();
TimeCacheMap.this._buckets.addFirst(new HashMap());
}
if (TimeCacheMap.this._callback != null)
for (Map.Entry entry : dead.entrySet())
TimeCacheMap.this._callback.expire(entry.getKey(), entry.getValue());
}
}
});
this._cleaner.setDaemon(true);
this._cleaner.start();
} public TimeCacheMap(int expirationSecs, ExpiredCallback<K, V> callback) {
this(expirationSecs, 3, callback);
} public TimeCacheMap(int expirationSecs) {
this(expirationSecs, 3);
} public TimeCacheMap(int expirationSecs, int numBuckets) {
this(expirationSecs, numBuckets, null);
} public boolean containsKey(K key) {
synchronized (this._lock) {
for (HashMap bucket : this._buckets) {
if (bucket.containsKey(key)) {
return true;
}
}
return false;
}
} public V get(K key) {
synchronized (this._lock) {
for (HashMap bucket : this._buckets) {
if (bucket.containsKey(key)) {
return bucket.get(key);
}
}
return null;
}
} public void putHead(K key, V value) {
synchronized (this._lock) {
((HashMap) this._buckets.getFirst()).put(key, value);
}
} public void put(K key, V value) {
synchronized (this._lock) {
Iterator it = this._buckets.iterator();
HashMap bucket = (HashMap) it.next();
bucket.put(key, value);
while (it.hasNext()) {
bucket = (HashMap) it.next();
bucket.remove(key);
}
}
} public Object remove(K key) {
synchronized (this._lock) {
for (HashMap bucket : this._buckets) {
if (bucket.containsKey(key)) {
return bucket.remove(key);
}
}
return null;
}
} public int size() {
synchronized (this._lock) {
int size = 0;
for (HashMap bucket : this._buckets) {
size += bucket.size();
}
return size;
}
} public void cleanup() {
this._cleaner.interrupt();
} public Map<K, V> buildMap() {
Map ret = new HashMap();
synchronized (this._lock) {
for (HashMap bucket : this._buckets) {
ret.putAll(bucket);
}
return ret;
}
}
}

  总体思路,linkList下默认带有3个hashmap,每次新加数据加到第一个hashmap内,同时删除后面map同样key的数据,里面一个线程定时清理过期数据,sleep后,删除list最后一个hashmap,新建一个空的hashmap放到linklist第一个的位置,下一个时间窗口添加数据就添加到该hashmap内,原有的第一个hashmap变为第二个,原有的第二个变为第三个,下次删除就清除最后一个hashmap, 依次循环。

sleep时间 如果默认为30秒参数, 根据代码公式,计算窗口移动时间为15秒, 第一个窗口最后一秒添加数据,30秒后删除,如果是第一个窗口第一秒添加,则需要45秒后删除

long expirationMillis = expirationSecs * 1000L;
long sleepTime = expirationMillis / (numBuckets - 1);

取数据则是遍历list下所有hashmap拿取数据

Jstorm TimeCacheMap源代码分析的更多相关文章

  1. android-plugmgr源代码分析

    android-plugmgr是一个Android插件加载框架,它最大的特点就是对插件不需要进行任何约束.关于这个类库的介绍见作者博客,市面上也有一些插件加载框架,但是感觉没有这个好.在这篇文章中,我 ...

  2. Twitter Storm源代码分析之ZooKeeper中的目录结构

    徐明明博客:Twitter Storm源代码分析之ZooKeeper中的目录结构 我们知道Twitter Storm的所有的状态信息都是保存在Zookeeper里面,nimbus通过在zookeepe ...

  3. 转:SDL2源代码分析

    1:初始化(SDL_Init()) SDL简介 有关SDL的简介在<最简单的视音频播放示例7:SDL2播放RGB/YUV>以及<最简单的视音频播放示例9:SDL2播放PCM>中 ...

  4. 转:RTMPDump源代码分析

    0: 主要函数调用分析 rtmpdump 是一个用来处理 RTMP 流媒体的开源工具包,支持 rtmp://, rtmpt://, rtmpe://, rtmpte://, and rtmps://. ...

  5. 转:ffdshow 源代码分析

    ffdshow神奇的功能:视频播放时显示运动矢量和QP FFDShow可以称得上是全能的解码.编码器.最初FFDShow只是mpeg视频解码器,不过现在他能做到的远不止于此.它能够解码的视频格式已经远 ...

  6. UiAutomator源代码分析之UiAutomatorBridge框架

    上一篇文章<UIAutomator源代码分析之启动和执行>我们描写叙述了uitautomator从命令行执行到载入測试用例执行測试的整个流程.过程中我们也描写叙述了UiAutomatorB ...

  7. MyBatis架构设计及源代码分析系列(一):MyBatis架构

    如果不太熟悉MyBatis使用的请先参见MyBatis官方文档,这对理解其架构设计和源码分析有很大好处. 一.概述 MyBatis并不是一个完整的ORM框架,其官方首页是这么介绍自己 The MyBa ...

  8. hostapd源代码分析(三):管理帧的收发和处理

    hostapd源代码分析(三):管理帧的收发和处理 原文链接:http://blog.csdn.net/qq_21949217/article/details/46004379 这篇文章我来讲解一下h ...

  9. hostapd源代码分析(二):hostapd的工作机制

    [转]hostapd源代码分析(二):hostapd的工作机制 原文链接:http://blog.csdn.net/qq_21949217/article/details/46004433 在我的上一 ...

随机推荐

  1. Apache Commons Email 使用网易企业邮箱发送邮件

    最近使用HtmlEmail 发送邮件,使用网易企业邮箱,发送邮件,死活发不出去!原以为是网易企业邮箱,不支持发送邮箱,后面经过研究发现,是apache htmlEmail 的协议导致,apache E ...

  2. Light OJ 1422 - Halloween Costumes(区间DP 最少穿几件)

    http://www.cnblogs.com/kuangbin/archive/2013/04/29/3051392.html http://www.cnblogs.com/ziyi--caolu/a ...

  3. c# 获取文件夹大小

    private long GetDirectorySizeMethod1(string directory) { long directorySize = 0; DirectoryInfo di = ...

  4. 学习CSS制作菜单列表,举一反三

    1.普通的二三级菜单 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type&qu ...

  5. 日常捕获的野生知识 - javascript获取屏幕大小

    刚刚接触JavaScript,涉及到 document , window 的一些基本知识不是很了解,今天为了一个屏幕大小折腾了半天,幸好找到了很好的例子学习. 代码如下: <html> & ...

  6. 注册表----修改Win7登录界面

    在进行操作前,需要准备好背景图片.对背景图片的要求有三点: (1)图片必须是JPG格式: (2)必须将图片命名为backgroundDefault; (3)图片的体积必须小于256KB. 按下[Win ...

  7. 139.00.006 Git学习-标签管理Tag

    @(139 - Environment Settings | 环境配置) 一.Why 发布一个版本时,我们通常先在版本库中打一个标签(tag),这样,就唯一确定了打标签时刻的版本.将来无论什么时候,取 ...

  8. 目前比较全的CSS重设(reset)方法总结(转)

    原文地址 在当今网页设计/开发实践中,使用CSS来为语义化的(X)HTML标记添加样式风格是重要的关键.在设计师们的梦想中都存在着这样的一个完美世界:所有的浏览器都能够理解和适用多有CSS规则,并且呈 ...

  9. Javascript之DOM的三大节点及部分用法

    DOM有三种节点:元素节点.属性节点.文本节点. 一.用nodeType可以检测节点的类型 节点类型 nodeType属性值 元素节点 1 属性节点 2 文本节点 3 这样方便在js中对各个节点进行操 ...

  10. androidwebview timeout

    public class MyWebViewClient extends WebViewClient { boolean timeout; public MyWebView() { timeout = ...