java.util.concurrent.ThreadPoolExecutor.allowCoreThreadTimeOut()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(10.7k)|赞(0)|评价(0)|浏览(139)

本文整理了Java中java.util.concurrent.ThreadPoolExecutor.allowCoreThreadTimeOut()方法的一些代码示例,展示了ThreadPoolExecutor.allowCoreThreadTimeOut()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ThreadPoolExecutor.allowCoreThreadTimeOut()方法的具体详情如下:
包路径:java.util.concurrent.ThreadPoolExecutor
类名称:ThreadPoolExecutor
方法名:allowCoreThreadTimeOut

ThreadPoolExecutor.allowCoreThreadTimeOut介绍

[英]If false (default), core threads stay alive even when idle. If true, core threads use keepAliveTime to time out waiting for work.
[中]如果为false(默认),则即使在空闲时,核心线程仍保持活动状态。如果为true,则核心线程使用keepAliveTime超时等待工作。

代码示例

代码示例来源:origin: Justson/AgentWeb

private void internalInit() {
  if (mThreadPoolExecutor != null && !mThreadPoolExecutor.isShutdown()) {
    mThreadPoolExecutor.shutdownNow();
  }
  mThreadPoolExecutor = new ThreadPoolExecutor(
      CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE_SECONDS, TimeUnit.SECONDS,
      sPoolWorkQueue, sThreadFactory);
  mThreadPoolExecutor.allowCoreThreadTimeOut(true);
}

代码示例来源:origin: apache/activemq

private ThreadPoolExecutor createExecutor() {
    ThreadPoolExecutor exec = new ThreadPoolExecutor(0, 2, 30, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), factory);
    exec.allowCoreThreadTimeOut(true);
    return exec;
  }
}

代码示例来源:origin: spring-projects/spring-framework

executor = new ThreadPoolExecutor(
    this.corePoolSize, this.maxPoolSize, this.keepAliveSeconds, TimeUnit.SECONDS,
    queue, threadFactory, rejectedExecutionHandler) {
executor = new ThreadPoolExecutor(
    this.corePoolSize, this.maxPoolSize, this.keepAliveSeconds, TimeUnit.SECONDS,
    queue, threadFactory, rejectedExecutionHandler);
executor.allowCoreThreadTimeOut(true);

代码示例来源:origin: apache/hbase

/**
 * Create a new CachedThreadPool with a bounded number as the maximum
 * thread size in the pool.
 *
 * @param maxCachedThread the maximum thread could be created in the pool
 * @param timeout the maximum time to wait
 * @param unit the time unit of the timeout argument
 * @param threadFactory the factory to use when creating new threads
 * @return threadPoolExecutor the cachedThreadPool with a bounded number
 * as the maximum thread size in the pool.
 */
public static ThreadPoolExecutor getBoundedCachedThreadPool(
  int maxCachedThread, long timeout, TimeUnit unit,
  ThreadFactory threadFactory) {
 ThreadPoolExecutor boundedCachedThreadPool =
  new ThreadPoolExecutor(maxCachedThread, maxCachedThread, timeout,
   unit, new LinkedBlockingQueue<>(), threadFactory);
 // allow the core pool threads timeout and terminate
 boundedCachedThreadPool.allowCoreThreadTimeOut(true);
 return boundedCachedThreadPool;
}

代码示例来源:origin: Alluxio/alluxio

/**
 * Creates a new instance of {@link AsyncUfsAbsentPathCache}.
 *
 * @param mountTable the mount table
 * @param numThreads the maximum number of threads for the async thread pool
 */
public AsyncUfsAbsentPathCache(MountTable mountTable, int numThreads) {
 mMountTable = mountTable;
 mCurrentPaths = new ConcurrentHashMap<>(8, 0.95f, 8);
 mCache = CacheBuilder.newBuilder().maximumSize(MAX_PATHS).build();
 mThreads = numThreads;
 mPool = new ThreadPoolExecutor(mThreads, mThreads, THREAD_KEEP_ALIVE_SECONDS,
   TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(),
   ThreadFactoryUtils.build("UFS-Absent-Path-Cache-%d", true));
 mPool.allowCoreThreadTimeOut(true);
}

代码示例来源:origin: EngineHub/WorldEdit

/**
 * Creates a thread pool that creates new threads as needed up to
 * a maximum number of threads, but will reuse previously constructed
 * threads when they are available.
 *
 * @param minThreads the minimum number of threads to have at a given time
 * @param maxThreads the maximum number of threads to have at a given time
 * @param queueSize the size of the queue before new submissions are rejected
 * @return the newly created thread pool
 */
public static ExecutorService newBoundedCachedThreadPool(int minThreads, int maxThreads, int queueSize) {
  ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
      minThreads, maxThreads,
      60L, TimeUnit.SECONDS,
      new ArrayBlockingQueue<>(queueSize));
  threadPoolExecutor.allowCoreThreadTimeOut(true);
  return threadPoolExecutor;
}

