com.opentable.concurrent.ThreadPoolConfig Maven / Gradle / Ivy
/**
* Copyright (C) 2012 Ness Computing, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.opentable.concurrent;
import java.time.Duration;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor.AbortPolicy;
import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy;
import java.util.concurrent.ThreadPoolExecutor.DiscardOldestPolicy;
import java.util.concurrent.ThreadPoolExecutor.DiscardPolicy;
/**
* Config for {@link ThreadPoolBuilder}.
*/
interface ThreadPoolConfig
{
int DEFAULT_MIN_THREADS = 1;
int DEFAULT_MAX_THREADS = 16;
Duration DEFAULT_TIMEOUT = Duration.ofMinutes(30);
int DEFAULT_QUEUE_SIZE = 10;
RejectedHandler DEFAULT_REJECTED_HANDLER = RejectedHandler.CALLER_RUNS;
/**
* Config options to select {@link RejectedExecutionHandler}s.
*/
enum RejectedHandler {
/**
* @see CallerRunsPolicy
*/
CALLER_RUNS {
@Override
RejectedExecutionHandler getHandler() {
return new ThreadPoolExecutor.CallerRunsPolicy();
}
},
/**
* @see AbortPolicy
*/
ABORT {
@Override
RejectedExecutionHandler getHandler() {
return new ThreadPoolExecutor.AbortPolicy();
}
},
/**
* @see DiscardPolicy
*/
DISCARD_NEWEST {
@Override
RejectedExecutionHandler getHandler() {
return new ThreadPoolExecutor.DiscardPolicy();
}
},
/**
* @see DiscardOldestPolicy
*/
DISCARD_OLDEST {
@Override
RejectedExecutionHandler getHandler() {
return new ThreadPoolExecutor.DiscardOldestPolicy();
}
};
abstract RejectedExecutionHandler getHandler();
}
/**
* The minimum thread pool size. Must be {@code 0 < min < max} unless max is {@code 0}.
*/
Integer getMinThreads();
/**
* The maximum thread pool size. May be 0, in which case there is no thread pool. All
* requests would then execute directly in the calling thread, which is good for testing
* and debugging.
*/
Integer getMaxThreads();
/**
* How long a thread may remain totally idle before the pool shrinks.
*/
Duration getThreadTimeout();
/**
* The size of the {@link LinkedBlockingQueue}. May be 0, in which case there is a {@link SynchronousQueue} instead.
*/
Integer getQueueSize();
/**
* The rejected execution handler to use for the thread pool.
* @see RejectedHandler
*/
RejectedHandler getRejectedHandler();
}