本文整理汇总了Java中java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy类的典型用法代码示例。如果您正苦于以下问题:Java CallerRunsPolicy类的具体用法?Java CallerRunsPolicy怎么用?Java CallerRunsPolicy使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CallerRunsPolicy类属于java.util.concurrent.ThreadPoolExecutor包,在下文中一共展示了CallerRunsPolicy类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testStandardRejectedExecutionHandlers
import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy; //导入依赖的package包/类
/** Directly test simple ThreadPoolExecutor RejectedExecutionHandlers. */
public void testStandardRejectedExecutionHandlers() {
final ThreadPoolExecutor p =
new ThreadPoolExecutor(1, 1, 1, SECONDS,
new ArrayBlockingQueue<Runnable>(1));
final AtomicReference<Thread> thread = new AtomicReference<>();
final Runnable r = new Runnable() { public void run() {
thread.set(Thread.currentThread()); }};
try {
new AbortPolicy().rejectedExecution(r, p);
shouldThrow();
} catch (RejectedExecutionException success) {}
assertNull(thread.get());
new DiscardPolicy().rejectedExecution(r, p);
assertNull(thread.get());
new CallerRunsPolicy().rejectedExecution(r, p);
assertSame(Thread.currentThread(), thread.get());
// check that pool was not perturbed by handlers
assertTrue(p.getRejectedExecutionHandler() instanceof AbortPolicy);
assertEquals(0, p.getTaskCount());
assertTrue(p.getQueue().isEmpty());
}
示例2: Processor
import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy; //导入依赖的package包/类
public Processor(ProcessorConfig config) {
ProfileCredentialsProvider creds = new ProfileCredentialsProvider(config.profile());
creds.getCredentials(); // credible credential criteria
if (config.disableCerts())
System.setProperty("com.amazonaws.sdk.disableCertChecking", "true");
// Rekognition init
rek = new AmazonRekognitionClient(creds);
if (config.endpointOverride())
rek.setEndpoint(config.endpoint());
minConfidence = Integer.parseInt(config.confidence());
// The SQS queue to find jobs on
sqs = new AmazonSQSClient(creds);
queueUrl = sqs.createQueue(config.queue()).getQueueUrl();
// Processors
if (config.wantCloudSearch())
processors.add(new CloudSearchIndexer(creds, config.cloudSearch()));
if (config.wantDynamo())
processors.add(new DynamoWriter(creds, config.dynamo()));
if (config.wantTags3())
processors.add(new S3ObjectTagger(creds, config.tagPrefix()));
// Executor Service
int maxWorkers = Integer.parseInt(config.concurrency());
executor = new ThreadPoolExecutor(
1, maxWorkers, 30, TimeUnit.SECONDS,
new ArrayBlockingQueue<>(maxWorkers * 2, false),
new CallerRunsPolicy() // prevents backing up too many jobs
);
maxImagesToProcess = Long.parseLong(config.max());
}
示例3: ThroughputPipe
import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy; //导入依赖的package包/类
/**
* 流量管道(流量控制器)
*
* @param pipeSize
* --管道大小
* @param poolSize
* --流量消费线程池大小
* @param handler
* --消费接口实现
* @param stepOfDispatcher
* --流量消费指派速度参数,单位ms毫秒。例如,每秒消费1个请求,则值为1000ms<br>
* 特别地,值为0时,指派速度没有限制,管道流量消费速度只依赖于线程池的大小及handler计算速度。
*/
public ThroughputPipe(int pipeSize, int poolSize,
IConsumeHandler<T> handler, long stepOfDispatcher) {
if (pipeSize <= 0 || poolSize <= 0) {
throw new RuntimeException("size's value must >0!");
}
if (null == handler) {
throw new RuntimeException("handle can't be null!");
}
this.queue = new LinkedBlockingQueue<T>(pipeSize);
this.handler = handler;
tpe = new ThreadPoolExecutor(poolSize, poolSize, livetime,
TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>(),
new CallerRunsPolicy());
this.stepOfDispatcher = stepOfDispatcher;
}
示例4: initWorkbench
import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy; //导入依赖的package包/类
/**
* @throws java.io.IOException
*/
private void initWorkbench(String configFile) throws IOException {
if (initialized) {
return;
}
this.stressStrategy = getStressStrategy(configFile);
this.config = stressStrategy.getStressConfig();
if (this.config == null) {
throw new NullPointerException("未配置压力测试参数!");
}
servicePool = new ThreadPoolExecutor(config.getThreadNum(), config.getThreadNum(), 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(2000), new CallerRunsPolicy());
finish = new CountDownLatch(config.getThreadNum());
scheduledThreadPool = Executors.newScheduledThreadPool(1);
scheduledThreadPool.scheduleAtFixedRate(new Runnable() {
public void run() {
statistic();
}
}, config.getStatPeriod(), config.getStatPeriod(), TimeUnit.MILLISECONDS);
if (!"".equals(config.getOutputFileName())) {
resultWriter = new FileWriter(config.getOutputFileName());
}
this.initialized = true;// 初始化完成
}
示例5: doStart
import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy; //导入依赖的package包/类
/**
* Move from direct to asynchronous subscriber processing.
*/
@Override
protected void doStart() throws Exception {
// direct hand-off used! Host pool will use caller thread to execute async subscribers when pool full!
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(
0,
HOST_THREAD_POOL_SIZE,
60L,
TimeUnit.SECONDS,
new SynchronousQueue<>(),
new NexusThreadFactory("event", "event-manager"),
new CallerRunsPolicy()
);
// begin distributing events in truly asynchronous fashion
delegate = NexusExecutorService.forCurrentSubject(threadPool);
}
示例6: getFixedPool
import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy; //导入依赖的package包/类
public static ExecutorService getFixedPool(String poolName, Integer min, Integer max, Integer queueSize,
Long aliveTimeInsecond) {
if (threadPoolHolder == null) {
synchronized (ThreadPoolHolder.class) {
if (threadPoolHolder == null) {
threadPoolHolder = new ThreadPoolHolder();
map = Maps.newHashMap();
lock = new ReentrantLock();
}
}
}
ExecutorService service = map.get(poolName);
if (service == null) {
lock.lock();
if (service == null) {
try {
int min2 = Runtime.getRuntime().availableProcessors();
int max2 = min2 * 2 + 2;
if (min == null || min < min2) {
min = min2;
}
if (max == null || max < max2) {
max = max2;
}
queueSize = queueSize == null ? 100 : queueSize;
aliveTimeInsecond = aliveTimeInsecond == null ? 100 : aliveTimeInsecond;
ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat(poolName + "-%d").build();
service = new ThreadPoolExecutor(min, max, aliveTimeInsecond, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(queueSize), threadFactory, new CallerRunsPolicy());
map.put(poolName, service);
} finally {
lock.unlock();
}
}
}
return service;
}
示例7: getFixedPool
import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy; //导入依赖的package包/类
public static ExecutorService getFixedPool(String poolName, Integer min, Integer max) {
if (threadPoolHolder == null) {
synchronized (ThreadPoolHolder.class) {
if (threadPoolHolder == null) {
threadPoolHolder = new ThreadPoolHolder();
map = Maps.newHashMap();
lock = new ReentrantLock();
}
}
}
ExecutorService service = map.get(poolName);
if (service == null) {
lock.lock();
if (service == null) {
try {
int min2 = Runtime.getRuntime().availableProcessors();
int max2 = min2 * 2 + 1;
if (min == null || min < min2) {
min = min2;
}
if (max == null || max < max2) {
max = max2;
}
ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat(poolName + "-%d").build();
service = new ThreadPoolExecutor(min, max, 100, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(100), threadFactory, new CallerRunsPolicy());
map.put(poolName, service);
} finally {
lock.unlock();
}
}
}
return service;
}
示例8: ServiceServer
import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy; //导入依赖的package包/类
public ServiceServer(int port) {
this.port = port;
this.channelGroup = new DefaultChannelGroup();
bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(new NamedThreadFactory(
"ServiceServer-bossExecutor-", false)),
Executors.newCachedThreadPool(new NamedThreadFactory(
"ServiceServer-workerExecutor-", true))));
bootstrap.setOption("tcpNoDelay", Boolean.parseBoolean(AppProperties
.get("rpc_server_tcpNoDelay", "true")));
bootstrap.setOption("reuseAddress", Boolean.parseBoolean(AppProperties
.get("rpc_server_reuseAddress", "true")));
String c1 = AppProperties.get("rpc_server_child_tcpNoDelay");
if (c1 != null && c1.trim().length() > 0) {
bootstrap.setOption("child.tcpNoDelay", Boolean.parseBoolean(c1));
}
c1 = AppProperties.get("rpc_server_child_receiveBufferSize");
if (c1 != null && c1.trim().length() > 0) {
bootstrap
.setOption("child.receiveBufferSize", Integer.parseInt(c1));
}
this.taskThreadPool = new TaskThreadPool(AppProperties.getAsInt(
"rpc_server_workThreadPool_coreSize", 50),
AppProperties
.getAsInt("rpc_server_workThreadPool_MaxSize", 200),
AppProperties.getAsInt(
"rpc_server_workThreadPool_keepAliveTime",
60 * 1000 * 5), true, new CallerRunsPolicy());
}
示例9: AsynchroCallImp
import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy; //导入依赖的package包/类
private AsynchroCallImp() {
String size = AppProperties.get("command_asynchro_call_queue_size");
if (size == null || size.length() == 0) {
size = "10";
}
int size2 = Integer.parseInt(size.trim());
wtp = new TaskThreadPool(size2, size2, 5 * 1000 * 60, true,
new CallerRunsPolicy());
cmdQueue = new BlockQueue();
consumer = new Consumer("AsynchroCallConsumer");
consumer.start();
}
示例10: Publisher
import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy; //导入依赖的package包/类
private Publisher(int count, Map <String, SubWorker> docSubWorkerMap) {
this.pubWorkerAmount = 0;
this.docQueue = new BlockQueue();
this.docSubWorkerMap = docSubWorkerMap;
this.re = new TaskExecutor(new TaskThreadPool(count, count,
5 * 1000 * 60, false, new CallerRunsPolicy()));
}
示例11: Subscriber
import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy; //导入依赖的package包/类
private Subscriber(int count, IQueue publisherDocqueue, Map<String, SubWorker> docSubWorkerMap) {
this.concurrentCounter = new Counter(0);
this.docSubWorkerMap = docSubWorkerMap;
this.wkpool = new TaskThreadPool(count, count, 5 * 1000 * 60, false,
new CallerRunsPolicy());
dp = new Dispatcher(this.wkpool, this.concurrentCounter,
publisherDocqueue, this.docSubWorkerMap);
}
示例12: uiExecutor
import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy; //导入依赖的package包/类
/**
* @return the executor for UI background processes.
*/
@Bean(name = "uiExecutor")
@ConditionalOnMissingBean(name = "uiExecutor")
public Executor uiExecutor() {
final BlockingQueue<Runnable> blockingQueue = new ArrayBlockingQueue<>(20);
final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 20, 10000, TimeUnit.MILLISECONDS,
blockingQueue, new ThreadFactoryBuilder().setNameFormat("ui-executor-pool-%d").build());
threadPoolExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
return new DelegatingSecurityContextExecutor(threadPoolExecutor);
}
示例13: EventBus
import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy; //导入依赖的package包/类
public EventBus(Context context)
{
myContext = context;
myEventDispatcherFactory = new EventDispatcherFactoryImpl();
mySubscriptionFactory = new SubscriptionFactoryImpl();
myBookkeeper = new SubscriptionBookkeeperImpl();
myWorkerPool = new ThreadPoolExecutor(4, 4, 0L, SECONDS, new ArrayBlockingQueue<Runnable>(256), new EventBusThreadFactory(), new CallerRunsPolicy());
}
示例14: createServiceExecutor
import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy; //导入依赖的package包/类
/**
* Creates and configures a new {@link ScheduledExecutorService} with a
* timeout value of {@code seconds}. If the timeout value is below or equal
* to zero then the returned executor will never timeout.
*
* @return the newly created and configured executor service.
*/
private static ScheduledExecutorService createServiceExecutor(long seconds) {
Preconditions.checkArgument(seconds >= 0, "The timeout value must be equal to or greater than 0!");
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
executor.setRejectedExecutionHandler(new CallerRunsPolicy());
executor.setThreadFactory(new ThreadFactoryBuilder().setNameFormat("ServiceQueueThread").build());
if (seconds > 0) {
executor.setKeepAliveTime(seconds, TimeUnit.SECONDS);
executor.allowCoreThreadTimeOut(true);
}
return executor;
}
示例15: newCachedThreadPool
import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy; //导入依赖的package包/类
public static ExecutorService newCachedThreadPool(final int nbThreads, final String name) {
// Note: we don't use the default rejection handler here (AbortPolicy) as we always want the tasks to be executed
return Executors.newCachedThreadPool(0,
nbThreads,
name,
60L,
TimeUnit.SECONDS,
new CallerRunsPolicy());
}