hazelcast 提供了3中方法调用startCleanup:
第一种是在ConcuurentMapManager的构造函数中,通过调用node的executorManager中的ScheduledExecutorService来创建每秒执行一次cleanup操作的线程(代码如下)。由于这是ConcuurentMapManager构造函数的代码,所以这种调用startCleanup的操作是默认就会有的。
第一种是在ConcuurentMapManager的构造函数中,通过调用node的executorManager中的ScheduledExecutorService来创建每秒执行一次cleanup操作的线程(代码如下)。由于这是ConcuurentMapManager构造函数的代码,所以这种调用startCleanup的操作是默认就会有的。
node.executorManager.getScheduledExecutorService().scheduleAtFixedRate(new Runnable() {
publicvoid run() {
for (CMap cMap : maps.values()) {
cMap.startCleanup(false);
}
}
}, 1, 1, TimeUnit.SECONDS);
第二种是通过配置文件来触发startCleanup的执行,配置 PutOperationhandlerif overcapacity policy,我们系统的配置文件没有配置这方面的policy,所有这种方式在我们系统中没有使用。
第三种是自己直接写代码去调用startCleanup函数(public方法,线程安全的). 这个没有实现在我们的系统中。
所以我的调查方向放在了第一种调用的情况,hazelcast里面的ScheduledExecutorService是通过java.util.ScheduledThreadPoolExecutor 来实现的.
esScheduled = new ScheduledThreadPoolExecutor(5, new ExecutorThreadFactory(node.threadGroup,
node.getThreadPoolNamePrefix("scheduled"), classLoader), new RejectionHandler()) {
protected void beforeExecute(Thread t, Runnable r) {
threadPoolBeforeExecute(t, r);
}
}
查看ScheduledThreadPoolExecutor的实现,它把线程实现分成了3个部分: runnable tasks可执行任务, workers to execute the tasks执行任务的具体线程 以及 ScheduledThreadPoolExecutor 调度workers按照要求执行runnable tasks。我们通过scheduleAtFixdRate提交了task,scheduleAtFixedRate先把它打包成重复执行的ScheduleFutureTask
<pre name="code" class="java"> public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
long initialDelay,
long period,
TimeUnit unit) {
if (command == null || unit == null)
throw new NullPointerException();
if (period <= 0)
thr