企业搜索引擎开发之连接器connector(二十二)
下面来分析线程执行类,线程池ThreadPool类
对该类的理解需要对java的线程池比较熟悉
该类引用了一个内部类
/**
* The lazily constructed LazyThreadPool instance.
*/
private LazyThreadPool lazyThreadPool;
该成员实现了单例模式,即该对象只有一个实例,属于懒汉式单例模式,当实例化该成员时,启用了线程同步机制
/**
* Shut down the {@link ThreadPool}. After this returns
* {@link ThreadPool#submit(TimedCancelable)} will return null.
*
* @param interrupt {@code true} if the threads executing tasks task should
* be interrupted; otherwise, in-progress tasks are allowed to complete
* normally.
* @param waitMillis maximum amount of time to wait for tasks to complete.
* @return {@code true} if all the running tasks terminated and
* {@code false} if the some running task did not terminate.
* @throws InterruptedException if interrupted while waiting.
*/
synchronized boolean shutdown(boolean interrupt, long waitMillis)
throws InterruptedException {
isShutdown = true;
if (lazyThreadPool == null) {
return true;
} else {
return lazyThreadPool.shutdown(interrupt, waitMillis);
}
} /**
* Return a LazyThreadPool.
*/
private synchronized LazyThreadPool getInstance() {
if (lazyThreadPool == null) {
lazyThreadPool = new LazyThreadPool();
}
return lazyThreadPool;
}
线程提交方法如下
/**
* Submit a {@link Cancelable} for execution and return a
* {@link TaskHandle} for the running task or null if the task has not been
* accepted. After {@link ThreadPool#shutdown(boolean, long)} returns this
* will always return null.
*/
public TaskHandle submit(Cancelable cancelable) {
if (isShutdown) {
return null;
}
if (cancelable instanceof TimedCancelable && maximumTaskLifeMillis != 0L) {
return getInstance().submit((TimedCancelable) cancelable);
} else {
return getInstance().submit(cancelable);
}
}
这里针对Cancelable对象类型和TimedCancelable类型提交到了不同的方法(多态)
提交Cancelable类型对象比较简单,提交任务后获取操作句柄
/**
* 提交任务2 获得操作句柄
* Submit a {@link Cancelable} for execution and return a
* {@link TaskHandle} for the running task or null if the task has not been
* accepted. After {@link LazyThreadPool#shutdown(boolean, long)} returns
* this will always return null.
*/
TaskHandle submit(Cancelable cancelable) {
try {
// taskFuture is used to cancel 'cancelable' and to determine if
// 'cancelable' is done.
Future<?> taskFuture = completionService.submit(cancelable, null);
return new TaskHandle(cancelable, taskFuture, clock.getTimeMillis());
} catch (RejectedExecutionException re) {
if (!executor.isShutdown()) {
LOGGER.log(Level.SEVERE, "Unable to execute task", re);
}
return null;
}
}
而提交TimedCancelable类型对象则相对比较复杂
基本思路是,首先启动一个延迟执行的线程,即在指定的时间延迟后执行TimedCancelable类型对象的timeout()方法,即取消另外一个线程的执行,即超时检测线程;
然后启动另外一个线程执行TimedCancelable类型对象的run()方法,同时执行完毕后,则同时取消上面的超时检测线程(如果指定时间内未执行完毕,则由超时检测线程来取消执行)。
这里我们可以类比于守护线程与用户线程的关系,前面的超时检测线程好比守护线程,后者好比用户线程,当用户线程执行完毕后,守护线程也就没有存在的必要了
提交TimedCancelable类型对象方法如下
/**
* 提交任务1
* Submit a {@link TimedCancelable} for execution and return a
* {@link TaskHandle} for the running task or null if the task has not been
* accepted. After {@link LazyThreadPool#shutdown(boolean, long)} returns
* this will always return null.
*/
TaskHandle submit(TimedCancelable cancelable) {
try {
// When timeoutTask is run it will cancel 'cancelable'.
TimeoutTask timeoutTask = new TimeoutTask(cancelable); // Schedule timeoutTask to run when 'cancelable's maximum run interval
// has expired.
// timeoutFuture will be used to cancel timeoutTask when 'cancelable'
// completes.
//延迟执行
Future<?> timeoutFuture = timeoutService.schedule(timeoutTask,
maximumTaskLifeMillis, TimeUnit.MILLISECONDS); //cancelable执行完毕之后,超时线程不再执行
// cancelTimeoutRunnable runs 'cancelable'. When 'cancelable' completes
// cancelTimeoutRunnable cancels 'timeoutTask'. This saves system
// resources. In addition it prevents timeout task from running and
// calling cancel after 'cancelable' completes successfully.
CancelTimeoutRunnable cancelTimeoutRunnable =
new CancelTimeoutRunnable(cancelable, timeoutFuture); // taskFuture is used to cancel 'cancelable' and to determine if
// 'cancelable' is done.
Future<?> taskFuture =
completionService.submit(cancelTimeoutRunnable, null);
TaskHandle handle =
new TaskHandle(cancelable, taskFuture, clock.getTimeMillis()); // TODO(strellis): test/handle timer pop/cancel before submit. In
// production with a 30 minute timeout this should never happen.
timeoutTask.setTaskHandle(handle);
return handle;
} catch (RejectedExecutionException re) {
if (!executor.isShutdown()) {
LOGGER.log(Level.SEVERE, "Unable to execute task", re);
}
return null;
}
}
首先构造超时检测任务对象,该类为静态内部类
/**
* 静态内部类 检测线程超时
* A task that cancels another task that is running a {@link TimedCancelable}.
* The {@link TimeoutTask} should be scheduled to run when the interval for
* the {@link TimedCancelable} to run expires.
*/
private static class TimeoutTask implements Runnable {
final TimedCancelable timedCancelable;
private volatile TaskHandle taskHandle; TimeoutTask(TimedCancelable timedCancelable) {
this.timedCancelable = timedCancelable;
} public void run() {
if (taskHandle != null) {
timedCancelable.timeout(taskHandle);
}
} void setTaskHandle(TaskHandle taskHandle) {
this.taskHandle = taskHandle;
}
}
然后延迟执行该线程,获得Future<?> timeoutFuture线程句柄
// Schedule timeoutTask to run when 'cancelable's maximum run interval
// has expired.
// timeoutFuture will be used to cancel timeoutTask when 'cancelable'
// completes.
//延迟执行
Future<?> timeoutFuture = timeoutService.schedule(timeoutTask,
maximumTaskLifeMillis, TimeUnit.MILLISECONDS);
然后构造CancelTimeoutRunnable对象,传入TimedCancelable类型对象和Future<?> timeoutFuture线程句柄
//cancelable执行完毕之后,超时线程不再执行
// cancelTimeoutRunnable runs 'cancelable'. When 'cancelable' completes
// cancelTimeoutRunnable cancels 'timeoutTask'. This saves system
// resources. In addition it prevents timeout task from running and
// calling cancel after 'cancelable' completes successfully.
CancelTimeoutRunnable cancelTimeoutRunnable =
new CancelTimeoutRunnable(cancelable, timeoutFuture);
内部类LazyThreadPool的内部类CancelTimeoutRunnable
/**
* 内部类LazyThreadPool的内部类1
* 执行TimedCancelable cancelable的run方法
* 执行完毕后取消超时线程
* A {@link Runnable} for running {@link TimedCancelable} that has been
* guarded by a timeout task. This will cancel the timeout task when the
* {@link TimedCancelable} completes. If the timeout task has already run,
* then canceling it has no effect.
*/
private class CancelTimeoutRunnable implements Runnable {
private final Future<?> timeoutFuture;
private final TimedCancelable cancelable; /**
* Constructs a {@link CancelTimeoutRunnable}.
*
* @param cancelable the {@link TimedCancelable} this runs.
* @param timeoutFuture the {@link Future} for canceling the timeout task.
*/
CancelTimeoutRunnable(TimedCancelable cancelable, Future<?> timeoutFuture) {
this.timeoutFuture = timeoutFuture;
this.cancelable = cancelable;
} public void run() {
try {
cancelable.run();
} finally {
timeoutFuture.cancel(true);
timeoutService.purge();
}
}
}
上面的run方法执行cancelable对象的run方法,执行完毕后取消超时线程并清理队列任务
后面部分是提交任务并获取线程操作句柄
// taskFuture is used to cancel 'cancelable' and to determine if
// 'cancelable' is done.
Future<?> taskFuture =
completionService.submit(cancelTimeoutRunnable, null);
TaskHandle handle =
new TaskHandle(cancelable, taskFuture, clock.getTimeMillis()); // TODO(strellis): test/handle timer pop/cancel before submit. In
// production with a 30 minute timeout this should never happen.
timeoutTask.setTaskHandle(handle);
shutdown方法关闭线程池
/**
* 关闭线程池任务
* Shut down the LazyThreadPool.
* @param interrupt {@code true} if the threads executing tasks task should
* be interrupted; otherwise, in-progress tasks are allowed to
* complete normally.
* @param waitMillis maximum amount of time to wait for tasks to complete.
* @return {@code true} if all the running tasks terminated, or
* {@code false} if some running task did not terminate.
* @throws InterruptedException if interrupted while waiting.
*/
boolean shutdown(boolean interrupt, long waitMillis)
throws InterruptedException {
if (interrupt) {
executor.shutdownNow();
} else {
executor.shutdown();
}
if (timeoutService != null) {
timeoutService.shutdown();
}
try {
return executor.awaitTermination(waitMillis, TimeUnit.MILLISECONDS);
} finally {
completionExecutor.shutdownNow();
if (timeoutService != null) {
timeoutService.shutdownNow();
}
}
}
内部类LazyThreadPool的内部类CompletionTask用于获取线程结果
/**
* 内部类LazyThreadPool的内部类2
* 获取结果线程
* A task that gets completion information from all the tasks that run in a
* {@link CompletionService} and logs uncaught exceptions that cause the
* tasks to fail.
*/
private class CompletionTask implements Runnable {
private void completeTask() throws InterruptedException {
Future<?> future = completionService.take();
try {
future.get();
} catch (CancellationException e) {
LOGGER.info("Batch terminated due to cancellation.");
} catch (ExecutionException e) {
Throwable cause = e.getCause();
// TODO(strellis): Should we call cancelable.cancel() if we get an
// exception?
if (cause instanceof InterruptedException) {
LOGGER.log(Level.INFO, "Batch terminated due to an interrupt.",
cause);
} else {
LOGGER.log(Level.SEVERE, "Batch failed with unhandled exception: ",
cause);
}
}
} public void run() {
try {
while (!Thread.currentThread().isInterrupted()) {
completeTask();
}
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
}
LOGGER.info("Completion task shutdown.");
}
}
}
ThreadNamingThreadFactory为静态内部类,用于构造线程对象
/**
* A {@link ThreadFactory} that adds a prefix to thread names assigned
* by {@link Executors#defaultThreadFactory()} to provide diagnostic
* context in stack traces.
*/
private static class ThreadNamingThreadFactory implements ThreadFactory {
private final ThreadFactory delegate = Executors.defaultThreadFactory();
private final String namePrefix; ThreadNamingThreadFactory(String namePrefix) {
this.namePrefix = namePrefix + "-";
} public Thread newThread(Runnable r) {
Thread t = delegate.newThread(r);
t.setName(namePrefix + t.getName());
return t;
}
}
---------------------------------------------------------------------------
本系列企业搜索引擎开发之连接器connector系本人原创
转载请注明出处 博客园 刺猬的温驯
本人邮箱: chenying998179@163#com (#改为.)
本文链接 http://www.cnblogs.com/chenying99/p/3775701.html
企业搜索引擎开发之连接器connector(二十二)的更多相关文章
- 企业搜索引擎开发之连接器connector(十九)
连接器是基于http协议通过推模式(push)向数据接收服务端推送数据,即xmlfeed格式数据(xml格式),其发送数据接口命名为Pusher Pusher接口定义了与发送数据相关的方法 publi ...
- 企业搜索引擎开发之连接器connector(十八)
创建并启动连接器实例之后,连接器就会基于Http协议向指定的数据接收服务器发送xmlfeed格式数据,我们可以通过配置http代理服务器抓取当前基于http协议格式的数据(或者也可以通过其他网络抓包工 ...
- 企业搜索引擎开发之连接器connector(十六)
本人有一段时间没有接触企业搜索引擎之连接器的开发了,连接器是涉及企业搜索引擎一个重要的组件,在数据源与企业搜索引擎中间起一个桥梁的作用,类似于数据库之JDBC,通过连接器将不同数据源的数据适配到企业搜 ...
- 企业搜索引擎开发之连接器connector(二十九)
在哪里调用监控器管理对象snapshotRepositoryMonitorManager的start方法及stop方法,然后又在哪里调用CheckpointAndChangeQueue对象的resum ...
- 企业搜索引擎开发之连接器connector(二十八)
通常一个SnapshotRepository仓库对象对应一个DocumentSnapshotRepositoryMonitor监视器对象,同时也对应一个快照存储器对象,它们的关联是通过监视器管理对象D ...
- 企业搜索引擎开发之连接器connector(二十六)
连接器通过监视器对象DocumentSnapshotRepositoryMonitor从上文提到的仓库对象SnapshotRepository(数据库仓库为DBSnapshotRepository)中 ...
- 企业搜索引擎开发之连接器connector(二十五)
下面开始具体分析连接器是怎么与连接器实例交互的,这里主要是分析连接器怎么从连接器实例获取数据的(前面文章有涉及基于http协议与连接器的xml格式的交互,连接器对连接器实例的设置都是通过配置文件操作的 ...
- 企业搜索引擎开发之连接器connector(二十四)
本人在上文中提到,连接器实现了两种事件依赖的机制 ,其一是我们手动操作连接器实例时:其二是由连接器的自动更新机制 上文中分析了连接器的自动更新机制,即定时器执行定时任务 那么,如果我们手动操作连接器实 ...
- 企业搜索引擎开发之连接器connector(二十)
连接器里面衔接数据源与数据推送对象的是QueryTraverser类对象,该类实现了Traverser接口 /** * Interface presented by a Traverser. Used ...
随机推荐
- 【linux】mkdir -p命令
如果要创建目录A并创建目录A的子目录B,没有用-p的情况下是mkdir 2次 如果用-p 可以直接创建2个目录 (迭代创建).mkdir -p 目录A/子目录B就可以
- Java 运用流传输文件
实例1 package IO; import java.io.FileReader; import java.io.FileWriter; import java.io.Reader; import ...
- C++ 构造函数_内存分区_对象初始化
内存分区 栈区:int x = 0:int *p = NULL; 定义一个变量,定义一个指针时,会在栈区进行分配内存.分配的内存系统分配收回的,我们不用管. 堆区:int *p = new i ...
- Solr Suggest组件的使用
使用suggest的原因,最主要就是相比于search速度快,In general, we need the autosuggest feature to satisfy two main requi ...
- JMS消息模型
消息机制: 系统之间通信的中介,作为一台单独的服务器部署,大多数使用多个系统之间协作,是系统解耦的常见解决方案. 基于CS架构 作用:多个系统之间解耦,项目可以分开开发,满足显示的高可用(也可以说是异 ...
- nginx正向代理访问百度地图API
正向代理的概念 正向代理,也就是传说中的代理,他的工作原理就像一个跳板,简单的说,我是一个用户,我访问不了某网站,但是我能访问一个代理服务器这个代理服务器呢,他能访问那个我不能访问的网站于是我先连上代 ...
- Java下LDAP操作的资料
话说LDAP真是个诡异的protocol(或者数据库,或者服务,whatever...),没有一个特别形象的spec.这里列出一些筛选出的还可以的文档,都是oracle的: https://docs. ...
- mybatis相对于ibatis的优势
2010年,apache的Ibatis框架停止更新,并移交给了google团队,同时更名为MyBatis.从2010年后Ibatis在没更新过,彻底变成了一个孤儿框架.一个没人维护的框架注定被myba ...
- CloudStack 4.1快照测试
目前4.1只支持存储快照,4.2能支持内快照 1. 选中实例 2. 查看实例卷 3. 创建快照 4. 通过快照创建模板 5. 查看通过快照创建的模板 6. 通过快照创建模板生成的实例 7. 自动定制创 ...
- Golang之一个简单的聊天机器人
翠花,上代码 package main import ( "bufio" "fmt" "os" "strings" ) ...