码迷,mamicode.com
首页 > 其他好文 > 详细

企业搜索引擎开发之连接器connector(二十二)

时间:2014-06-08 19:15:24      阅读:303      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   java   

下面来分析线程执行类,线程池ThreadPool类

对该类的理解需要对java的线程池比较熟悉

该类引用了一个内部类

bubuko.com,布布扣
/**
   * The lazily constructed LazyThreadPool instance.
   */
  private LazyThreadPool lazyThreadPool;
bubuko.com,布布扣

该成员实现了单例模式,即该对象只有一个实例,属于懒汉式单例模式,当实例化该成员时,启用了线程同步机制

bubuko.com,布布扣
/**
   * 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;
  }
bubuko.com,布布扣

线程提交方法如下

bubuko.com,布布扣
/**
   * 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);
    }
  }
bubuko.com,布布扣

这里针对Cancelable对象类型和TimedCancelable类型提交到了不同的方法(多态)

提交Cancelable类型对象比较简单,提交任务后获取操作句柄

bubuko.com,布布扣
/**
     * 提交任务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;
      }
    }
bubuko.com,布布扣

而提交TimedCancelable类型对象则相对比较复杂

基本思路是,首先启动一个延迟执行的线程,即在指定的时间延迟后执行TimedCancelable类型对象的timeout()方法,即取消另外一个线程的执行,即超时检测线程;

然后启动另外一个线程执行TimedCancelable类型对象的run()方法,同时执行完毕后,则同时取消上面的超时检测线程(如果指定时间内未执行完毕,则由超时检测线程来取消执行)。

这里我们可以类比于守护线程与用户线程的关系,前面的超时检测线程好比守护线程,后者好比用户线程,当用户线程执行完毕后,守护线程也就没有存在的必要了

提交TimedCancelable类型对象方法如下

bubuko.com,布布扣
 /**
     * 提交任务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;
      }
    }
bubuko.com,布布扣

首先构造超时检测任务对象,该类为静态内部类

bubuko.com,布布扣
/**
   * 静态内部类  检测线程超时
   * 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;
    }
  }
bubuko.com,布布扣

然后延迟执行该线程,获得Future<?> timeoutFuture线程句柄

bubuko.com,布布扣
// 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);
bubuko.com,布布扣

然后构造CancelTimeoutRunnable对象,传入TimedCancelable类型对象和Future<?> timeoutFuture线程句柄

bubuko.com,布布扣
//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);
bubuko.com,布布扣

内部类LazyThreadPool的内部类CancelTimeoutRunnable

bubuko.com,布布扣
/**
    * 内部类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();
       }
     }
   }
bubuko.com,布布扣

上面的run方法执行cancelable对象的run方法,执行完毕后取消超时线程并清理队列任务

后面部分是提交任务并获取线程操作句柄

bubuko.com,布布扣
// 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);
bubuko.com,布布扣

shutdown方法关闭线程池

bubuko.com,布布扣
/**
     * 关闭线程池任务
     * 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();
        }
      }
    }
bubuko.com,布布扣

内部类LazyThreadPool的内部类CompletionTask用于获取线程结果

bubuko.com,布布扣
/**
    * 内部类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.");
     }
   }
  }
bubuko.com,布布扣

ThreadNamingThreadFactory为静态内部类,用于构造线程对象

bubuko.com,布布扣
/**
   * 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;
    }
  }
bubuko.com,布布扣

---------------------------------------------------------------------------

本系列企业搜索引擎开发之连接器connector系本人原创

转载请注明出处 博客园 刺猬的温驯

本人邮箱: chenying998179@163#com (#改为.)

本文链接 http://www.cnblogs.com/chenying99/p/3775701.html 

企业搜索引擎开发之连接器connector(二十二),布布扣,bubuko.com

企业搜索引擎开发之连接器connector(二十二)

标签:c   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/chenying99/p/3775701.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!