本文整理汇总了Java中java.util.concurrent.ThreadPoolExecutor.allowCoreThreadTimeOut方法的典型用法代码示例。如果您正苦于以下问题:Java ThreadPoolExecutor.allowCoreThreadTimeOut方法的具体用法?Java ThreadPoolExecutor.allowCoreThreadTimeOut怎么用?Java ThreadPoolExecutor.allowCoreThreadTimeOut使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.ThreadPoolExecutor
的用法示例。
在下文中一共展示了ThreadPoolExecutor.allowCoreThreadTimeOut方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getDefaultExecutor
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
@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?
}
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(1, maxThreads, keepAliveTime, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>(), Threads.newDaemonThreadFactory("htable"));
pool.allowCoreThreadTimeOut(true);
return pool;
}
示例2: AsyncDiskService
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
/**
* Create a AsyncDiskServices with a set of volumes (specified by their
* root directories).
*
* The AsyncDiskServices uses one ThreadPool per volume to do the async
* disk operations.
*
* @param volumes The roots of the file system volumes.
*/
public AsyncDiskService(String[] volumes) {
threadFactory = new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
return new Thread(threadGroup, r);
}
};
// Create one ThreadPool per volume
for (int v = 0 ; v < volumes.length; v++) {
ThreadPoolExecutor executor = new ThreadPoolExecutor(
CORE_THREADS_PER_VOLUME, MAXIMUM_THREADS_PER_VOLUME,
THREADS_KEEP_ALIVE_SECONDS, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(), threadFactory);
// This can reduce the number of running threads
executor.allowCoreThreadTimeOut(true);
executors.put(volumes[v], executor);
}
}
示例3: createIOPoolExecutor
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
private ThreadPoolExecutor createIOPoolExecutor(){
//IO线程工厂类
ThreadFactory threadFactory = new ThreadFactory() {
@Override
public Thread newThread(@NonNull Runnable runnable) {
Thread thread = new Thread(runnable);
thread.setName("dunit-io");
return thread;
}
};
//创建一个任务拒绝策略
//直接忽略新进的任务
RejectedExecutionHandler rejectedExecutionHandler = new ThreadPoolExecutor.DiscardPolicy();
//创建一个最大线程数为3的线程池
ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(1, 3, 3, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(10),threadFactory,rejectedExecutionHandler);
//当核心线程空闲时,允许杀死核心线程
poolExecutor.allowCoreThreadTimeOut(true);
return poolExecutor;
}
示例4: initializeCacheExecutor
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
protected ThreadPoolExecutor initializeCacheExecutor(File parent) {
if (storageType.isTransient()) {
return null;
}
if (dataset.datanode == null) {
// FsVolumeImpl is used in test.
return null;
}
final int maxNumThreads = dataset.datanode.getConf().getInt(
DFSConfigKeys.DFS_DATANODE_FSDATASETCACHE_MAX_THREADS_PER_VOLUME_KEY,
DFSConfigKeys.DFS_DATANODE_FSDATASETCACHE_MAX_THREADS_PER_VOLUME_DEFAULT);
ThreadFactory workerFactory = new ThreadFactoryBuilder()
.setDaemon(true)
.setNameFormat("FsVolumeImplWorker-" + parent.toString() + "-%d")
.build();
ThreadPoolExecutor executor = new ThreadPoolExecutor(
1, maxNumThreads,
60, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(),
workerFactory);
executor.allowCoreThreadTimeOut(true);
return executor;
}
示例5: getDefaultExecutorService
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
/**
* Returns a new instance of a default ExecutorService.<p>
*
* The default executor service is a thread-limited pool
* where the number of threads is one less than the number
* of processors set with {@link #setNumberOfProcessors(int)}.
*
* @return A new instance of a default ExecutorService.
*
* @since 1.3
*/
public static ExecutorService getDefaultExecutorService()
{
// Executor service with all daemon threads, to avoid clean-up
ThreadFactory threadFactory = new ThreadFactory()
{
public Thread newThread(Runnable runnable)
{
Thread thread = this.defaultThreadFactory.newThread(runnable);
thread.setDaemon(true);
return thread;
}
private ThreadFactory defaultThreadFactory = Executors.defaultThreadFactory();
};
int numberOfThreads = Math.max(1, getContext().getNumberOfProcessors() - 1);
ThreadPoolExecutor executorService = new ThreadPoolExecutor(numberOfThreads, numberOfThreads, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), threadFactory);
executorService.allowCoreThreadTimeOut(true);
return executorService;
}
示例6: buildDownloadExecutor
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
private static ExecutorService buildDownloadExecutor() {
final int maxConcurrent = 5;
// Create a bounded thread pool for executing downloads; it creates
// threads as needed (up to maximum) and reclaims them when finished.
final ThreadPoolExecutor executor = new ThreadPoolExecutor(
maxConcurrent, maxConcurrent, 10, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>()) {
@Override
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
if (t == null && r instanceof Future<?>) {
try {
((Future<?>) r).get();
} catch (CancellationException ce) {
t = ce;
} catch (ExecutionException ee) {
t = ee.getCause();
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
}
}
if (t != null) {
Log.w(TAG, "Uncaught exception", t);
}
}
};
executor.allowCoreThreadTimeOut(true);
return executor;
}
示例7: initInternal
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
@Override
protected void initInternal() throws LifecycleException {
BlockingQueue<Runnable> startStopQueue = new LinkedBlockingQueue<Runnable>();
startStopExecutor = new ThreadPoolExecutor(getStartStopThreadsInternal(), getStartStopThreadsInternal(), 10,
TimeUnit.SECONDS, startStopQueue, new StartStopThreadFactory(getName() + "-startStop-"));
startStopExecutor.allowCoreThreadTimeOut(true);
super.initInternal();
}
示例8: createAstSerializerExecutorService
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
/**
* Creates the AST serializer executor service used for in-memory serialization of split functions' ASTs.
* It is created with an unbounded queue (so it can queue any number of pending tasks). Its core and max
* threads is the same, but they are all allowed to time out so when there's no work, they can all go
* away. The threads will be daemons, and they will time out if idle for a minute. Their priority is also
* slightly lower than normal priority as we'd prefer the CPU to keep running the program; serializing
* split function is a memory conservation measure (it allows us to release the AST), it can wait a bit.
* @return an executor service with above described characteristics.
*/
private static ExecutorService createAstSerializerExecutorService() {
final int threads = Math.max(1, Options.getIntProperty("nashorn.serialize.threads", Runtime.getRuntime().availableProcessors() / 2));
final ThreadPoolExecutor service = new ThreadPoolExecutor(threads, threads, 1, TimeUnit.MINUTES, new LinkedBlockingDeque<>(),
(r) -> {
final Thread t = new Thread(r, "Nashorn AST Serializer");
t.setDaemon(true);
t.setPriority(Thread.NORM_PRIORITY - 1);
return t;
});
service.allowCoreThreadTimeOut(true);
return service;
}
示例9: initInternal
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
@Override
protected void initInternal() throws LifecycleException {
BlockingQueue<Runnable> startStopQueue =
new LinkedBlockingQueue<Runnable>();
startStopExecutor = new ThreadPoolExecutor(
getStartStopThreadsInternal(),
getStartStopThreadsInternal(), 10, TimeUnit.SECONDS,
startStopQueue,
new StartStopThreadFactory(getName() + "-startStop-"));
startStopExecutor.allowCoreThreadTimeOut(true);
super.initInternal();
}
示例10: test
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
void test(String[] args) throws Throwable {
final int threadCount = 10;
final int timeoutMillis = 30;
BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(2*threadCount);
ThreadPoolExecutor tpe
= new ThreadPoolExecutor(threadCount, threadCount,
timeoutMillis, TimeUnit.MILLISECONDS,
q, new IdentifiableThreadFactory());
equal(tpe.getCorePoolSize(), threadCount);
check(! tpe.allowsCoreThreadTimeOut());
tpe.allowCoreThreadTimeOut(true);
check(tpe.allowsCoreThreadTimeOut());
equal(countExecutorThreads(), 0);
long startTime = System.nanoTime();
for (int i = 0; i < threadCount; i++) {
tpe.submit(() -> {});
int count = countExecutorThreads();
if (millisElapsedSince(startTime) < timeoutMillis)
equal(count, i + 1);
}
while (countExecutorThreads() > 0 &&
millisElapsedSince(startTime) < LONG_DELAY_MS)
Thread.yield();
equal(countExecutorThreads(), 0);
check(millisElapsedSince(startTime) >= timeoutMillis);
tpe.shutdown();
check(tpe.allowsCoreThreadTimeOut());
check(tpe.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
if (failed > 0) throw new Exception("Some tests failed");
}
示例11: newDynamicSingleThreadedExecutor
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
public static ExecutorService newDynamicSingleThreadedExecutor() {
ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 60, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>());
executor.allowCoreThreadTimeOut(true);
return executor;
}
示例12: initializeExecutor
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
@Override
protected ExecutorService initializeExecutor(
ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler) {
BlockingQueue<Runnable> queue = createQueue(this.queueCapacity);
ThreadPoolExecutor executor = new ThreadPoolExecutor(
this.corePoolSize, this.maxPoolSize, this.keepAliveSeconds, TimeUnit.SECONDS,
queue, threadFactory, rejectedExecutionHandler);
if (this.allowCoreThreadTimeOut) {
executor.allowCoreThreadTimeOut(true);
}
this.threadPoolExecutor = executor;
return executor;
}
示例13: FreeMarkerService
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
private FreeMarkerService(Builder bulder) {
maxOutputLength = bulder.getMaxOutputLength();
maxThreads = bulder.getMaxThreads();
maxQueueLength = bulder.getMaxQueueLength();
maxTemplateExecutionTime = bulder.getMaxTemplateExecutionTime();
int actualMaxQueueLength = maxQueueLength != null
? maxQueueLength
: Math.max(
MIN_DEFAULT_MAX_QUEUE_LENGTH,
(int) (MAX_DEFAULT_MAX_QUEUE_LENGTH_MILLISECONDS / maxTemplateExecutionTime));
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
maxThreads, maxThreads,
THREAD_KEEP_ALIVE_TIME, TimeUnit.MILLISECONDS,
new BlockingArrayQueue<Runnable>(actualMaxQueueLength));
threadPoolExecutor.allowCoreThreadTimeOut(true);
templateExecutor = threadPoolExecutor;
freeMarkerConfig = new Configuration(Configuration.getVersion());
freeMarkerConfig.setNewBuiltinClassResolver(TemplateClassResolver.ALLOWS_NOTHING_RESOLVER);
freeMarkerConfig.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
freeMarkerConfig.setLogTemplateExceptions(false);
freeMarkerConfig.setAttemptExceptionReporter(new AttemptExceptionReporter() {
@Override
public void report(TemplateException te, Environment env) {
// Suppress it
}
});
freeMarkerConfig.setLocale(AllowedSettingValuesMaps.DEFAULT_LOCALE);
freeMarkerConfig.setTimeZone(AllowedSettingValuesMaps.DEFAULT_TIME_ZONE);
freeMarkerConfig.setOutputFormat(AllowedSettingValuesMaps.DEFAULT_OUTPUT_FORMAT);
freeMarkerConfig.setOutputEncoding("UTF-8");
}
示例14: init
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
public void init(Context context, @NonNull DownloadConfiguration config) {
if (this.mConfig != null) {
L.w("DownloadManger has already been initialized before");
} else if (config.getThreadNum() > config.getMaxThreadNum()) {
throw new IllegalArgumentException("thread num must < max thread num");
} else {
this.mConfig = config;
this.mDBManager = DataBaseManager.getInstance(context);
ThreadPoolExecutor executor = new ThreadPoolExecutor(this.mConfig.getMaxThreadNum(), this.mConfig.getMaxThreadNum(), 60, TimeUnit.SECONDS, new LinkedBlockingQueue());
executor.allowCoreThreadTimeOut(true);
this.mExecutorService = executor;
this.mDelivery = new DownloadStatusDeliveryImpl(new Handler(Looper.getMainLooper()));
}
}
示例15: createNewThreadPoolService
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
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(nThreads, nThreads, 3L,
TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
pool.setThreadFactory(tf);
pool.allowCoreThreadTimeOut(true);
return pool;
}