代码示例来源:origin: apache/geode

public static ExecutorService newFixedThreadPoolWithFeedSize(String threadName,
  int poolSize, int feedSize) {
 LinkedBlockingQueue<Runnable> feed = new LinkedBlockingQueue<>(feedSize);
 RejectedExecutionHandler rejectionHandler = new ThreadPoolExecutor.CallerRunsPolicy();
 ThreadFactory threadFactory = new LoggingThreadFactory(threadName);
 ThreadPoolExecutor executor = new ThreadPoolExecutor(poolSize, poolSize, 10, SECONDS, feed,
   threadFactory, rejectionHandler);
 executor.allowCoreThreadTimeOut(true);
 return executor;
}

代码示例来源:origin: apache/activemq

private ThreadPoolExecutor createExecutor() {
  ThreadPoolExecutor exec = new ThreadPoolExecutor(0, Integer.MAX_VALUE, getDefaultKeepAliveTime(), TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), factory);
  exec.allowCoreThreadTimeOut(true);
  return exec;
}

代码示例来源:origin: SonarSource/sonarqube

private static ThreadPoolExecutor createDelegate() {
 ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
  MAX_THREAD_COUNT, MAX_THREAD_COUNT,
  KEEP_ALIVE_TIME_IN_MINUTES, MINUTES,
  new LinkedBlockingQueue<>(UNLIMITED_QUEUE),
  new ThreadFactoryBuilder()
   .setDaemon(false)
   .setNameFormat("SQ_async-%d")
   .setUncaughtExceptionHandler(((t, e) -> LOG.error("Thread " + t + " failed unexpectedly", e)))
   .build());
 threadPoolExecutor.allowCoreThreadTimeOut(true);
 return threadPoolExecutor;
}

代码示例来源:origin: apache/zookeeper

this.connectionExecutor = new ThreadPoolExecutor(3,
    quorumCnxnThreadsSize, 60, TimeUnit.SECONDS,
    new SynchronousQueue<Runnable>(), daemonThFactory);
this.connectionExecutor.allowCoreThreadTimeOut(true);

代码示例来源:origin: wildfly/wildfly

public AsyncNoBundler() {
  thread_pool=new ThreadPoolExecutor(0, max_threads,
                    30000, TimeUnit.MICROSECONDS,
                    new SynchronousQueue<>(),
                    new DefaultThreadFactory("async-bundler", true, true),
                    new ThreadPoolExecutor.CallerRunsPolicy());
  thread_pool.allowCoreThreadTimeOut(true);
}

代码示例来源:origin: apache/hbase

private void createBatchPool(Configuration conf) {
 // Use the same config for keep alive as in ConnectionImplementation.getBatchPool();
 int maxThreads = conf.getInt("hbase.multihconnection.threads.max", 256);
 if (maxThreads == 0) {
  maxThreads = Runtime.getRuntime().availableProcessors() * 8;
 }
 long keepAliveTime = conf.getLong("hbase.multihconnection.threads.keepalivetime", 60);
 LinkedBlockingQueue<Runnable> workQueue =
   new LinkedBlockingQueue<>(maxThreads
     * conf.getInt(HConstants.HBASE_CLIENT_MAX_TOTAL_TASKS,
      HConstants.DEFAULT_HBASE_CLIENT_MAX_TOTAL_TASKS));
 ThreadPoolExecutor tpe =
   new ThreadPoolExecutor(maxThreads, maxThreads, keepAliveTime, TimeUnit.SECONDS, workQueue,
     Threads.newDaemonThreadFactory("MultiHConnection" + "-shared-"));
 tpe.allowCoreThreadTimeOut(true);
 this.batchPool = tpe;
}

代码示例来源:origin: apache/hbase

FlushTableSubprocedurePool(String name, Configuration conf, Abortable abortable) {
 this.abortable = abortable;
 // configure the executor service
 long keepAlive = conf.getLong(
  RegionServerFlushTableProcedureManager.FLUSH_TIMEOUT_MILLIS_KEY,
  RegionServerFlushTableProcedureManager.FLUSH_TIMEOUT_MILLIS_DEFAULT);
 int threads = conf.getInt(CONCURENT_FLUSH_TASKS_KEY, DEFAULT_CONCURRENT_FLUSH_TASKS);
 this.name = name;
 executor = new ThreadPoolExecutor(threads, threads, keepAlive, TimeUnit.MILLISECONDS,
   new LinkedBlockingQueue<>(), new DaemonThreadFactory("rs("
     + name + ")-flush-proc-pool"));
 executor.allowCoreThreadTimeOut(true);
 taskPool = new ExecutorCompletionService<>(executor);
}

代码示例来源:origin: apache/hbase

