企业搜索引擎开发之连接器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 ...
随机推荐
- Ubuntu各种indicator汇总
1. indicator-multiload: 可视化图形显示 sudo apt-get install indicator-multiload 2. indicator-sysmonitor: 以 ...
- vim配置之安装脚本
vimConfig/install/install.sh git clone https://github.com/gmarik/vundle.git ~/.vim/bundle/vundle cp ...
- 三.jQuery源码解析之jQuery的框架图
这张图片是对jQuery源码截图,一点一点拼出来的. 现在根据这张图片来对jQuery框架做一些说明. 一.16~9404行可以发现,最外层是一个自调用函数.当jQuery初始化时,这个自调用函数包含 ...
- Windows 远程桌面连接Ubuntu16.04图像界面
1.安装xrdp sudo apt-get install xrdp 2. 安装vnc4server sudo apt-get install vnc4server 3. 安装xubuntu-desk ...
- django-引用静态文件
1.需要配置settings # 静态文件目录 STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static') 2.页面加载静态文件 {% load sta ...
- 让别人能登陆你的mysql
线上的数据库肯定是不能轻易在开发新功能的时候动的,如果你的数据库跟线上不一样了又没有新数据库的备份,就很麻烦. 当然去动线上数据库,出了什么问题我是不想背锅的. 最稳健的办法!让管理线上数据库的同学, ...
- 迷你MVVM框架 avalonjs 1.3.3发布
大家可以在仓库中看到,多出了一个叫avalon.observe的东西,它是基于Object.observe,dataset, Promise等新API实现.其中,它也使用全新的静态收集依赖的机制,这个 ...
- open File Browser in shell
[maxosx] open /usr/include [ubuntu] 发现三个,如下: xdg-open xxxx.pdf gnome-open . nautilus . 喜欢把它alias一下 . ...
- 14.Longest Common Prefix (String)
Write a function to find the longest common prefix string amongst an array of strings. class Solutio ...
- 在Action中获取表单提交数据
-----------------siwuxie095 在 Action 中获取表单提交数据 1.之前的 Web 阶段是提交表单到 Servlet,在其中使用 Request 对象 的方法获取数据 2 ...