java.util.concurrent.ThreadPoolExecutor Java Examples
The following examples show how to use
java.util.concurrent.ThreadPoolExecutor.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: QueueServiceImpl.java From pmq with Apache License 2.0 | 6 votes |
@Override public void start() { if (startFlag.compareAndSet(false, true)) { updateCache(); executor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(50), SoaThreadFactory.create("QueueService", true), new ThreadPoolExecutor.DiscardOldestPolicy()); executor.execute(() -> { // 因为第一次的时候,会由topic和dbnode 触发初始化,所以自身初始化可以减少一次 checkChanged(); while (isRunning) { try { updateCache(); lastUpdateTime = System.currentTimeMillis(); } catch (Exception e) { log.error("QueueServiceImpl_initCache_error", e); } Util.sleep(soaConfig.getMqQueueCacheInterval()); } }); } }
Example #2
Source File: BaseRuleFactoryTest.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Tests that a given rule rule will be computed and added once to the cache * whatever the number of times this rule is called concurrently. */ @Test public void testConcurrentCreation() throws InterruptedException, ExecutionException { // Number of times the same rule will be called. final int numTasks = 20; final ThreadPoolExecutor exec = new ThreadPoolExecutor(3, numTasks, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(2)); final List<Future<Pair<double[], double[]>>> results = new ArrayList<Future<Pair<double[], double[]>>>(); for (int i = 0; i < numTasks; i++) { results.add(exec.submit(new RuleBuilder())); } // Ensure that all computations have completed. for (Future<Pair<double[], double[]>> f : results) { f.get(); } // Assertion would fail if "getRuleInternal" were not "synchronized". final int n = RuleBuilder.getNumberOfCalls(); Assert.assertEquals("Rule computation was called " + n + " times", 1, n); }
Example #3
Source File: SocketService.java From hack-root with MIT License | 6 votes |
public SocketService(SocketListener listener) { this.listener = listener; try { ServerSocket serverSocket = new ServerSocket(PORT); System.out.println("server running " + PORT + " port"); ArrayBlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(10); ThreadPoolExecutor executor = new ThreadPoolExecutor( 5, 10, 5000, TimeUnit.MILLISECONDS, queue ); while (true) { Socket socket = serverSocket.accept(); executor.execute(new processMsg(socket)); } } catch (Exception e) { System.out.println("SocketServer create Exception:" + e); } }
Example #4
Source File: ThreadPoolTest.java From java-study with Apache License 2.0 | 6 votes |
/** * ThreadPoolExecutor线程池 */ private static void threadPoolExecutor() { int corePoolSize=5; int maximumPoolSize=10; long keepAliveTime=2L; // 线程核心数,最大线程数,线程缓存时间,时间格式,缓存队列 ,线程工厂,拒绝策略 ThreadPoolExecutor tpx=new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(3), Executors.defaultThreadFactory(),new ThreadPoolExecutor.DiscardOldestPolicy()); for (int i = 1; i <= 10; i++) { try { // 产生一个任务,并将其加入到线程池 String task = "task@ " + i; // System.out.println("put " + task); tpx.execute(new MyThread(task)); // 便于观察,等待一段时间 Thread.sleep(20); } catch (Exception e) { e.printStackTrace(); } } }
Example #5
Source File: ElasticSearchRefresh.java From metacat with Apache License 2.0 | 6 votes |
private static ExecutorService newFixedThreadPool( final int nThreads, final String threadFactoryName, final int queueSize ) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(queueSize), new ThreadFactoryBuilder() .setNameFormat(threadFactoryName) .build(), (r, executor) -> { // this will block if the queue is full try { executor.getQueue().put(r); } catch (InterruptedException e) { throw Throwables.propagate(e); } }); }
Example #6
Source File: JavaCronetEngine.java From cronet with BSD 3-Clause "New" or "Revised" License | 6 votes |
public JavaCronetEngine(CronetEngineBuilderImpl builder) { // On android, all background threads (and all threads that are part // of background processes) are put in a cgroup that is allowed to // consume up to 5% of CPU - these worker threads spend the vast // majority of their time waiting on I/O, so making them contend with // background applications for a slice of CPU doesn't make much sense. // We want to hurry up and get idle. final int threadPriority = builder.threadPriority(THREAD_PRIORITY_BACKGROUND + THREAD_PRIORITY_MORE_FAVORABLE); this.mUserAgent = builder.getUserAgent(); this.mExecutorService = new ThreadPoolExecutor(10, 20, 50, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new ThreadFactory() { @Override public Thread newThread(final Runnable r) { return Executors.defaultThreadFactory().newThread(new Runnable() { @Override public void run() { Thread.currentThread().setName("JavaCronetEngine"); android.os.Process.setThreadPriority(threadPriority); r.run(); } }); } }); }
Example #7
Source File: Session.java From EggCrack with GNU General Public License v2.0 | 6 votes |
public Session(ThreadPoolExecutor executorService, AuthenticationService authenticationService, ExtendedList<Account> accountList, ExtendedList<Credential> credentialList, ExtendedList<Proxy> proxyList, ExtendedList<Objective> objectiveList, ExtendedList<AccountOutput> outputList, Tracker tracker, URL checkUrl, int proxyTimeout) { this.executorService = executorService; this.authenticationService = authenticationService; this.accountList = accountList; this.credentialList = credentialList; this.proxyList = proxyList; this.objectiveList = objectiveList; this.outputList = outputList; this.tracker = tracker; this.tracker.setTotal(accountList.size()); this.checkUrl = checkUrl; this.proxyTimeout = proxyTimeout; }
Example #8
Source File: Util.java From canal with Apache License 2.0 | 6 votes |
public static ThreadPoolExecutor newSingleDaemonThreadExecutor(long keepAliveTime) { return new ThreadPoolExecutor(1, 1, keepAliveTime, TimeUnit.MILLISECONDS, new SynchronousQueue<>(), DaemonThreadFactory.daemonThreadFactory, (r, exe) -> { if (!exe.isShutdown()) { try { exe.getQueue().put(r); } catch (InterruptedException e) { // ignore } } }); }
Example #9
Source File: BaseScheduler.java From nano-framework with Apache License 2.0 | 6 votes |
/** * 逻辑调用结束后处理阶段 */ private void finallyProcess() { final ThreadPoolExecutor service = config.getService(); if (service == null) { throw new SchedulerException("ThreadPoolExecutor不能为空"); } if (!close && !service.isShutdown()) { final long interval = delay(); final int runNumberOfTimes = config.getRunNumberOfTimes(); if (runNumberOfTimes == 0) { thisWait(interval); } else { nowTimes++; if (nowTimes < runNumberOfTimes) { thisWait(interval); } else { close = true; nowTimes = 0; } } } }
Example #10
Source File: StatsItemSetTest.java From rocketmq with Apache License 2.0 | 6 votes |
private AtomicLong test_unit() throws InterruptedException { final StatsItemSet statsItemSet = new StatsItemSet("topicTest", scheduler, null); executor = new ThreadPoolExecutor(10, 20, 10, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(100), new ThreadFactoryImpl("testMultiThread")); for (int i = 0; i < 10; i++) { executor.submit(new Runnable() { @Override public void run() { statsItemSet.addValue("topicTest", 2, 1); } }); } while (true) { if (executor.getCompletedTaskCount() == 10) { break; } Thread.sleep(1000); } return statsItemSet.getStatsItem("topicTest").getValue(); }
Example #11
Source File: ExecutorConfig.java From youkefu with Apache License 2.0 | 6 votes |
/** * 作业平台使用的线程池 * @return */ @Bean(name = "uKeFuTaskExecutor") public ThreadPoolTaskExecutor taskExecutor() { ThreadPoolTaskExecutor poolTaskExecutor = new ThreadPoolTaskExecutor(); // 线程池维护线程的最少数量 poolTaskExecutor.setCorePoolSize(CORE_POOL_SIZE); // 线程池维护线程的最大数量 poolTaskExecutor.setMaxPoolSize(MAX_POOL_SIZE); // 线程池所使用的缓冲队列 poolTaskExecutor.setQueueCapacity(200); // 线程池维护线程所允许的空闲时间 poolTaskExecutor.setKeepAliveSeconds(30); poolTaskExecutor.setWaitForTasksToCompleteOnShutdown(true); poolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); return poolTaskExecutor; }
Example #12
Source File: RocksDBSegmentLogStorage.java From sofa-jraft with Apache License 2.0 | 6 votes |
public RocksDBSegmentLogStorage(final String path, final RaftOptions raftOptions, final int valueSizeThreshold, final int maxSegmentFileSize, final int preAllocateSegmentCount, final int keepInMemorySegmentCount, final int checkpointIntervalMs, final ThreadPoolExecutor writeExecutor) { super(path, raftOptions); if (Platform.isMac()) { LOG.warn("RocksDBSegmentLogStorage is not recommended on mac os x, it's performance is poorer than RocksDBLogStorage."); } Requires.requireTrue(maxSegmentFileSize > 0, "maxSegmentFileSize is not greater than zero"); Requires.requireTrue(preAllocateSegmentCount > 0, "preAllocateSegmentCount is not greater than zero"); Requires.requireTrue(checkpointIntervalMs > 0, "checkpointIntervalMs is not greater than zero"); Requires.requireTrue(keepInMemorySegmentCount > 0, "keepInMemorySegmentCount is not greater than zero"); this.segmentsPath = path + File.separator + "segments"; this.abortFile = new AbortFile(this.segmentsPath + File.separator + "abort"); this.checkpointFile = new CheckpointFile(this.segmentsPath + File.separator + "checkpoint"); this.valueSizeThreshold = valueSizeThreshold; this.maxSegmentFileSize = maxSegmentFileSize; this.writeExecutor = writeExecutor == null ? createDefaultWriteExecutor() : writeExecutor; this.preAllocateSegmentCount = preAllocateSegmentCount; this.checkpointIntervalMs = checkpointIntervalMs; this.keepInMemorySegmentCount = keepInMemorySegmentCount; }
Example #13
Source File: MultiNodeTask.java From heisenberg with Apache License 2.0 | 6 votes |
public void execute() { ThreadPoolExecutor exec = ss.getSource().getProcessor().getExecutor(); for (final RouteResultsetNode rrn : nodes) { final Channel c = ss.getTarget().get(rrn); if (c != null && !c.isRunning() && !c.isClosed()) { c.setRunning(true); exec.execute(new Runnable() { @Override public void run() { execute0(rrn, c, autocommit, ss, flag, sql, exeTime); } }); } else { newExecute(rrn, autocommit, ss, flag, sql, exeTime); } } }
Example #14
Source File: ThreadFNSPool.java From fabric-net-server with Apache License 2.0 | 5 votes |
/** * 获取线程池中完成的任务数 * * @return */ public long getFixedThreadCompleteCount() { if (null != mCacheThreadPool) { return ((ThreadPoolExecutor) mCacheThreadPool).getCompletedTaskCount(); } else { return 0; } }
Example #15
Source File: FlakyThreadFactory.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
void test(final Class<?> exceptionClass, final ThreadFactory failingThreadFactory) throws Throwable { ThreadFactory flakyThreadFactory = new ThreadFactory() { int seq = 0; public Thread newThread(Runnable r) { if (seq++ < 4) return new Thread(r); else return failingThreadFactory.newThread(r); }}; ThreadPoolExecutor pool = new ThreadPoolExecutor(10, 10, 0L, TimeUnit.SECONDS, new LinkedBlockingQueue(), flakyThreadFactory); try { for (int i = 0; i < 8; i++) pool.submit(new Runnable() { public void run() {} }); check(exceptionClass == null); } catch (Throwable t) { /* t.printStackTrace(); */ check(exceptionClass.isInstance(t)); } pool.shutdown(); check(pool.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); }
Example #16
Source File: AsyncDiskService.java From big-c with Apache License 2.0 | 5 votes |
/** * Shut down all ThreadPools immediately. */ public synchronized List<Runnable> shutdownNow() { LOG.info("Shutting down all AsyncDiskService threads immediately..."); List<Runnable> list = new ArrayList<Runnable>(); for (Map.Entry<String, ThreadPoolExecutor> e : executors.entrySet()) { list.addAll(e.getValue().shutdownNow()); } return list; }
Example #17
Source File: ThreadPoolFactory.java From Thunder with Apache License 2.0 | 5 votes |
public static ThreadPoolExecutor createThreadPoolDefaultExecutor(String url, String interfaze) { return createThreadPoolExecutor(url, interfaze, ThunderConstant.CPUS * 1, ThunderConstant.CPUS * 2, 15 * 60 * 1000, false); }
Example #18
Source File: ThreadPoolManager.java From CodeDefenders with GNU Lesser General Public License v3.0 | 5 votes |
private void destroy(final ThreadPoolExecutor executor) { executor.shutdown(); if (shutdownTime > 0) { try { executor.awaitTermination(shutdownTime, shutdownTimeUnit); } catch (final InterruptedException e) { Thread.interrupted(); } } }
Example #19
Source File: ThreadPoolGovernorTest.java From sofa-common-tools with Apache License 2.0 | 5 votes |
@Test public void testEmptyThreadPoolName() { ThreadPoolGovernor.registerThreadPoolExecutor("", new ThreadPoolExecutor(1, 1, 4, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10))); ThreadPoolGovernor.registerThreadPoolExecutor(null, new ThreadPoolExecutor(1, 1, 4, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10))); Assert.assertEquals(1, infoListAppender.list.size()); Assert.assertTrue(isMatch(lastWarnString(), ERROR, "Rejected registering request of instance .+")); Assert.assertEquals(2, aberrantListAppender.list.size()); }
Example #20
Source File: ThreadPoolExecutorTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * invokeAny(c) returns result of some task */ public void testInvokeAny5() 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(new StringTask()); String result = e.invokeAny(l); assertSame(TEST_STRING, result); } }
Example #21
Source File: QueryServlet.java From database with GNU General Public License v2.0 | 5 votes |
/** * Check whether the service is overloaded. * @see ConfigParams#EXECUTOR_SERVICE_MAX_THREADS */ private boolean overloaded(final BigdataRDFContext context) { final ExecutorService executorService = getIndexManager().getExecutorService(); if (!(executorService instanceof ThreadPoolExecutor)) { return false; } long limit = context.getConfig().executorMaxThreads; if (limit <= 0) { return false; } final ThreadPoolExecutor tpExecutor = (ThreadPoolExecutor)executorService; return (tpExecutor.getActiveCount() > limit); }
Example #22
Source File: RshServerTestThreaded.java From p4ic4idea with Apache License 2.0 | 5 votes |
/** * Test 'rsh' mode server. */ @Test public void testRshServer() { try{ ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newCachedThreadPool(); //adjust number of threads as needed for testing for (int i = 1; i <= 2; i++) { String depotPath = "//depot/..."; SyncDepot task = new SyncDepot(depotPath); System.out.println("A new task has been added to sync : " + depotPath); executor.execute(task); } executor.shutdown(); while (!executor.isTerminated()) { System.out.println("Threads are still running..."); Thread.sleep(1000); } System.out.println("Finished all threads"); } catch (Exception exc) { fail("Unexpected exception: " + exc.getLocalizedMessage()); } }
Example #23
Source File: AstrixConcurrencyStrategy.java From astrix with Apache License 2.0 | 5 votes |
@PreDestroy public void destroy() { /* * Destroy all threads associated with current AstrixContext */ this.threadPoolByKey.values().stream().forEach(ThreadPoolExecutor::shutdownNow); }
Example #24
Source File: TransactionRetryerGUIDemoController.java From olat with Apache License 2.0 | 5 votes |
private ThreadPoolExecutor createNewThreadPoolExecutor(BlockingQueue<Runnable> workQueue) { int corePoolSize = 2; int maximumPoolSize = 10; long keepAliveTime = 10000; // millis? TimeUnit unit = TimeUnit.MILLISECONDS; threadPoolExecutor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue); return threadPoolExecutor; }
Example #25
Source File: SimpleAsyncManager.java From lastaflute with Apache License 2.0 | 5 votes |
protected void handleRejectedExecution(final Runnable runnable, final ThreadPoolExecutor executor) { // caller thread if (logger.isDebugEnabled()) { logger.debug("#flow #async ...Registering the runnable to waiting queue as retry: " + runnable); } getWaitingQueueExecutorService().execute(() -> { try { retryPuttingQueue(runnable, executor); } catch (InterruptedException e) { final String torExp = buildExecutorHashExp(executor); logger.warn("*Failed to put the runnable to the executor" + torExp + "'s queue: " + runnable, e); } }); }
Example #26
Source File: MoreExecutors.java From slack-client with Apache License 2.0 | 5 votes |
@Override public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { try { // block until there's room executor.getQueue() .put(r); } catch (InterruptedException e) { throw new RejectedExecutionException("Unexpected InterruptedException", e); } }
Example #27
Source File: BatchTestCaseExecution.java From LuckyFrameClient with GNU Affero General Public License v3.0 | 5 votes |
/** * �����̳߳أ����߳�ִ������ * @param taskid ����ID * @param batchcase ���������ַ�����#���� * @throws Exception ���쳣 */ public static void batchCaseExecuteForTast(String taskid, String batchcase) throws Exception{ int threadcount = GetServerApi.cGetTaskSchedulingByTaskId(Integer.parseInt(taskid)).getExThreadCount(); ThreadPoolExecutor threadExecute = new ThreadPoolExecutor(threadcount, 30, 3, TimeUnit.SECONDS, new ArrayBlockingQueue<>(1000), new ThreadPoolExecutor.CallerRunsPolicy()); //ִ��ȫ���dzɹ�״̬���� if(batchcase.contains("ALLFAIL")){ //��ʼ��д��������Լ���־ģ�� serverOperation caselog = new serverOperation(); List<Integer> caseIdList = caselog.getCaseListForUnSucByTaskId(taskid); for (Integer integer : caseIdList) { ProjectCase testcase = GetServerApi.cGetCaseByCaseId(integer); TestControl.THREAD_COUNT++; //���̼߳���++�����ڼ���߳��Ƿ�ȫ��ִ���� threadExecute.execute(new ThreadForBatchCase(testcase.getCaseId(), taskid)); } }else{ //����ִ������ String[] temp=batchcase.split("#"); LogUtil.APP.info("��ǰ����ִ�������й��С�{}��������������...",temp.length); for (String s : temp) { TestControl.THREAD_COUNT++; //���̼߳���++�����ڼ���߳��Ƿ�ȫ��ִ���� threadExecute.execute(new ThreadForBatchCase(Integer.valueOf(s), taskid)); } } //���̼߳��������ڼ���߳��Ƿ�ȫ��ִ���� int i=0; while(TestControl.THREAD_COUNT!=0){ i++; if(i>600){ break; } Thread.sleep(6000); } threadExecute.shutdown(); }
Example #28
Source File: UnloadDistributedZkTest.java From lucene-solr with Apache License 2.0 | 5 votes |
private void testUnloadLotsOfCores() throws Exception { JettySolrRunner jetty = jettys.get(0); try (final HttpSolrClient adminClient = (HttpSolrClient) jetty.newClient(15000, 60000)) { int numReplicas = atLeast(3); ThreadPoolExecutor executor = new ExecutorUtil.MDCAwareThreadPoolExecutor(0, Integer.MAX_VALUE, 5, TimeUnit.SECONDS, new SynchronousQueue<>(), new SolrNamedThreadFactory("testExecutor")); try { // create the cores createCollectionInOneInstance(adminClient, jetty.getNodeName(), executor, "multiunload", 2, numReplicas); } finally { ExecutorUtil.shutdownAndAwaitTermination(executor); } executor = new ExecutorUtil.MDCAwareThreadPoolExecutor(0, Integer.MAX_VALUE, 5, TimeUnit.SECONDS, new SynchronousQueue<>(), new SolrNamedThreadFactory("testExecutor")); try { for (int j = 0; j < numReplicas; j++) { final int freezeJ = j; executor.execute(() -> { Unload unloadCmd = new Unload(true); unloadCmd.setCoreName("multiunload" + freezeJ); try { adminClient.request(unloadCmd); } catch (SolrServerException | IOException e) { throw new RuntimeException(e); } }); Thread.sleep(random().nextInt(50)); } } finally { ExecutorUtil.shutdownAndAwaitTermination(executor); } } }
Example #29
Source File: ReportServlet.java From sailfish-core with Apache License 2.0 | 5 votes |
@Override public void init(ServletConfig config) throws ServletException { super.init(config); ThreadFactory threadFactory = new ThreadFactoryBuilder() .setNameFormat(ReportServlet.class.getSimpleName() + "-%d") .setDaemon(true) .build(); this.executor = new ThreadPoolExecutor(4, 4, 1, TimeUnit.MINUTES, new LinkedBlockingQueue<Runnable>(), threadFactory); }
Example #30
Source File: ThreadPoolMXBeanAdapter.java From ignite with Apache License 2.0 | 5 votes |
/** {@inheritDoc} */ @Override public String getRejectedExecutionHandlerClass() { if (!(exec instanceof ThreadPoolExecutor)) return ""; RejectedExecutionHandler hnd = ((ThreadPoolExecutor)exec).getRejectedExecutionHandler(); return hnd == null ? "" : hnd.getClass().getName(); }