Esempio n. 1
0
 @GwtIncompatible // TODO
 private static void useDaemonThreadFactory(ThreadPoolExecutor executor) {
   executor.setThreadFactory(
       new ThreadFactoryBuilder()
           .setDaemon(true)
           .setThreadFactory(executor.getThreadFactory())
           .build());
 }
Esempio n. 2
0
 /**
  * Creates a new {@link ThreadPoolExecutor} ready to carry out work. All pools are pre-started by
  * default and will terminate after not receiving work for the argued timeout value.
  *
  * @param poolName the name of this thread pool.
  * @param poolSize the size of this thread pool.
  * @param poolPriority the priority of this thread pool.
  * @param timeout how long in minutes it takes for threads in this pool to timeout.
  * @return the newly constructed thread pool.
  */
 public static ThreadPoolExecutor createThreadPool(
     String poolName, int poolSize, int poolPriority, long timeout) {
   ThreadPoolExecutor threadPool = (ThreadPoolExecutor) Executors.newFixedThreadPool(poolSize);
   threadPool.setThreadFactory(new ThreadProvider(poolName, poolPriority, true));
   threadPool.setRejectedExecutionHandler(new ThreadPoolRejectedExecutionHook());
   threadPool.setKeepAliveTime(timeout, TimeUnit.MINUTES);
   threadPool.allowCoreThreadTimeOut(true);
   threadPool.prestartAllCoreThreads();
   return threadPool;
 }
  /**
   * Creates a new custom thread pool
   *
   * @param pattern pattern of the thread name
   * @param name ${name} in the pattern name
   * @param corePoolSize the core size
   * @param maxPoolSize the maximum pool size
   * @param keepAliveTime keep alive time
   * @param timeUnit keep alive time unit
   * @param maxQueueSize the maximum number of tasks in the queue, use <tt>Integer.MAX_VALUE</tt> or
   *     <tt>-1</tt> to indicate unbounded
   * @param rejectedExecutionHandler the handler for tasks which cannot be executed by the thread
   *     pool. If <tt>null</tt> is provided then {@link
   *     java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy CallerRunsPolicy} is used.
   * @param daemon whether the threads is daemon or not
   * @return the created pool
   * @throws IllegalArgumentException if parameters is not valid
   */
  public static ExecutorService newThreadPool(
      final String pattern,
      final String name,
      int corePoolSize,
      int maxPoolSize,
      long keepAliveTime,
      TimeUnit timeUnit,
      int maxQueueSize,
      RejectedExecutionHandler rejectedExecutionHandler,
      final boolean daemon) {

    // validate max >= core
    if (maxPoolSize < corePoolSize) {
      throw new IllegalArgumentException(
          "MaxPoolSize must be >= corePoolSize, was " + maxPoolSize + " >= " + corePoolSize);
    }

    BlockingQueue<Runnable> queue;
    if (corePoolSize == 0 && maxQueueSize <= 0) {
      // use a synchronous queue
      queue = new SynchronousQueue<Runnable>();
      // and force 1 as pool size to be able to create the thread pool by the JDK
      corePoolSize = 1;
      maxPoolSize = 1;
    } else if (maxQueueSize <= 0) {
      // unbounded task queue
      queue = new LinkedBlockingQueue<Runnable>();
    } else {
      // bounded task queue
      queue = new LinkedBlockingQueue<Runnable>(maxQueueSize);
    }
    ThreadPoolExecutor answer =
        new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime, timeUnit, queue);
    answer.setThreadFactory(
        new ThreadFactory() {
          public Thread newThread(Runnable r) {
            Thread answer = new Thread(r, getThreadName(pattern, name));
            answer.setDaemon(daemon);
            return answer;
          }
        });
    if (rejectedExecutionHandler == null) {
      rejectedExecutionHandler = new ThreadPoolExecutor.CallerRunsPolicy();
    }
    answer.setRejectedExecutionHandler(rejectedExecutionHandler);
    return answer;
  }
  protected ThreadPoolExecutor createNewThreadPoolService(Configuration conf) {
    int nThreads =
        conf.getInt(
            YarnConfiguration.RM_DELEGATION_TOKEN_RENEWER_THREAD_COUNT,
            YarnConfiguration.DEFAULT_RM_DELEGATION_TOKEN_RENEWER_THREAD_COUNT);

    ThreadFactory tf =
        new ThreadFactoryBuilder().setNameFormat("DelegationTokenRenewer #%d").build();
    ThreadPoolExecutor pool =
        new ThreadPoolExecutor(
            (5 < nThreads ? 5 : nThreads),
            nThreads,
            3L,
            TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>());
    pool.setThreadFactory(tf);
    pool.allowCoreThreadTimeOut(true);
    return pool;
  }
 private void configure(final String name) {
   /*
    * All executors should be shutdown on application shutdown.
    */
   ShutdownHookManager.register(shutdownHook);
   /*
    * All threads should stop after 60 seconds of idle time
    */
   delegate.setKeepAliveTime(
       FIXED_THREAD_KEEPALIVE_TIMEOUT.longValue(), FIXED_THREAD_KEEPALIVE_TIMEOUT.getTimeUnit());
   /*
    * Fixes non starting with corepoolsize von 0 and not filled queue (Java Conurrency In Practice Chapter 8.3.1).
    * If this bug can occur, a exception would be thrown here.
    */
   delegate.allowCoreThreadTimeOut(true);
   /*
    * Named threads improve debugging.
    */
   delegate.setThreadFactory(new WrappedThreadFactory(name, delegate.getThreadFactory()));
 }
Esempio n. 6
0
  static {
    ThreadPoolExecutor executor =
        new ThreadPoolExecutor(
            10,
            100,
            60,
            TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(10),
            new ThreadPoolExecutor.CallerRunsPolicy());
    executor.setThreadFactory(
        new ThreadFactory() {
          int i = 0;

          @Override
          public Thread newThread(Runnable runnable) {
            Thread thread = new Thread(runnable);
            thread.setDaemon(true);
            thread.setName("Mustache-FutureWriter-" + i++);
            return thread;
          }
        });
    es = executor;
  }
 public void testMustNotBeAbleToShutdownGlobalPool() {
   ExecutorService service = AppExecutorUtil.getAppExecutorService();
   try {
     service.shutdown();
     fail();
   } catch (Exception ignored) {
   }
   try {
     service.shutdownNow();
     fail();
   } catch (Exception ignored) {
   }
   try {
     ((ThreadPoolExecutor) service).setThreadFactory(Thread::new);
     fail();
   } catch (Exception ignored) {
   }
   try {
     ((ThreadPoolExecutor) service).setCorePoolSize(0);
     fail();
   } catch (Exception ignored) {
   }
 }
Esempio n. 8
0
 public void setThreadFactory(ThreadFactory factory) {
   pool.setThreadFactory(factory);
 }