@SuppressWarnings("FutureReturnValueIgnored") synchronized void submit(ForkJoinTask task) { pool.submit(task); }
/** * @param grpCtx Group context. * @param part Local partition. */ private Future<Map<PartitionKey, PartitionHashRecord>> calculatePartitionHashAsync( final CacheGroupContext grpCtx, final GridDhtLocalPartition part ) { return ForkJoinPool.commonPool().submit(new Callable<Map<PartitionKey, PartitionHashRecord>>() { @Override public Map<PartitionKey, PartitionHashRecord> call() throws Exception { return calculatePartitionHash(grpCtx, part); } }); }
/** * @param grpCtx Group context. * @param part Local partition. * @param cpFlag Checkpoint flag. */ private Future<Map<PartitionKeyV2, PartitionHashRecordV2>> calculatePartitionHashAsync( final CacheGroupContext grpCtx, final GridDhtLocalPartition part, AtomicBoolean cpFlag ) { return ForkJoinPool.commonPool().submit(() -> calculatePartitionHash(grpCtx, part, cpFlag)); }
public void publish(Runnable task) { ForkJoinPool current = ForkJoinTask.getPool(); ForkedRunnable fajita = new ForkedRunnable(task); count.incrementAndGet(); if (current==pool) fajita.fork(); else pool.submit(fajita); }
ForkJoinPool pool = new ForkJoinPool(parallelism); try { pool.submit(() -> taskIndices.forEach(task::accept)).get(); } catch (InterruptedException e) { throw new IllegalStateException(e);
ForkJoinPool pool = new ForkJoinPool(parallelism); try { return pool.submit(() -> taskIndices.mapToObj(task::apply).collect(collector)).get(); } catch (InterruptedException e) { throw new IllegalStateException(e);
bySuccess = parallelizer.getForkJoinPool().submit(() -> args.works.parallelStream().collect( Collectors.groupingByConcurrent((x) -> {
@Override protected AbstractFSWAL<?> createWAL() throws IOException { // just like what may do in the WALListeners, schedule an asynchronous task to call the // getWALs method. GET_WALS_FUTURE = ForkJoinPool.commonPool().submit(this::getWALs); // sleep a while to make the getWALs arrive before we return Threads.sleep(2000); return Mockito.mock(AbstractFSWAL.class); }
ForkJoinTask rootTask = builderPool.submit(new Runnable() { @Override public void run() {
@Test(timeout = 5_000) public void testThreadSafety() throws Exception { final int numOfThreads = 3; CountDownLatch latch = new CountDownLatch(numOfThreads); AtomicInteger counter = new AtomicInteger(); Future<Integer> lazyFuture = new LazyFuture<Integer>() { @Override @SneakyThrows(InterruptedException.class) protected Integer resolve() { latch.await(); return counter.incrementAndGet(); } }; Future<List<Integer>> task = new ForkJoinPool(numOfThreads).submit(() -> { return IntStream.rangeClosed(1, numOfThreads).parallel().mapToObj(i -> Futures.getUnchecked(lazyFuture)).collect(toList()); }); while (latch.getCount() > 0) { latch.countDown(); } assertEquals("All threads receives the same result", Collections.nCopies(numOfThreads, 1), task.get()); }
builderPool.submit(new Runnable() { @Override public void run() {
CountDownLatch txCommitLatch = new CountDownLatch( 1 ); Future<?> result = ForkJoinPool.commonPool().submit( () ->
new ReplicationSourceWALReader(fs, CONF, walQueue, 0, getDummyFilter(), source); reader.start(); Future<WALEntryBatch> future = ForkJoinPool.commonPool().submit(() -> { return reader.take(); });
default <R> R foldParallel(ForkJoinPool fj,Function<? super Stream<T>,? extends R> fn){ return fj.submit(() -> foldParallel(fn)).join(); }
protected List<Path> writeParallel(Configuration hadoopConfig, Map<Path, List<byte[]>> toWrite, int parallelism) throws IOException { List<Path> outFiles = Collections.synchronizedList(new ArrayList<>()); ForkJoinPool tp = new ForkJoinPool(parallelism); try { tp.submit(() -> { toWrite.entrySet().parallelStream().forEach(e -> { Path path = e.getKey(); List<byte[]> data = e.getValue(); if (data.size() > 0) { try { write(getResultsWriter(), hadoopConfig, data, path); } catch (IOException ioe) { throw new RuntimeException( String.format("Failed to write results to path '%s'", path.toString()), ioe); } outFiles.add(path); } }); }).get(); } catch (InterruptedException | ExecutionException e) { throw new IOException("Error finalizing results.", e); } catch (RuntimeException e) { throw new IOException(e.getMessage(), e.getCause()); } outFiles.sort((o1, o2) -> o1.getName().compareTo(o2.getName())); return outFiles; }
/** * @throws Exception If failed. */ @Test public void testSizeIsConsistentAfterRebalanceDuringInsert() throws Exception { IgniteEx ignite = startGrid(0); IgniteCache<?, ?> tbl = createTable(ignite); Future<?> f = null; for (int i = 0; i < 100; i++) { if (i == 50) f = ForkJoinPool.commonPool().submit(() -> startGrid(1)); tbl.query(q("insert into person values(?, ?)").setArgs(i, i)); } f.get(); awaitPartitionMapExchange(); IgniteCache<?, ?> tbl0 = grid(0).cache("person"); IgniteCache<?, ?> tbl1 = grid(1).cache("person"); assert tbl0.localSize() != 0 && tbl1.localSize() != 0; assertEquals(100, tbl1.size()); assertEquals(100, tbl0.localSize() + tbl1.localSize()); }
private int getNumberOfBatches(final ReaderSpliterator spliterator) throws ExecutionException, InterruptedException { final AtomicInteger numSplits = new AtomicInteger(0); //we want to wrap the spliterator and count the (valid) splits Spliterator<String> delegatingSpliterator = spy(spliterator); doAnswer(invocationOnMock -> { Spliterator<String> ret = spliterator.trySplit(); if(ret != null) { numSplits.incrementAndGet(); } return ret; }).when(delegatingSpliterator).trySplit(); Stream<String> stream = StreamSupport.stream(delegatingSpliterator, true); //now run it in a parallel pool and do some calculation that doesn't really matter. ForkJoinPool forkJoinPool = ForkJoinPool.commonPool(); forkJoinPool.submit(() -> { Map<String, Integer> threads = stream.parallel().map(s -> Thread.currentThread().getName()) .collect(Collectors.toMap(s -> s, s -> 1, Integer::sum)); Assert.assertTrue(threads.size() > 0); } ).get(); return numSplits.get(); }
@Test public void tlsh_multithread() throws Exception { //we want to ensure that everything is threadsafe, so we'll spin up some random data //generate some hashes and then do it all in parallel and make sure it all matches. Map<Map.Entry<byte[], Map<String, Object>>, String> hashes = new HashMap<>(); Random r = new Random(0); for(int i = 0;i < 20;++i) { byte[] d = new byte[256]; r.nextBytes(d); Map<String, Object> config = new HashMap<String, Object>() {{ put(TLSHHasher.Config.BUCKET_SIZE.key, r.nextBoolean() ? 128 : 256); put(TLSHHasher.Config.CHECKSUM.key, r.nextBoolean() ? 1 : 3); }}; String hash = (String)run("HASH(data, 'tlsh', config)", ImmutableMap.of("config", config, "data", d)); Assert.assertNotNull(hash); hashes.put(new AbstractMap.SimpleEntry<>(d, config), hash); } ForkJoinPool forkJoinPool = new ForkJoinPool(5); forkJoinPool.submit(() -> hashes.entrySet().parallelStream().forEach( kv -> { Map<String, Object> config = kv.getKey().getValue(); byte[] data = kv.getKey().getKey(); String hash = (String)run("HASH(data, 'tlsh', config)", ImmutableMap.of("config", config, "data", data)); Assert.assertEquals(hash, kv.getValue()); } ) ); }
@Test public void testCreatingMultipleTablesAtOnce() throws InterruptedException { int threadCount = 16; CyclicBarrier barrier = new CyclicBarrier(threadCount); ForkJoinPool threadPool = new ForkJoinPool(threadCount); threadPool.submit(() -> IntStream.range(0, threadCount).parallel().forEach(i -> { try { barrier.await(); slowTimeoutKvs.createTable(GOOD_TABLE, AtlasDbConstants.GENERIC_TABLE_METADATA); } catch (BrokenBarrierException | InterruptedException e) { // Do nothing } })); threadPool.shutdown(); Preconditions.checkState(threadPool.awaitTermination(90, TimeUnit.SECONDS), "Not all table creation threads completed within the time limit"); slowTimeoutKvs.dropTable(GOOD_TABLE); }