107 lines
3.2 KiB
Java
Raw Normal View History

2018-05-10 13:52:58 +08:00
/*
2019-07-11 13:51:05 +08:00
* IK 中文分词 版本 8.1.1
* IK Analyzer release 8.1.1
2018-11-15 11:05:24 +08:00
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* 源代码由林良益(linliangyi2005@gmail.com)提供
* 版权声明 2012乌龙茶工作室
* provided by Linliangyi and copyright 2012 by Oolong studio
*
2019-07-11 13:51:05 +08:00
* 8.1.1版本 Magese (magese@live.cn) 更新
* release 8.1.1 update by Magese(magese@live.cn)
2018-11-15 11:05:24 +08:00
*
2018-05-10 13:52:58 +08:00
*/
package org.wltea.analyzer.lucene;
import java.io.IOException;
import java.util.Vector;
/**
* 更新扩展词典子线程类
*/
2018-09-03 13:54:01 +08:00
public class UpdateThread implements Runnable {
private static final long INTERVAL = 30000L; // 循环等待时间
private Vector<UpdateJob> filterFactorys; // 更新任务集合
2018-05-10 13:52:58 +08:00
/**
* 私有化构造器阻止外部进行实例化
*/
2018-09-03 13:54:01 +08:00
private UpdateThread() {
2018-05-10 13:52:58 +08:00
this.filterFactorys = new Vector<>();
Thread worker = new Thread(this);
worker.setDaemon(true);
worker.start();
}
/**
* 静态内部类实现线程安全单例模式
*/
private static class Builder {
2018-09-03 13:54:01 +08:00
private static UpdateThread singleton = new UpdateThread();
}
/**
* 获取本类的实例
* 线程安全单例模式
*
* @return 本类的实例
*/
2018-09-03 13:54:01 +08:00
static UpdateThread getInstance() {
return UpdateThread.Builder.singleton;
2018-05-10 13:52:58 +08:00
}
/**
* 将运行中的IK分词工厂实例注册到本类定时任务中
*
* @param filterFactory 运行中的IK分词器
*/
2018-05-10 13:52:58 +08:00
void register(UpdateJob filterFactory) {
this.filterFactorys.add(filterFactory);
}
/**
* 子线程执行任务
*/
2018-05-10 13:52:58 +08:00
@Override
public void run() {
//noinspection InfiniteLoopStatement
while (true) {
try {
Thread.sleep(INTERVAL);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 如果更新字典任务集合不为空
2018-05-10 13:52:58 +08:00
if (!this.filterFactorys.isEmpty()) {
// 进行循环并执行更新
2018-05-10 13:52:58 +08:00
for (UpdateJob factory : this.filterFactorys) {
try {
factory.update();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
public interface UpdateJob {
void update() throws IOException;
}
}