Java Code Examples for java.util.concurrent.ExecutorService#invokeAll()
The following examples show how to use
java.util.concurrent.ExecutorService#invokeAll() .
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: ToStringStyleConcurrencyTest.java From astor with GNU General Public License v2.0 | 6 votes |
private void testConcurrency(final CollectionHolder<List<Integer>> holder) throws InterruptedException, ExecutionException { final List<Integer> list = holder.collection; // make a big array that takes a long time to toString() list.addAll(LIST); // Create a thread pool with two threads to cause the most contention on the underlying resource. final ExecutorService threadPool = Executors.newFixedThreadPool(2); // Consumes toStrings final Callable<Integer> consumer = new Callable<Integer>() { @Override public Integer call() { for (int i = 0; i < REPEAT; i++) { // Calls ToStringStyle new ToStringBuilder(holder).append(holder.collection); } return Integer.valueOf(REPEAT); } }; final Collection<Callable<Integer>> tasks = new ArrayList<Callable<Integer>>(); tasks.add(consumer); tasks.add(consumer); final List<Future<Integer>> futures = threadPool.invokeAll(tasks); for (final Future<Integer> future : futures) { future.get(); } }
Example 2
Source File: AbstractExecutorServiceTest.java From j2objc with Apache License 2.0 | 6 votes |
/** * get of returned element of invokeAll(c) throws exception on failed task */ public void testTimedInvokeAll4() throws Exception { final ExecutorService e = new DirectExecutorService(); try (PoolCleaner cleaner = cleaner(e)) { List<Callable<String>> l = new ArrayList<Callable<String>>(); l.add(new NPETask()); List<Future<String>> futures = e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); assertEquals(1, futures.size()); try { futures.get(0).get(); shouldThrow(); } catch (ExecutionException success) { assertTrue(success.getCause() instanceof NullPointerException); } } }
Example 3
Source File: ScanManager.java From ciscorouter with MIT License | 6 votes |
/** * Runs the scan on all hosts and returns the full report * @return The report covering all hosts */ public FullReport run() { FullReport fullReport = null; try { ExecutorService executor = Executors.newFixedThreadPool(MAX_THREADS); List<Future<HostReport>> futureReports; ArrayList<HostReport> reports = new ArrayList<>(); ArrayList<Scanner> scanners = new ArrayList<>(); for (Host h : hosts) { Scanner s = new Scanner(h); scanners.add(s); } futureReports = executor.invokeAll(scanners); for (Future<HostReport> fReport : futureReports) { reports.add(fReport.get()); } fullReport = new FullReport(reports); } catch (InterruptedException | ExecutionException ex) { Logger.getLogger(ScanManager.class.getName()).log(Level.SEVERE, null, ex); } return fullReport; }
Example 4
Source File: MVDeconvolution.java From SPIM_Registration with GNU General Public License v2.0 | 6 votes |
private static final void execTasks( final ArrayList< Callable< Void > > tasks, final int nThreads, final String jobDescription ) { final ExecutorService taskExecutor = Executors.newFixedThreadPool( nThreads ); try { // invokeAll() returns when all tasks are complete taskExecutor.invokeAll( tasks ); } catch ( final InterruptedException e ) { IOFunctions.println( "Failed to " + jobDescription + ": " + e ); e.printStackTrace(); return; } taskExecutor.shutdown(); }
Example 5
Source File: ToStringStyleConcurrencyTest.java From astor with GNU General Public License v2.0 | 6 votes |
private void testConcurrency(final CollectionHolder<List<Integer>> holder) throws InterruptedException, ExecutionException { final List<Integer> list = holder.collection; // make a big array that takes a long time to toString() list.addAll(LIST); // Create a thread pool with two threads to cause the most contention on the underlying resource. final ExecutorService threadPool = Executors.newFixedThreadPool(2); // Consumes toStrings Callable<Integer> consumer = new Callable<Integer>() { @Override public Integer call() { for (int i = 0; i < REPEAT; i++) { // Calls ToStringStyle new ToStringBuilder(holder).append(holder.collection); } return Integer.valueOf(REPEAT); } }; Collection<Callable<Integer>> tasks = new ArrayList<Callable<Integer>>(); tasks.add(consumer); tasks.add(consumer); final List<Future<Integer>> futures = threadPool.invokeAll(tasks); for (Future<Integer> future : futures) { future.get(); } }
Example 6
Source File: BulkheadFallbackRejectTest.java From smallrye-fault-tolerance with Apache License 2.0 | 6 votes |
@Test public void testFallbackNotRejected() throws InterruptedException, ExecutionException { ExecutorService executorService = Executors.newFixedThreadPool(QUEUE_SIZE); try { List<Callable<String>> tasks = new ArrayList<>(); for (int i = 1; i <= QUEUE_SIZE; i++) { tasks.add(() -> pingService.ping()); } List<Future<String>> futures = executorService.invokeAll(tasks); for (Future<String> future : futures) { assertThat(future.get(), anyOf(equalTo("fallback"), equalTo("pong"))); } } finally { if (executorService != null) { executorService.shutdown(); } } }
Example 7
Source File: ThreadPoolExecutorTest.java From j2objc with Apache License 2.0 | 6 votes |
/** * timed invokeAll(c) returns results of all completed tasks */ public void testTimedInvokeAll5() 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<Callable<String>>(); l.add(new StringTask()); l.add(new StringTask()); List<Future<String>> futures = e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); assertEquals(2, futures.size()); for (Future<String> future : futures) assertSame(TEST_STRING, future.get()); } }
Example 8
Source File: ScheduledExecutorSubclassTest.java From j2objc with Apache License 2.0 | 5 votes |
/** * timed invokeAll(empty collection) returns empty collection */ public void testTimedInvokeAll2() throws Exception { final ExecutorService e = new CustomExecutor(2); try (PoolCleaner cleaner = cleaner(e)) { List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); assertTrue(r.isEmpty()); } }
Example 9
Source File: ScheduledExecutorSubclassTest.java From j2objc with Apache License 2.0 | 5 votes |
/** * timed invokeAll(c) returns results of all completed tasks */ public void testTimedInvokeAll5() throws Exception { final ExecutorService e = new CustomExecutor(2); try (PoolCleaner cleaner = cleaner(e)) { List<Callable<String>> l = new ArrayList<Callable<String>>(); l.add(new StringTask()); l.add(new StringTask()); List<Future<String>> futures = e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); assertEquals(2, futures.size()); for (Future<String> future : futures) assertSame(TEST_STRING, future.get()); } }
Example 10
Source File: AbstractExecutorServiceTest.java From j2objc with Apache License 2.0 | 5 votes |
/** * invokeAll(c) returns results of all completed tasks in c */ public void testInvokeAll5() throws Exception { final ExecutorService e = new DirectExecutorService(); try (PoolCleaner cleaner = cleaner(e)) { List<Callable<String>> l = new ArrayList<Callable<String>>(); l.add(new StringTask()); l.add(new StringTask()); List<Future<String>> futures = e.invokeAll(l); assertEquals(2, futures.size()); for (Future<String> future : futures) assertSame(TEST_STRING, future.get()); } }
Example 11
Source File: AbstractExecutorServiceTest.java From j2objc with Apache License 2.0 | 5 votes |
/** * invokeAll(c) throws NPE if c has null elements */ public void testInvokeAll3() throws InterruptedException { final ExecutorService e = new DirectExecutorService(); try (PoolCleaner cleaner = cleaner(e)) { List<Callable<String>> l = new ArrayList<Callable<String>>(); l.add(new StringTask()); l.add(null); try { e.invokeAll(l); shouldThrow(); } catch (NullPointerException success) {} } }
Example 12
Source File: ForkJoinPoolTest.java From j2objc with Apache License 2.0 | 5 votes |
/** * invokeAll(empty collection) returns empty collection */ public void testInvokeAll2() throws InterruptedException { ExecutorService e = new ForkJoinPool(1); try (PoolCleaner cleaner = cleaner(e)) { List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); assertTrue(r.isEmpty()); } }
Example 13
Source File: ScheduledExecutorSubclassTest.java From j2objc with Apache License 2.0 | 5 votes |
/** * timed invokeAll(c) throws NPE if c has null elements */ public void testTimedInvokeAll3() throws Exception { final ExecutorService e = new CustomExecutor(2); try (PoolCleaner cleaner = cleaner(e)) { List<Callable<String>> l = new ArrayList<Callable<String>>(); l.add(new StringTask()); l.add(null); try { e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (NullPointerException success) {} } }
Example 14
Source File: GetMacAddress.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
public static void main(String[] args) throws Exception { List<NetworkInterface> toTest = getNetworkInterfacesAsStream() .filter(hasHardwareAddress) .collect(Collectors.toList()); ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS); for (NetworkInterface ni : toTest) { Phaser startingGate = new Phaser(NUM_THREADS); System.out.println("Testing: " + ni.getName()); List<Callable<Exception>> list = new ArrayList<>(); for (int i = 0; i < NUM_THREADS; i++) list.add(new GetMacAddress(ni, ni.getName() + "-Thread-" + i, startingGate)); List<Future<Exception>> futures = executor.invokeAll(list); for (Future<Exception> f : futures) { if (f.get() != null) f.get().printStackTrace(System.out); } if (failed) break; } executor.shutdownNow(); if (!failed) { System.out.println("PASSED - Finished all threads"); } else { throw new RuntimeException("Failed"); } }
Example 15
Source File: ExecutorIT.java From glowroot with Apache License 2.0 | 5 votes |
@Override public void transactionMarker() throws Exception { ExecutorService executor = createExecutorService(); List<Callable<Void>> callables = Lists.newArrayList(); callables.add(new Callable<Void>() { @Override public Void call() { new CreateTraceEntry().traceEntryMarker(); return null; } }); callables.add(new Callable<Void>() { @Override public Void call() { new CreateTraceEntry().traceEntryMarker(); return null; } }); callables.add(new Callable<Void>() { @Override public Void call() { new CreateTraceEntry().traceEntryMarker(); return null; } }); for (Future<Void> future : executor.invokeAll(callables, 10, SECONDS)) { future.get(); } }
Example 16
Source File: AllureLifecycleTest.java From allure1 with Apache License 2.0 | 5 votes |
@Test public void supportForConcurrentUseOfChildThreads() throws Exception { final int threads = 20; final int stepsCount = 1000; final Allure lifecycle = Allure.LIFECYCLE; String suiteUid = UUID.randomUUID().toString(); fireTestSuiteStart(suiteUid); fireTestCaseStart(suiteUid); ExecutorService service = Executors.newFixedThreadPool(threads); final List<Callable<Object>> tasks = new ArrayList<>(); for (int i = 0; i < threads; i++) { tasks.add(new Callable<Object>() { @Override public Object call() throws Exception { for (int i = 0; i < stepsCount; i++) { lifecycle.fire(new StepStartedEvent("test-step")); lifecycle.fire(new StepFinishedEvent()); } return ""; } }); } List<Future<Object>> futures = service.invokeAll(tasks); for (Future<Object> future : futures) { future.get(); } assertThat(lifecycle.getStepStorage().pollLast().getSteps(), hasSize(threads * stepsCount)); }
Example 17
Source File: ScheduledExecutorSubclassTest.java From j2objc with Apache License 2.0 | 5 votes |
/** * invokeAll(c) throws NPE if c has null elements */ public void testInvokeAll3() throws Exception { final ExecutorService e = new CustomExecutor(2); try (PoolCleaner cleaner = cleaner(e)) { List<Callable<String>> l = new ArrayList<Callable<String>>(); l.add(new StringTask()); l.add(null); try { e.invokeAll(l); shouldThrow(); } catch (NullPointerException success) {} } }
Example 18
Source File: GridBoundedConcurrentLinkedHashSetLoadTest.java From ignite with Apache License 2.0 | 4 votes |
/** * @param args Arguments. */ public static void main(String[] args) throws Exception { QueuePolicy qPlc = args.length > 0 ? QueuePolicy.valueOf(args[0]) : SINGLE_Q; int threadCnt = args.length > 1 ? Integer.valueOf(args[1]) : Runtime.getRuntime().availableProcessors(); X.println("Queue policy: " + qPlc); X.println("Threads: " + threadCnt); ExecutorService pool = Executors.newFixedThreadPool(threadCnt); final Collection<IgniteUuid> set = new GridBoundedConcurrentLinkedHashSet<>(10240, 32, 0.75f, 128, qPlc); X.println("Set: " + set); final LongAdder execCnt = new LongAdder(); final AtomicBoolean finish = new AtomicBoolean(); // Thread that measures and outputs performance statistics. Thread collector = new Thread(new Runnable() { @SuppressWarnings({"BusyWait"}) @Override public void run() { GridCumulativeAverage avgTasksPerSec = new GridCumulativeAverage(); try { while (!finish.get()) { Thread.sleep(UPDATE_INTERVAL_SEC * 1000); long curTasksPerSec = execCnt.sumThenReset() / UPDATE_INTERVAL_SEC; X.println(">>> Tasks/s: " + curTasksPerSec); avgTasksPerSec.update(curTasksPerSec); } } catch (InterruptedException ignored) { X.println(">>> Interrupted."); Thread.currentThread().interrupt(); } } }); collector.start(); Collection<Callable<Object>> producers = new ArrayList<>(threadCnt); for (int i = 0; i < threadCnt; i++) producers.add(new Callable<Object>() { @Override public Object call() throws Exception { UUID id = UUID.randomUUID(); try { while (!finish.get()) { set.add(IgniteUuid.fromUuid(id)); execCnt.increment(); } return null; } catch (Throwable t) { t.printStackTrace(); throw new Exception(t); } finally { X.println("Thread finished."); } } }); pool.invokeAll(producers); }
Example 19
Source File: MCSS.java From ReactionDecoder with GNU Lesser General Public License v3.0 | 4 votes |
private synchronized LinkedBlockingQueue<IAtomContainer> submitMultiThreadedJob(List<IAtomContainer> mcssList, JobType jobType, int nThreads) { int taskNumber = 1; LinkedBlockingQueue<IAtomContainer> solutions = new LinkedBlockingQueue<>(); LinkedBlockingQueue<Callable<LinkedBlockingQueue<IAtomContainer>>> callablesQueue = new LinkedBlockingQueue<>(); ExecutorService threadPool = newFixedThreadPool(nThreads); int step = (int) ceil(mcssList.size() / nThreads); if (step < 2) { step = 2; // Can't have a step size of less than 2 } for (int i = 0; i < mcssList.size(); i += step) { int endPoint = i + step; if (endPoint > mcssList.size()) { endPoint = mcssList.size(); } List<IAtomContainer> subList = new ArrayList<>(mcssList.subList(i, endPoint)); if (subList.size() > 1) { MCSSThread mcssJobThread = new MCSSThread(subList, jobType, taskNumber, am ,bm); callablesQueue.add(mcssJobThread); taskNumber++; } else { solutions.add(subList.get(0)); } } try { /* * Wait all the threads to finish */ List<Future<LinkedBlockingQueue<IAtomContainer>>> futureList = threadPool.invokeAll(callablesQueue); /* * Collect the results */ for (Future<LinkedBlockingQueue<IAtomContainer>> callable : futureList) { LinkedBlockingQueue<IAtomContainer> mapping = callable.get(); if (callable.isDone() && mapping != null) { solutions.addAll(mapping); } else { LOGGER.warn("WARNING: InComplete job in AtomMappingTool: "); } } threadPool.shutdown(); // Wait until all threads are finish while (!threadPool.isTerminated()) { } gc(); } catch (InterruptedException | ExecutionException e) { LOGGER.debug("ERROR: in AtomMappingTool: " + e.getMessage()); LOGGER.error(e); } finally { threadPool.shutdown(); } return solutions; }
Example 20
Source File: WatchmanWatcher.java From buck with Apache License 2.0 | 4 votes |
/** * Query Watchman for file change events. If too many events are pending or an error occurs an * overflow event is posted to the EventBus signalling that events may have been lost (and so * typically caches must be cleared to avoid inconsistency). Interruptions and IOExceptions are * propagated to callers, but typically if overflow events are handled conservatively by * subscribers then no other remedial action is required. * * <p>Any diagnostics posted by Watchman are added to watchmanDiagnosticCache. */ public void postEvents(BuckEventBus buckEventBus, FreshInstanceAction freshInstanceAction) throws IOException, InterruptedException { // Speculatively set to false AtomicBoolean filesHaveChanged = new AtomicBoolean(false); ExecutorService executorService = MostExecutors.newMultiThreadExecutor(getClass().getName(), numThreads); buckEventBus.post(WatchmanStatusEvent.started()); try { List<Callable<Unit>> watchmanQueries = new ArrayList<>(); for (AbsPath cellPath : queries.keySet()) { watchmanQueries.add( () -> { WatchmanQuery query = queries.get(cellPath); WatchmanCursor cursor = cursors.get(cellPath); if (query != null && cursor != null) { try (SimplePerfEvent.Scope perfEvent = SimplePerfEvent.scope( buckEventBus, SimplePerfEvent.PerfEventId.of("check_watchman"), "cell", cellPath); WatchmanClient client = watchmanClientFactory.newInstance()) { // Include the cellPath in the finished event so it can be matched with the begin // event. perfEvent.appendFinishedInfo("cell", cellPath); postEvents( buckEventBus, freshInstanceAction, cellPath, client, query, cursor, filesHaveChanged, perfEvent); } } return Unit.UNIT; }); } // Run all of the Watchman queries in parallel. This can be significant if you have a lot of // cells. List<Future<Unit>> futures = executorService.invokeAll(watchmanQueries); for (Future<Unit> future : futures) { try { future.get(); } catch (ExecutionException e) { Throwable cause = e.getCause(); if (cause != null) { Throwables.throwIfUnchecked(cause); Throwables.propagateIfPossible(cause, IOException.class); Throwables.propagateIfPossible(cause, InterruptedException.class); } throw new RuntimeException(e); } } if (!filesHaveChanged.get()) { buckEventBus.post(WatchmanStatusEvent.zeroFileChanges()); } } finally { buckEventBus.post(WatchmanStatusEvent.finished()); executorService.shutdown(); } }