本文整理汇总了Java中java.util.concurrent.ThreadPoolExecutor类的典型用法代码示例。如果您正苦于以下问题:Java ThreadPoolExecutor类的具体用法?Java ThreadPoolExecutor怎么用?Java ThreadPoolExecutor使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ThreadPoolExecutor类属于java.util.concurrent包,在下文中一共展示了ThreadPoolExecutor类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doGet
import java.util.concurrent.ThreadPoolExecutor; //导入依赖的package包/类
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.getWriter().println(
"The current thread served " + this + " servlet");
tpe = new ThreadPoolExecutor(tpSize, tpSize, 50000L,
TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
Task[] tasks = new Task[nTasks];
for (int i = 0; i < nTasks; i++) {
tasks[i] = new Task("Task " + i);
tpe.execute(tasks[i]);
}
resp.getWriter().println("Started " + nTasks +
" never ending tasks using the ThreadPoolExecutor");
resp.getWriter().flush();
}
开发者ID:sunmingshuai,项目名称:apache-tomcat-7.0.73-with-comment,代码行数:19,代码来源:TestWebappClassLoaderExecutorMemoryLeak.java
示例2: testInterruptedSubmit
import java.util.concurrent.ThreadPoolExecutor; //导入依赖的package包/类
/**
* submit(callable).get() throws InterruptedException if interrupted
*/
public void testInterruptedSubmit() throws InterruptedException {
final CountDownLatch submitted = new CountDownLatch(1);
final CountDownLatch quittingTime = new CountDownLatch(1);
final Callable<Void> awaiter = new CheckedCallable<Void>() {
public Void realCall() throws InterruptedException {
assertTrue(quittingTime.await(2*LONG_DELAY_MS, MILLISECONDS));
return null;
}};
final ExecutorService p
= new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(10));
try (PoolCleaner cleaner = cleaner(p, quittingTime)) {
Thread t = newStartedThread(new CheckedInterruptedRunnable() {
public void realRun() throws Exception {
Future<Void> future = p.submit(awaiter);
submitted.countDown();
future.get();
}});
await(submitted);
t.interrupt();
awaitTermination(t);
}
}
示例3: newExecutorService
import java.util.concurrent.ThreadPoolExecutor; //导入依赖的package包/类
private static ExecutorService newExecutorService(
ThreadFactory threadFactory, String threadName) {
boolean background = threadFactory instanceof GaeThreadFactory
&& ((GaeThreadFactory) threadFactory).isUsingBackgroundThreads();
if (background) {
// Create a thread pool with long-lived threads if background thread support is available.
return new RevivingScheduledExecutor(threadFactory, threadName, true);
} else {
// Create an executor that creates a new thread for each submitted task, when background
// thread support is not available.
return new ThreadPoolExecutor(
0,
Integer.MAX_VALUE,
0L,
TimeUnit.SECONDS,
new SynchronousQueue<Runnable>(),
threadFactory);
}
}
示例4: createExecutor
import java.util.concurrent.ThreadPoolExecutor; //导入依赖的package包/类
private ThreadPoolExecutor createExecutor() {
return new ThreadPoolExecutor(
Config.concurrentThreadNum(),
Config.concurrentThreadNum(),
0L,
TimeUnit.SECONDS,
new LinkedBlockingQueue<>(),
defaultFactory,
(r, executor) -> {
if (r instanceof TaskRunner) {
TaskRunner task = (TaskRunner) r;
onReject(task.getCmd());
LOGGER.warn("Reject cmd: %s", task.getCmd());
}
});
}
示例5: CsapEventClient
import java.util.concurrent.ThreadPoolExecutor; //导入依赖的package包/类
public CsapEventClient( ) {
BasicThreadFactory eventThreadFactory = new BasicThreadFactory.Builder()
.namingPattern( "CsapEventPost-%d" )
.daemon( true )
.priority( Thread.NORM_PRIORITY + 1 )
.build();
eventPostQueue = new ArrayBlockingQueue<>( MAX_EVENT_BACKLOG );
// Use a single thread to sequence and post
// eventPostPool = Executors.newFixedThreadPool(1, schedFactory, queue);
// really only needs to be 1 - adding the others for lt scenario
eventPostPool = new ThreadPoolExecutor( 1, 1,
30, TimeUnit.SECONDS,
eventPostQueue, eventThreadFactory );
eventPostCompletionService = new ExecutorCompletionService<String>(
eventPostPool );
}
示例6: getThreadPoolSummary
import java.util.concurrent.ThreadPoolExecutor; //导入依赖的package包/类
protected String getThreadPoolSummary(ThreadPool threadPool) {
StringBuffer sb = new StringBuffer(512);
sb.append(threadPool.getName());
if (threadPool.getExecutor() instanceof ThreadPoolExecutor) {
sb.append(" (");
ThreadPoolExecutor executor = (ThreadPoolExecutor) threadPool.getExecutor();
sb.append("max=" + executor.getMaximumPoolSize());
sb.append(", current=" + executor.getPoolSize());
sb.append(", active=" + executor.getActiveCount());
sb.append(", largest=" + executor.getLargestPoolSize());
sb.append(", core=" + executor.getCorePoolSize());
sb.append(", all tasks=" + executor.getTaskCount());
sb.append(", completed tasks=" + executor.getCompletedTaskCount());
sb.append(", queue size=" + executor.getQueue().size());
sb.append(", queue remaining capacity=" + executor.getQueue().remainingCapacity());
sb.append(")");
}
return sb.toString();
}
示例7: testInvokeAll3
import java.util.concurrent.ThreadPoolExecutor; //导入依赖的package包/类
/**
* invokeAll(c) throws NPE if c has null elements
*/
public void testInvokeAll3() throws Exception {
final ExecutorService e =
new ThreadPoolExecutor(2, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
try (PoolCleaner cleaner = cleaner(e)) {
List<Callable<String>> l = new ArrayList<>();
l.add(new StringTask());
l.add(null);
try {
e.invokeAll(l);
shouldThrow();
} catch (NullPointerException success) {}
}
}
示例8: testGetActiveCount
import java.util.concurrent.ThreadPoolExecutor; //导入依赖的package包/类
/**
* getActiveCount increases but doesn't overestimate, when a
* thread becomes active
*/
public void testGetActiveCount() throws InterruptedException {
final CountDownLatch done = new CountDownLatch(1);
final ThreadPoolExecutor p =
new ThreadPoolExecutor(2, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
try (PoolCleaner cleaner = cleaner(p, done)) {
final CountDownLatch threadStarted = new CountDownLatch(1);
assertEquals(0, p.getActiveCount());
p.execute(new CheckedRunnable() {
public void realRun() throws InterruptedException {
threadStarted.countDown();
assertEquals(1, p.getActiveCount());
await(done);
}});
await(threadStarted);
assertEquals(1, p.getActiveCount());
}
}
示例9: initDictionaryCaches
import java.util.concurrent.ThreadPoolExecutor; //导入依赖的package包/类
private void initDictionaryCaches(DictionaryDAOImpl dictionaryDAO, TenantService tenantService)
{
CompiledModelsCache compiledModelsCache = new CompiledModelsCache();
compiledModelsCache.setDictionaryDAO(dictionaryDAO);
compiledModelsCache.setTenantService(tenantService);
compiledModelsCache.setRegistry(new DefaultAsynchronouslyRefreshedCacheRegistry());
TraceableThreadFactory threadFactory = new TraceableThreadFactory();
threadFactory.setThreadDaemon(true);
threadFactory.setThreadPriority(Thread.NORM_PRIORITY);
ThreadPoolExecutor threadPoolExecutor = new DynamicallySizedThreadPoolExecutor(20, 20, 90, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), threadFactory,
new ThreadPoolExecutor.CallerRunsPolicy());
compiledModelsCache.setThreadPoolExecutor(threadPoolExecutor);
dictionaryDAO.setDictionaryRegistryCache(compiledModelsCache);
dictionaryDAO.init();
}
示例10: testActiveThreadsCount
import java.util.concurrent.ThreadPoolExecutor; //导入依赖的package包/类
/**
* This is for testing the active number of threads that were used while
* doing a batch operation. It inserts one row per region via the batch
* operation, and then checks the number of active threads.
* For HBASE-3553
* @throws IOException
* @throws InterruptedException
* @throws NoSuchFieldException
* @throws SecurityException
*/
@Ignore ("Nice bug flakey... expected 5 but was 4..") @Test(timeout=300000)
public void testActiveThreadsCount() throws Exception {
try (Connection connection = ConnectionFactory.createConnection(UTIL.getConfiguration())) {
ThreadPoolExecutor executor = HTable.getDefaultExecutor(UTIL.getConfiguration());
try {
try (Table t = connection.getTable(TEST_TABLE, executor)) {
List<Put> puts = constructPutRequests(); // creates a Put for every region
t.batch(puts);
HashSet<ServerName> regionservers = new HashSet<ServerName>();
try (RegionLocator locator = connection.getRegionLocator(TEST_TABLE)) {
for (Row r : puts) {
HRegionLocation location = locator.getRegionLocation(r.getRow());
regionservers.add(location.getServerName());
}
}
assertEquals(regionservers.size(), executor.getLargestPoolSize());
}
} finally {
executor.shutdownNow();
}
}
}
示例11: testPrestartCoreThread
import java.util.concurrent.ThreadPoolExecutor; //导入依赖的package包/类
/**
* prestartCoreThread starts a thread if under corePoolSize, else doesn't
*/
public void testPrestartCoreThread() {
final ThreadPoolExecutor p =
new ThreadPoolExecutor(2, 6,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
try (PoolCleaner cleaner = cleaner(p)) {
assertEquals(0, p.getPoolSize());
assertTrue(p.prestartCoreThread());
assertEquals(1, p.getPoolSize());
assertTrue(p.prestartCoreThread());
assertEquals(2, p.getPoolSize());
assertFalse(p.prestartCoreThread());
assertEquals(2, p.getPoolSize());
p.setCorePoolSize(4);
assertTrue(p.prestartCoreThread());
assertEquals(3, p.getPoolSize());
assertTrue(p.prestartCoreThread());
assertEquals(4, p.getPoolSize());
assertFalse(p.prestartCoreThread());
assertEquals(4, p.getPoolSize());
}
}
示例12: testInvokeAll4
import java.util.concurrent.ThreadPoolExecutor; //导入依赖的package包/类
/**
* get of element of invokeAll(c) throws exception on failed task
*/
public void testInvokeAll4() throws Exception {
final ExecutorService e =
new ThreadPoolExecutor(2, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
try (PoolCleaner cleaner = cleaner(e)) {
List<Callable<String>> l = new ArrayList<>();
l.add(new NPETask());
List<Future<String>> futures = e.invokeAll(l);
assertEquals(1, futures.size());
try {
futures.get(0).get();
shouldThrow();
} catch (ExecutionException success) {
assertTrue(success.getCause() instanceof NullPointerException);
}
}
}
示例13: serviceInit
import java.util.concurrent.ThreadPoolExecutor; //导入依赖的package包/类
@Override
protected void serviceInit(Configuration conf) throws Exception {
int threadCount = conf.getInt(
YarnConfiguration.RM_AMLAUNCHER_THREAD_COUNT,
YarnConfiguration.DEFAULT_RM_AMLAUNCHER_THREAD_COUNT);
ThreadFactory tf = new ThreadFactoryBuilder()
.setNameFormat("ApplicationMasterLauncher #%d")
.build();
launcherPool = new ThreadPoolExecutor(threadCount, threadCount, 1,
TimeUnit.HOURS, new LinkedBlockingQueue<Runnable>());
launcherPool.setThreadFactory(tf);
Configuration newConf = new YarnConfiguration(conf);
newConf.setInt(CommonConfigurationKeysPublic.
IPC_CLIENT_CONNECT_MAX_RETRIES_ON_SOCKET_TIMEOUTS_KEY,
conf.getInt(YarnConfiguration.RM_NODEMANAGER_CONNECT_RETIRES,
YarnConfiguration.DEFAULT_RM_NODEMANAGER_CONNECT_RETIRES));
setConfig(newConf);
super.serviceInit(newConf);
}
示例14: multiThreadUpload
import java.util.concurrent.ThreadPoolExecutor; //导入依赖的package包/类
private ThreadPoolExecutor multiThreadUpload(int threadNum, final int threadFileNum) {
ThreadPoolExecutor pool = (ThreadPoolExecutor) Executors.newFixedThreadPool(threadNum);
pool.prestartAllCoreThreads();
for (int i = 0; i < threadNum; ++i) {
final int threadId = i;
pool.submit(new Runnable() {
@Override
public void run() {
uploadAndDownloadPerform(threadId, threadFileNum);
}
});
}
pool.shutdown();
return pool;
}
示例15: setUp
import java.util.concurrent.ThreadPoolExecutor; //导入依赖的package包/类
@Setup(Level.Trial)
@Override
public void setUp() throws Exception {
ListeningExecutorService dsExec = MoreExecutors.newDirectExecutorService();
executor = MoreExecutors.listeningDecorator(
MoreExecutors.getExitingExecutorService((ThreadPoolExecutor) Executors.newFixedThreadPool(1), 1L,
TimeUnit.SECONDS));
InMemoryDOMDataStore operStore = new InMemoryDOMDataStore("OPER", dsExec);
InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("CFG", dsExec);
Map<LogicalDatastoreType, DOMStore> datastores = ImmutableMap.of(
LogicalDatastoreType.OPERATIONAL, (DOMStore)operStore,
LogicalDatastoreType.CONFIGURATION, configStore);
domBroker = new SerializedDOMDataBroker(datastores, executor);
schemaContext = BenchmarkModel.createTestContext();
configStore.onGlobalContextUpdated(schemaContext);
operStore.onGlobalContextUpdated(schemaContext);
initTestNode();
}