完整注释,将循环等待时间减少至30S;

This commit is contained in:
magese 2018-08-23 09:41:16 +08:00
parent c026752cbe
commit 74962a171e

View File

@ -1,42 +1,59 @@
/* /*
* IK 中文分词 版本 7.0 * IK 中文分词 版本 7.4
* IK Analyzer release 7.0 * IK Analyzer release 7.4
* update by 高志成(magese@live.cn) * update by Magese(magese@live.cn)
*/ */
package org.wltea.analyzer.lucene; package org.wltea.analyzer.lucene;
import java.io.IOException; import java.io.IOException;
import java.util.Vector; import java.util.Vector;
/**
* 更新扩展词典子线程类
*/
public class UpdateKeeper implements Runnable { public class UpdateKeeper implements Runnable {
private static final long INTERVAL = 60000L; private static final long INTERVAL = 30000L; // 循环等待时间
private static UpdateKeeper singleton; private Vector<UpdateJob> filterFactorys; // 更新任务集合
private Vector<UpdateJob> filterFactorys;
/**
* 私有化构造器阻止外部进行实例化
*/
private UpdateKeeper() { private UpdateKeeper() {
this.filterFactorys = new Vector<>(); this.filterFactorys = new Vector<>();
Thread worker = new Thread(this); Thread worker = new Thread(this);
worker.setDaemon(true); worker.setDaemon(true);
worker.start(); worker.start();
} }
static UpdateKeeper getInstance() { /**
if (singleton == null) { * 静态内部类实现线程安全单例模式
synchronized (UpdateKeeper.class) { */
if (singleton == null) { private static class Builder {
singleton = new UpdateKeeper(); private static UpdateKeeper singleton = new UpdateKeeper();
return singleton;
}
}
}
return singleton;
} }
/**
* 获取本类的实例
* 线程安全单例模式
*
* @return 本类的实例
*/
static UpdateKeeper getInstance() {
return UpdateKeeper.Builder.singleton;
}
/**
* 将运行中的IK分词工厂实例注册到本类定时任务中
*
* @param filterFactory 运行中的IK分词器
*/
void register(UpdateJob filterFactory) { void register(UpdateJob filterFactory) {
this.filterFactorys.add(filterFactory); this.filterFactorys.add(filterFactory);
} }
/**
* 子线程执行任务
*/
@Override @Override
public void run() { public void run() {
//noinspection InfiniteLoopStatement //noinspection InfiniteLoopStatement
@ -47,7 +64,9 @@ public class UpdateKeeper implements Runnable {
e.printStackTrace(); e.printStackTrace();
} }
// 如果更新字典任务集合不为空
if (!this.filterFactorys.isEmpty()) { if (!this.filterFactorys.isEmpty()) {
// 进行循环并执行更新
for (UpdateJob factory : this.filterFactorys) { for (UpdateJob factory : this.filterFactorys) {
try { try {
factory.update(); factory.update();