本文整理汇总了Java中java.util.concurrent.ThreadPoolExecutor.setKeepAliveTime方法的典型用法代码示例。如果您正苦于以下问题:Java ThreadPoolExecutor.setKeepAliveTime方法的具体用法?Java ThreadPoolExecutor.setKeepAliveTime怎么用?Java ThreadPoolExecutor.setKeepAliveTime使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.ThreadPoolExecutor
的用法示例。
在下文中一共展示了ThreadPoolExecutor.setKeepAliveTime方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testKeepAliveTimeIllegalArgumentException
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
/**
* setKeepAliveTime throws IllegalArgumentException
* when given a negative value
*/
public void testKeepAliveTimeIllegalArgumentException() {
final ThreadPoolExecutor p =
new CustomTPE(2, 3,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
try (PoolCleaner cleaner = cleaner(p)) {
try {
p.setKeepAliveTime(-1, MILLISECONDS);
shouldThrow();
} catch (IllegalArgumentException success) {}
}
}
示例2: testKeepAliveTimeIllegalArgumentException
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
/**
* setKeepAliveTime throws IllegalArgumentException
* when given a negative value
*/
public void testKeepAliveTimeIllegalArgumentException() {
final ThreadPoolExecutor p =
new ThreadPoolExecutor(2, 3,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
try (PoolCleaner cleaner = cleaner(p)) {
try {
p.setKeepAliveTime(-1, MILLISECONDS);
shouldThrow();
} catch (IllegalArgumentException success) {}
}
}
示例3: positiveTest
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
@Test
public void positiveTest() {
// create the threads manager
this.threadsManager = new ThreadsManager();
// create all the threads
ThreadPoolExecutor executor = ( ThreadPoolExecutor ) Executors.newCachedThreadPool();
executor.setKeepAliveTime( 0, TimeUnit.SECONDS );
ExecutorCompletionService<Object> executionService = new ExecutorCompletionService<Object>( executor );
IterationListener listener = new IterationListener();
msg( log, "Ask Java to create all threads" );
for( int j = 0; j < N_THREADS; j++ ) {
executionService.submit( new TestWorker( N_ITERATIONS, threadsManager, listener ), null );
}
// run all iterations
for( int i = 0; i < N_ITERATIONS; i++ ) {
msg( log, "ITERATION " + i + "\n\n" );
runThisIteration();
waitForIterationCompletion();
}
// it may take a little while all threads are gone
try {
Thread.sleep( 1000 );
} catch( InterruptedException e ) {}
// check if there are any remaining threads started by this test
checkAllRemainingThreads();
}
示例4: initExecutionPool
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
private void initExecutionPool(int corePoolSize, int maxPoolSize, long keepAliveTime) {
executorPool = (ThreadPoolExecutor) Executors.newCachedThreadPool();
executorPool.setCorePoolSize(corePoolSize);
executorPool.setMaximumPoolSize(maxPoolSize);
executorPool.setKeepAliveTime(keepAliveTime, TimeUnit.MILLISECONDS);
}
示例5: scheduleThreads
import java.util.concurrent.ThreadPoolExecutor; //导入方法依赖的package包/类
@Override
public synchronized void
scheduleThreads( String caller,
boolean isUseSynchronizedIterations ) throws ActionExecutionException,
ActionTaskLoaderException,
NoSuchComponentException,
NoSuchActionException,
NoCompatibleMethodFoundException,
ThreadingPatternNotSupportedException {
//check the state first
if (state != ActionTaskLoaderState.NOT_STARTED) {
throw new ActionTaskLoaderException("Cannot schedule load queue " + queueName
+ " - it has already been scheduled");
}
//create the executor - terminate threads when finished
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newCachedThreadPool();
executor.setKeepAliveTime(0, TimeUnit.SECONDS);
ExecutorCompletionService<Object> executionService = new ExecutorCompletionService<Object>(executor);
//synchronization aids
threadsManagers = new ArrayList<ThreadsManager>();
// create the thread for managing max iteration length
int iterationTimeout = startPattern.getIterationTimeout();
if (iterationTimeout > 0) {
itManager = new IterationTimeoutManager(iterationTimeout);
}
taskFutures = new ArrayList<Future<Object>>();
for (int i = 0; i < numThreadGroups; i++) {
//create the thread iterations manager for this group
ThreadsManager threadsManager = new ThreadsManager();
int numThreadsInGroup = (i != numThreadGroups - 1)
? numThreadsPerStep
: numThreadsInLastGroup;
//distribute executions per timeFrame according to the number of threads
int executionsPerTimeFrame = executionPattern.getExecutionsPerTimeFrame();
int[] executionsPerTimeFramePerThread = new EvenLoadDistributingUtils().getEvenLoad(executionsPerTimeFrame,
numThreads);
if (numThreads > executionsPerTimeFrame && executionsPerTimeFrame > 0) {
throw new ActionTaskLoaderException("We cannot evenly distribute " + executionsPerTimeFrame
+ " iterations per time frame to " + numThreads
+ " threads. Iterations per time frame must be at least as many as threads!");
}
for (int j = 0; j < numThreadsInGroup; j++) {
Future<Object> taskFuture = executionService.submit(ActionTaskFactory.createTask(caller,
queueName,
executionPattern,
executionsPerTimeFramePerThread[j],
threadsManager,
itManager,
actionRequests,
parameterDataProviders,
defaultTaskListeners,
isUseSynchronizedIterations),
null);
taskFutures.add(taskFuture);
}
threadsManagers.add(threadsManager);
}
state = ActionTaskLoaderState.SCHEDULED;
}