/** * 新建一个CompletionService,调用其submit方法可以异步执行多个任务,最后调用take方法按照完成的顺序获得其结果。<br> * 若未完成,则会阻塞 * * @param <T> 回调对象类型 * @param executor 执行器 {@link ExecutorService} * @return CompletionService */ public static <T> CompletionService<T> newCompletionService(ExecutorService executor) { return new ExecutorCompletionService<T>(executor); }
/** * 新建一个CompletionService,调用其submit方法可以异步执行多个任务,最后调用take方法按照完成的顺序获得其结果。<br> * 若未完成,则会阻塞 * * @param <T> 回调对象类型 * @param executor 执行器 {@link ExecutorService} * @return CompletionService */ public static <T> CompletionService<T> newCompletionService(ExecutorService executor) { return new ExecutorCompletionService<T>(executor); }
public PoolService(int threadPoolSize) { ThreadFactory threadFactory = new ThreadFactory() { private final AtomicInteger threadNumber = new AtomicInteger(0); @Override public Thread newThread(Runnable r) { return new Thread(r, ThreadUtil.THREAD_NAME + "-PoolService-" + threadNumber.getAndIncrement()); } }; m_executor = Executors.newFixedThreadPool(threadPoolSize, threadFactory); m_completionService = new ExecutorCompletionService<>(m_executor); }
public ExecutorTemplate(ThreadPoolExecutor executor){ futures = Collections.synchronizedList(new ArrayList<Future>()); comletions = new ExecutorCompletionService(executor); }
public PoolService(int threadPoolSize) { ThreadFactory threadFactory = new ThreadFactory() { private final AtomicInteger threadNumber = new AtomicInteger(0); @Override public Thread newThread(@Nonnull Runnable r) { return new Thread( r, ThreadUtil.THREAD_NAME + "-PoolService-" + threadNumber.getAndIncrement()); } }; m_executor = Executors.newFixedThreadPool(threadPoolSize, threadFactory); m_completionService = new ExecutorCompletionService<>(m_executor); }
/** * 新建一个CompletionService,调用其submit方法可以异步执行多个任务,最后调用take方法按照完成的顺序获得其结果。<br> * 若未完成,则会阻塞 * * @param <T> 回调对象类型 * @return CompletionService */ public static <T> CompletionService<T> newCompletionService() { return new ExecutorCompletionService<T>(GlobalThreadPool.getExecutor()); }
/** * 新建一个CompletionService,调用其submit方法可以异步执行多个任务,最后调用take方法按照完成的顺序获得其结果。<br> * 若未完成,则会阻塞 * * @param <T> 回调对象类型 * @return CompletionService */ public static <T> CompletionService<T> newCompletionService() { return new ExecutorCompletionService<T>(GlobalThreadPool.getExecutor()); }
final ExecutorService pool = Executors.newFixedThreadPool(2); final CompletionService<String> service = new ExecutorCompletionService<String>(pool); final List<? extends Callable<String>> callables = Arrays.asList( new SleepingCallable("slow", 5000), new SleepingCallable("quick", 500)); for (final Callable<String> callable : callables) { service.submit(callable); } pool.shutdown(); try { while (!pool.isTerminated()) { final Future<String> future = service.take(); System.out.println(future.get()); } } catch (ExecutionException | InterruptedException ex) { }
public IteratorExecutor(Iterator<Callable<T>> runnableIterator, int numThreads, ThreadFactory threadFactory) { this.numThreads = numThreads; this.iterator = runnableIterator; this.executor = ExecutorsUtils.loggingDecorator(Executors.newFixedThreadPool(numThreads, threadFactory)); this.completionService = new ExecutorCompletionService<>(this.executor); this.executed = false; }
public ParallelJobListener(List<JobListener> jobListeners) { this.jobListeners = jobListeners; this.executor = Executors.newCachedThreadPool( ExecutorsUtils.newThreadFactory(Optional.of(LOGGER), Optional.of("ParallelJobListener"))); this.completionService = new ExecutorCompletionService<>(this.executor); }
public LogRollBackupSubprocedurePool(String name, Configuration conf) { // configure the executor service long keepAlive = conf.getLong(LogRollRegionServerProcedureManager.BACKUP_TIMEOUT_MILLIS_KEY, LogRollRegionServerProcedureManager.BACKUP_TIMEOUT_MILLIS_DEFAULT); int threads = conf.getInt(CONCURENT_BACKUP_TASKS_KEY, DEFAULT_CONCURRENT_BACKUP_TASKS); this.name = name; executor = new ThreadPoolExecutor(1, threads, keepAlive, TimeUnit.SECONDS, new LinkedBlockingQueue<>(), new DaemonThreadFactory("rs(" + name + ")-backup-pool")); taskPool = new ExecutorCompletionService<>(executor); }
SnapshotSubprocedurePool(String name, Configuration conf, Abortable abortable) { this.abortable = abortable; // configure the executor service long keepAlive = conf.getLong( RegionServerSnapshotManager.SNAPSHOT_TIMEOUT_MILLIS_KEY, RegionServerSnapshotManager.SNAPSHOT_TIMEOUT_MILLIS_DEFAULT); int threads = conf.getInt(CONCURENT_SNAPSHOT_TASKS_KEY, DEFAULT_CONCURRENT_SNAPSHOT_TASKS); this.name = name; executor = new ThreadPoolExecutor(threads, threads, keepAlive, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(), new DaemonThreadFactory("rs(" + name + ")-snapshot-pool")); executor.allowCoreThreadTimeOut(true); taskPool = new ExecutorCompletionService<>(executor); }
FlushTableSubprocedurePool(String name, Configuration conf, Abortable abortable) { this.abortable = abortable; // configure the executor service long keepAlive = conf.getLong( RegionServerFlushTableProcedureManager.FLUSH_TIMEOUT_MILLIS_KEY, RegionServerFlushTableProcedureManager.FLUSH_TIMEOUT_MILLIS_DEFAULT); int threads = conf.getInt(CONCURENT_FLUSH_TASKS_KEY, DEFAULT_CONCURRENT_FLUSH_TASKS); this.name = name; executor = new ThreadPoolExecutor(threads, threads, keepAlive, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(), new DaemonThreadFactory("rs(" + name + ")-flush-proc-pool")); executor.allowCoreThreadTimeOut(true); taskPool = new ExecutorCompletionService<>(executor); }
public MultiThreadProcessor(final PMDConfiguration configuration) { super(configuration); executor = Executors.newFixedThreadPool(configuration.getThreads(), new PmdThreadFactory()); completionService = new ExecutorCompletionService<>(executor); }
public SampleResult getCollectSampleResultByIp(String ip, int sampleTime) throws Exception { List<SampleResult> resultList = new ArrayList<SampleResult>(10); String url = Constants.COMMUNICATION_CONTROLLER_PATH + "/watcherConfigs"; Map<String, String> params = new HashMap<String, String>(50); params.put("ip", ip); BlockingQueue<Future<SampleResult>> queue = new LinkedBlockingDeque<Future<SampleResult>>( serverListService.getServerList().size()); CompletionService<SampleResult> completionService = new ExecutorCompletionService<SampleResult>(scheduler, queue); SampleResult sampleCollectResult = new SampleResult(); for (int i = 0; i < sampleTime; i++) { List<SampleResult> sampleResults = runCollectionJob(url, params, completionService, resultList); if (sampleResults != null) { sampleCollectResult = mergeSampleResult(sampleCollectResult, sampleResults); } } return sampleCollectResult; }
ExecutorService executor = ...; ExecutorCompletionService ecs = new ExecutorCompletionService(executor); for (int i = 0; i < totalTasks; i++) { ... ecs.submit(...); ... } for (int i = 0; i < totalTasks; i++) { ecs.take(); }
@Before public void init() { executor = Executors.newFixedThreadPool(outerMax); completionService = new ExecutorCompletionService<>(executor); }
public SimpleSubprocedurePool(String name, Configuration conf) { this.name = name; executor = new ThreadPoolExecutor(1, 1, 500, TimeUnit.SECONDS, new LinkedBlockingQueue<>(), new DaemonThreadFactory("rs(" + name + ")-procedure-pool")); taskPool = new ExecutorCompletionService<>(executor); }
@BeforeClass public static void setUp() throws Exception { threadPool = Threads.getBoundedCachedThreadPool(32, 60L, TimeUnit.SECONDS, Threads.newDaemonThreadFactory("ProcedureDispatcher", new UncaughtExceptionHandler() { @Override public void uncaughtException(Thread t, Throwable e) { LOG.warn("Failed thread " + t.getName(), e); } })); executorService = new ExecutorCompletionService(threadPool); }
@Test public void testMultipleClients() throws Exception { ExecutorService exec = Executors.newFixedThreadPool(NUM_THREADS); try { ExecutorCompletionService<Boolean> ecs = new ExecutorCompletionService<>(exec); for (int i = 0; i < NUM_THREADS; ++i) ecs.submit(new IdLockTestThread("client_" + i)); for (int i = 0; i < NUM_THREADS; ++i) { Future<Boolean> result = ecs.take(); assertTrue(result.get()); } idLock.assertMapEmpty(); } finally { exec.shutdown(); exec.awaitTermination(5000, TimeUnit.MILLISECONDS); } }