下面列出了怎么用java.util.concurrent.ThreadPoolExecutor.AbortPolicy的API类实例代码及写法,或者点击链接到github查看源代码。
public QuartzThreadPool(final int poolSize, final int threadPriority) {
checkArgument(poolSize > 0, "Pool size must be greater than zero");
checkArgument(threadPriority >= Thread.MIN_PRIORITY && threadPriority <= Thread.MAX_PRIORITY, String
.format("Thread priority value must be an int between %s and %s", Thread.MIN_PRIORITY, Thread.MAX_PRIORITY));
this.threadPoolExecutor = new ThreadPoolExecutor(
poolSize, // core-size
poolSize, // max-size
0L, // keep-alive
TimeUnit.MILLISECONDS,
new SynchronousQueue<>(), // no queuing
new NexusThreadFactory("quartz", "nx-tasks", threadPriority),
new AbortPolicy());
// wrapper for Shiro integration
this.nexusExecutorService = NexusExecutorService.forFixedSubject(
threadPoolExecutor, FakeAlmightySubject.TASK_SUBJECT);
semaphore = new Semaphore(poolSize);
}
/**
* set sdk threadPool.
*
* @return
*/
@Bean
public ThreadPoolTaskExecutor sdkThreadPool() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(corePoolSize);
executor.setMaxPoolSize(maxPoolSize);
executor.setQueueCapacity(queueCapacity);
executor.setKeepAliveSeconds(keepAlive);
executor.setRejectedExecutionHandler(new AbortPolicy());
executor.setThreadNamePrefix("sdkThreadPool-");
executor.initialize();
return executor;
}
/**
* set sdk threadPool.
*
* @return
*/
@Bean
public ThreadPoolTaskExecutor sdkThreadPool() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(corePoolSize);
executor.setMaxPoolSize(maxPoolSize);
executor.setQueueCapacity(queueCapacity);
executor.setKeepAliveSeconds(keepAlive);
executor.setRejectedExecutionHandler(new AbortPolicy());
executor.setThreadNamePrefix("sdkThreadPool-");
executor.initialize();
return executor;
}
/**
* set ThreadPoolTaskExecutor.
*
* @return
*/
@Bean
public ThreadPoolTaskExecutor transExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(50);
executor.setMaxPoolSize(100);
executor.setQueueCapacity(500);
executor.setKeepAliveSeconds(60);
executor.setRejectedExecutionHandler(new AbortPolicy());
executor.setThreadNamePrefix("transExecutor-");
executor.initialize();
return executor;
}
public final synchronized void a(Runnable runnable) {
if (runnable != null) {
try {
if (this.a == null || this.a.isShutdown()) {
this.a = new ThreadPoolExecutor(this.b, this.c, this.d, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(), Executors.defaultThreadFactory(), new AbortPolicy());
}
this.a.execute(runnable);
} catch (Exception e) {
e.printStackTrace();
}
}
}
static ThreadPoolExecutor newDefaultThreadPool() {
final ThreadPoolExecutor result = new ThreadPoolExecutor(
// Bound the pool. Most foreground dependency managers should be called only very rarely, so
// keep a minimal core pool around and only grow it on demand.
1, 16,
// Threads will be kept alive in an idle state for a minute or two. After that, they may be
// garbage-collected, so that we're keeping a larger thread pool only during weird periods of
// congestion. (Note: the background manager will typically keep all threads pretty active, since it's
// repeatedly launching new pingers. The live manager will spin them up and down based on traffic to
// the rather uncommonly used /healthcheck/live uri).
30, TimeUnit.SECONDS,
// Use a blocking queue just to keep track of checks when the world is going wrong. This is mostly useful
// when we're adding a bunch of checks at the same time, such as during a live healthcheck. Might as well
// keep this pretty small, because any nontrivial wait to execute is going to blow up a timeout anyway.
new SynchronousQueue<Runnable>(),
// Name your threads.
new ThreadFactoryBuilder()
.setNameFormat("dependency-default-" + DEFAULT_THREAD_POOL_COUNT.getAndIncrement() + "-checker-%d")
.setDaemon(true)
.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
Logger.getLogger(AbstractDependencyManager.class)
.error("Uncaught throwable in thread " + t.getName() + "/" + t.getId(), e);
}
})
.build(),
// Explicitly restating the default policy here, because healthchecks should Just Not Work if there
// are insufficient resources to support them. Given the smallish queue above, this means that
// we're going to end up throwing exceptions if we get too blocked up somehow.
new AbortPolicy());
result.prestartAllCoreThreads();
return result;
}
@Ignore
@Test
public void givenAbortPolicy_WhenSaturated_ThenShouldThrowRejectedExecutionException() {
executor = new ThreadPoolExecutor(1, 1, 0, MILLISECONDS, new SynchronousQueue<>(), new AbortPolicy());
executor.execute(() -> waitFor(250));
assertThatThrownBy(() -> executor.execute(() -> System.out.println("Will be rejected"))).isInstanceOf(RejectedExecutionException.class);
}
public ThreadPoolExecutorBuilder() {
this(null, null, null, null, false, null,
defaultThreadFactory(), new AbortPolicy());
}