SnapshotSubprocedurePool(String name, Configuration conf, Abortable abortable) {
 this.abortable = abortable;
 // configure the executor service
 long keepAlive = conf.getLong(
  RegionServerSnapshotManager.SNAPSHOT_TIMEOUT_MILLIS_KEY,
  RegionServerSnapshotManager.SNAPSHOT_TIMEOUT_MILLIS_DEFAULT);
 int threads = conf.getInt(CONCURENT_SNAPSHOT_TASKS_KEY, DEFAULT_CONCURRENT_SNAPSHOT_TASKS);
 this.name = name;
 executor = new ThreadPoolExecutor(threads, threads, keepAlive, TimeUnit.MILLISECONDS,
   new LinkedBlockingQueue<>(), new DaemonThreadFactory("rs("
     + name + ")-snapshot-pool"));
 executor.allowCoreThreadTimeOut(true);
 taskPool = new ExecutorCompletionService<>(executor);
}

代码示例来源:origin: org.apache.hadoop/hadoop-common

GroupCacheLoader() {
 if (reloadGroupsInBackground) {
  ThreadFactory threadFactory = new ThreadFactoryBuilder()
    .setNameFormat("Group-Cache-Reload")
    .setDaemon(true)
    .build();
  // With coreThreadCount == maxThreadCount we effectively
  // create a fixed size thread pool. As allowCoreThreadTimeOut
  // has been set, all threads will die after 60 seconds of non use
  ThreadPoolExecutor parentExecutor =  new ThreadPoolExecutor(
    reloadGroupsThreadCount,
    reloadGroupsThreadCount,
    60,
    TimeUnit.SECONDS,
    new LinkedBlockingQueue<>(),
    threadFactory);
  parentExecutor.allowCoreThreadTimeOut(true);
  executorService = MoreExecutors.listeningDecorator(parentExecutor);
 }
}

代码示例来源:origin: lingochamp/FileDownloader

public static ThreadPoolExecutor newDefaultThreadPool(int nThreads,
                           LinkedBlockingQueue<Runnable> queue,
                           String prefix) {
  final ThreadPoolExecutor executor = new ThreadPoolExecutor(nThreads, nThreads,
      DEFAULT_IDLE_SECOND, TimeUnit.SECONDS, queue,
      new FileDownloadThreadFactory(prefix));
  executor.allowCoreThreadTimeOut(true);
  return executor;
}

代码示例来源:origin: org.apache.hadoop/hadoop-common

ThreadPoolExecutor executor = new ThreadPoolExecutor(
  CORE_THREADS_PER_VOLUME, MAXIMUM_THREADS_PER_VOLUME, 
  THREADS_KEEP_ALIVE_SECONDS, TimeUnit.SECONDS, 
executor.allowCoreThreadTimeOut(true);
executors.put(volumes[v], executor);

代码示例来源:origin: org.springframework/spring-context

executor = new ThreadPoolExecutor(
    this.corePoolSize, this.maxPoolSize, this.keepAliveSeconds, TimeUnit.SECONDS,
    queue, threadFactory, rejectedExecutionHandler) {
executor = new ThreadPoolExecutor(
    this.corePoolSize, this.maxPoolSize, this.keepAliveSeconds, TimeUnit.SECONDS,
    queue, threadFactory, rejectedExecutionHandler);
executor.allowCoreThreadTimeOut(true);

代码示例来源:origin: apache/hbase

@InterfaceAudience.Private
public static ThreadPoolExecutor getDefaultExecutor(Configuration conf) {
 int maxThreads = conf.getInt("hbase.htable.threads.max", Integer.MAX_VALUE);
 if (maxThreads == 0) {
  maxThreads = 1; // is there a better default?
 }
 int corePoolSize = conf.getInt("hbase.htable.threads.coresize", 1);
 long keepAliveTime = conf.getLong("hbase.htable.threads.keepalivetime", 60);
 // Using the "direct handoff" approach, new threads will only be created
 // if it is necessary and will grow unbounded. This could be bad but in HCM
 // we only create as many Runnables as there are region servers. It means
 // it also scales when new region servers are added.
 ThreadPoolExecutor pool = new ThreadPoolExecutor(corePoolSize, maxThreads, keepAliveTime,
  TimeUnit.SECONDS, new SynchronousQueue<>(), Threads.newDaemonThreadFactory("htable"));
 pool.allowCoreThreadTimeOut(true);
 return pool;
}

代码示例来源:origin: lealone/Lealone

private static ThreadPoolExecutor getThreadPoolExecutor() {
  int corePoolSize = 3;
  int maxPoolSize = Integer.MAX_VALUE;
  int keepAliveTime = 3;
  ThreadPoolExecutor executor = new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime, TimeUnit.SECONDS,
      new SynchronousQueue<Runnable>(), new NamedThreadFactory(ThreadPool.class.getSimpleName()));
  executor.allowCoreThreadTimeOut(true);
  return executor;
}

相关文章

ThreadPoolExecutor类方法