1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.yanzuoguang.util.cache;
import com.yanzuoguang.util.extend.ConfigBase;
import com.yanzuoguang.util.thread.ThreadHelper;
import com.yanzuoguang.util.thread.AbstractThreadList;
import java.util.List;
import java.util.Vector;
/**
* 内存缓存中心,负责自动清除过期缓存
*
* @author 颜佐光
*/
public class MemoryCacheCenter {
/**
* 缓存的对象
*/
public final static List<MemoryCache> CLEAR_LIST = new Vector<MemoryCache>();
static {
init();
}
/**
* 初始化
*/
private static void init() {
// 缓存对象处理线程
ThreadHelper.runThread(new Runnable() {
@Override
public void run() {
do {
clear();
// 等待1秒
ThreadHelper.sleep(ConfigBase.MEMORY_CLEAR_TIMEOUT);
} while (true);
}
});
}
/**
* 间隔5秒执行
*/
private static void clear() {
// todo: 需要修改
// 死循环处理
AbstractThreadList<MemoryCache> threadList = new AbstractThreadList<MemoryCache>(1) {
@Override
public void run(MemoryCache item) {
item.clearTimeout();
}
};
threadList.add(CLEAR_LIST);
threadList.waitRun(false);
}
}