当前位置: 首页>>代码示例>>Java>>正文


Java ThreadPoolExecutor.setKeepAliveTime方法代码示例

本文整理汇总了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) {}
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:17,代码来源:ThreadPoolExecutorSubclassTest.java

示例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) {}
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:17,代码来源:ThreadPoolExecutorTest.java

示例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();
}
 
开发者ID:Axway,项目名称:ats-framework,代码行数:35,代码来源:Test_ThreadsManager.java

示例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);

}
 
开发者ID:DSC-SPIDAL,项目名称:twister2,代码行数:8,代码来源:TaskExecutorCachedThreadPool.java

示例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;
}
 
开发者ID:Axway,项目名称:ats-framework,代码行数:72,代码来源:RampUpQueueLoader.java


注:本文中的java.util.concurrent.ThreadPoolExecutor.setKeepAliveTime方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。