java.util.concurrent.ForkJoinPool Java Examples

The following examples show how to use java.util.concurrent.ForkJoinPool. 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: ClusterMetricsRetriever.java    From vespa with Apache License 2.0 8 votes vote down vote up
/**
 * Call the metrics API on each host and aggregate the metrics
 * into a single value, grouped by cluster.
 */
public Map<ClusterInfo, MetricsAggregator> requestMetricsGroupedByCluster(Collection<URI> hosts) {
    Map<ClusterInfo, MetricsAggregator> clusterMetricsMap = new ConcurrentHashMap<>();

    long startTime = System.currentTimeMillis();
    Runnable retrieveMetricsJob = () ->
            hosts.parallelStream().forEach(host ->
                getHostMetrics(host, clusterMetricsMap)
            );

    ForkJoinPool threadPool = new ForkJoinPool(10);
    threadPool.submit(retrieveMetricsJob);
    threadPool.shutdown();

    try {
        threadPool.awaitTermination(1, TimeUnit.MINUTES);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }

    log.log(Level.FINE, () ->
            String.format("Metric retrieval for %d nodes took %d milliseconds", hosts.size(), System.currentTimeMillis() - startTime)
    );

    return clusterMetricsMap;
}
 
Example #2
Source File: ChiSquareFrequentTermsFBMaster.java    From jate with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public AbstractFeature build() throws JATEException {
    ChiSquareFrequentTerms feature = new ChiSquareFrequentTerms();

    int cores = properties.getMaxCPUCores();
    cores = cores == 0 ? 1 : cores;

    int maxPerThread = getMaxPerThread(cores);

    LOG.info("Beginning building features (ChiSquare frequent terms). Total terms=" + allFrequentTerms.size() + ", cpu cores=" +
            cores + ", max per core=" + maxPerThread);
    ChiSquareFrequentTermsFBWorker worker = new
            ChiSquareFrequentTermsFBWorker(allFrequentTerms, maxPerThread, ctx2TTF, term2Ctx,
            feature, ttfInCorpus);
    ForkJoinPool forkJoinPool = new ForkJoinPool(cores);
    int total = forkJoinPool.invoke(worker);
    StringBuilder sb = new StringBuilder("Complete building features. Total processed terms = " + total);
    LOG.info(sb.toString());

    return feature;
}
 
Example #3
Source File: ContainmentFBMaster.java    From jate with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public AbstractFeature build() throws JATEException {
    Containment feature = new Containment();

    //start workers
    int cores = properties.getMaxCPUCores();
    cores = cores == 0 ? 1 : cores;
    int maxPerThread = getMaxPerThread(cores);

    StringBuilder sb = new StringBuilder("Building features using cpu cores=");
    sb.append(cores).append(", total terms=").append(uniqueCandidateTerms.size()).append(", max per worker=")
            .append(maxPerThread);
    LOG.info(sb.toString());
    ContainmentFBWorker worker = new
            ContainmentFBWorker(new ArrayList<>(uniqueCandidateTerms), maxPerThread,
            feature,
            termComponentIndex);
    ForkJoinPool forkJoinPool = new ForkJoinPool(cores);
    int[] total = forkJoinPool.invoke(worker);
    sb = new StringBuilder("Complete building features. Total=");
    sb.append(total[1]).append(" success=").append(total[0]);
    LOG.info(sb.toString());

    return feature;
}
 
Example #4
Source File: MappedFaweQueue.java    From FastAsyncWorldedit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void optimize() {
    final ForkJoinPool pool = TaskManager.IMP.getPublicForkJoinPool();
    map.forEachChunk(new RunnableVal<FaweChunk>() {
        @Override
        public void run(final FaweChunk chunk) {
            pool.submit(new Runnable() {
                @Override
                public void run() {
                    chunk.optimize();
                }
            });
        }
    });
    pool.awaitQuiescence(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
}
 
Example #5
Source File: SkipperStreamDeployerTests.java    From spring-cloud-dataflow with Apache License 2.0 6 votes vote down vote up
@Test
public void testInvalidPlatformName() {
	Map<String, String> skipperDeployerProperties = new HashMap<>();
	skipperDeployerProperties.put(SkipperStream.SKIPPER_PACKAGE_NAME, "package1");
	skipperDeployerProperties.put(SkipperStream.SKIPPER_PACKAGE_VERSION, "1.0.1");
	skipperDeployerProperties.put(SkipperStream.SKIPPER_PLATFORM_NAME, "badPlatform");
	skipperDeployerProperties.put(SkipperStream.SKIPPER_REPO_NAME, "mylocal-repo1");
	StreamDeploymentRequest streamDeploymentRequest = new StreamDeploymentRequest("test1", "time | log",
			new ArrayList<>(),
			skipperDeployerProperties);

	SkipperClient skipperClient = MockUtils.createSkipperClientMock();

	SkipperStreamDeployer skipperStreamDeployer = new SkipperStreamDeployer(skipperClient,
			mock(StreamDefinitionRepository.class), mock(AppRegistryService.class), mock(ForkJoinPool.class), new DefaultStreamDefinitionService());
	try {
		skipperStreamDeployer.deployStream(streamDeploymentRequest);
		fail();
	}
	catch (IllegalArgumentException expected) {
		assertThat(expected).hasMessage("No platform named 'badPlatform'");
	}
}
 
Example #6
Source File: Futures_Tests.java    From goclipse with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void test_result() throws Exception {
	super.test_result();
	

	IRunnableFuture2<Result<Object, RuntimeException>> future = IRunnableFuture2.toResultFuture(() -> { 
		throw new RuntimeException("xxx2");
	});
	
	submitToExecutor(future, ForkJoinPool.commonPool());
	Result<Object, RuntimeException> result = future.awaitResult();
	
	verifyThrows(() -> {
		result.get();
	}, RuntimeException.class, "xxx2");

}
 
Example #7
Source File: SendToKafkaTest.java    From metron with Apache License 2.0 6 votes vote down vote up
@Test
public void testWritesCorrectNumber() {
  ExecutorService executor = ForkJoinPool.commonPool();
  AtomicLong numSent = new AtomicLong(0);
  long expectedSent = 100;
  SendToKafka sender = new SendToKafka(null, expectedSent, 10, () -> "msg", executor, numSent, ThreadLocal.withInitial(() -> null) ) {
    @Override
    protected Future<?> sendToKafka(KafkaProducer producer, String kafkaTopic, String message) {
      assertEquals(message, "msg");
      return ForkJoinPool.commonPool().submit(() -> {
        numSent.incrementAndGet();
      });
    }
  };
  sender.run();
  assertEquals(numSent.get(), expectedSent);
}
 
Example #8
Source File: Chapter07Concurrency04.java    From Java-11-Cookbook-Second-Edition with MIT License 6 votes vote down vote up
private static void demo4_Flow_submissionPublisher() {
    System.out.println();

    ExecutorService execService =  ForkJoinPool.commonPool();//Executors.newFixedThreadPool(3);
    try (SubmissionPublisher<Integer> publisher = new SubmissionPublisher<>()){//execService, 1)){
        demoSubscribe(publisher, execService, "One");
        demoSubscribe(publisher, execService, "Two");
        demoSubscribe(publisher, execService, "Three");
        IntStream.range(1, 5).forEach(publisher::submit);
    } finally {
        try {
            execService.shutdown();
            int shutdownDelaySec = 1;
            System.out.println("Waiting for " + shutdownDelaySec + " sec before shutting down service...");
            execService.awaitTermination(shutdownDelaySec, TimeUnit.SECONDS);
        } catch (Exception ex) {
            System.out.println("Caught around execService.awaitTermination(): " + ex.getClass().getName());
        } finally {
            System.out.println("Calling execService.shutdownNow()...");
            List<Runnable> l = execService.shutdownNow();
            System.out.println(l.size() + " tasks were waiting to be executed. Service stopped.");
        }

    }

}
 
Example #9
Source File: LoggingTest.java    From gcp-ingestion with Mozilla Public License 2.0 6 votes vote down vote up
@Test
public void canWriteLogs() {
  final ListAppender appender = logs.getListAppender("STDOUT");

  // emit warning with exception from an async CompletableFuture
  CompletableFuture.runAsync(() -> LoggerFactory.getLogger(LoggingTest.class).warn("msg",
      new UncheckedIOException(new IOException("test"))), ForkJoinPool.commonPool()).join();
  assertThat(appender.getMessages(), containsInAnyOrder(allOf(containsString("LoggingTest"),
      containsString("\"level\":\"WARN\""), containsString("\"message\":\"msg\""), containsString(
          "\"extendedStackTrace\":\"java.io.UncheckedIOException: java.io.IOException: test"))));

  // emit error without exception from an async CompletableFuture
  appender.clear();
  LoggerFactory.getLogger(LoggingTest.class).error("test message");
  assertThat(appender.getMessages(),
      containsInAnyOrder(allOf(containsString("LoggingTest"),
          containsString("\"level\":\"ERROR\""), containsString("\"message\":\"test message\""),
          not(containsString("extendedStackTrace")))));
}
 
Example #10
Source File: SimpleOntologyManagerTest.java    From mobi with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void testActivate() {
    Map<String, Object> props = new HashMap<>();
    props.put("poolSize", 0);
    manager.modified(props);
    ForkJoinPool pool = Whitebox.getInternalState(manager, "threadPool");
    assertTrue(pool.getParallelism() > 0);
    props.put("poolSize", -1);
    manager.modified(props);
    pool = Whitebox.getInternalState(manager, "threadPool");
    assertEquals(pool.getParallelism(), 1);
    props.put("poolSize", 1);
    manager.modified(props);
    pool = Whitebox.getInternalState(manager, "threadPool");
    assertEquals(pool.getParallelism(), 1);
    props.put("poolSize", 2);
    manager.modified(props);
    pool = Whitebox.getInternalState(manager, "threadPool");
    assertEquals(pool.getParallelism(), 2);
}
 
Example #11
Source File: ResolveFuture.java    From effective-debugging with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    Path path = Paths.get(args[0]);
    // Create pool of 100 threads to compute results
    ForkJoinPool fjp = new ForkJoinPool(100);

    try {
        // Obtain list of lines
        List<CompletableFuture<String>> list = 
            Files.lines(path)
            // Map lines into a future task
            .map(line -> CompletableFuture.supplyAsync(
                        () -> addressName(line), fjp))
            // Collect future tasks into a list
            .collect(Collectors.toList());
        // Wait for tasks to complete, and print the result
        list.stream().map(CompletableFuture::join)
            .forEach(System.out::println);
    } catch (IOException e) {
        System.err.println("Failed: " + e);
    }
}
 
Example #12
Source File: PojoClient.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
private static void testContextClassLoaderIsNull() throws Exception {
  ForkJoinPool pool = new ForkJoinPool(4);
  pool.submit(() ->
      IntStream.range(0, 20).parallel().forEach(item -> {
        if (Thread.currentThread().getName().equals("main")) {
          return;
        }
        // in web environment, this could be null, here we just mock a null class loader.
        Thread.currentThread().setContextClassLoader(null);
        TestMgr.check(null, test.postTestStatic(2));
      })).get();
}
 
Example #13
Source File: ConcurrentHashMap1.java    From javacore with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
public static void main(String[] args) {
    System.out.println("Parallelism: " + ForkJoinPool.getCommonPoolParallelism());

    testForEach();
    testSearch();
    testReduce();
}
 
Example #14
Source File: ForkJoinPoolTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * timed invokeAll(null) throws NullPointerException
 */
public void testTimedInvokeAll1() throws Throwable {
    ExecutorService e = new ForkJoinPool(1);
    try (PoolCleaner cleaner = cleaner(e)) {
        try {
            e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
            shouldThrow();
        } catch (NullPointerException success) {}
    }
}
 
Example #15
Source File: ForkJoinPoolTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * timed invokeAny(c) throws NullPointerException if c has null elements
 */
public void testTimedInvokeAny3() throws Throwable {
    CountDownLatch latch = new CountDownLatch(1);
    ExecutorService e = new ForkJoinPool(1);
    try (PoolCleaner cleaner = cleaner(e)) {
        List<Callable<String>> l = new ArrayList<Callable<String>>();
        l.add(latchAwaitingStringTask(latch));
        l.add(null);
        try {
            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
            shouldThrow();
        } catch (NullPointerException success) {}
        latch.countDown();
    }
}
 
Example #16
Source File: FirstLight.java    From pgadba with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * create a Session and send a SQL to the database.
 */
@Test
public void sqlOperation() {
  DataSource ds = ConnectUtil.openDb(postgres);
  Session session = ds.getSession(t -> fail("ERROR: " + t.getMessage()));
  try (session) {
    assertNotNull(session);
    session.operation(TRIVIAL).submit();
  }
  ForkJoinPool.commonPool().awaitQuiescence(1, TimeUnit.MINUTES);
}
 
Example #17
Source File: RecursiveTaskTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private <T> T testInvokeOnPool(ForkJoinPool pool, RecursiveTask<T> a) {
    try (PoolCleaner cleaner = cleaner(pool)) {
        checkNotDone(a);

        T result = pool.invoke(a);

        checkCompletedNormally(a, result);
        return result;
    }
}
 
Example #18
Source File: ArrayPrefixHelpers.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/** Root task constructor */
public CumulateTask(CumulateTask<T> parent,
                    BinaryOperator<T> function,
                    T[] array, int lo, int hi) {
    super(parent);
    this.function = function; this.array = array;
    this.lo = this.origin = lo; this.hi = this.fence = hi;
    int p;
    this.threshold =
            (p = (hi - lo) / (ForkJoinPool.getCommonPoolParallelism() << 3))
            <= MIN_PARTITION ? MIN_PARTITION : p;
}
 
Example #19
Source File: ArrayPrefixHelpers.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/** Root task constructor */
public LongCumulateTask(LongCumulateTask parent,
                        LongBinaryOperator function,
                        long[] array, int lo, int hi) {
    super(parent);
    this.function = function; this.array = array;
    this.lo = this.origin = lo; this.hi = this.fence = hi;
    int p;
    this.threshold =
            (p = (hi - lo) / (ForkJoinPool.getCommonPoolParallelism() << 3))
            <= MIN_PARTITION ? MIN_PARTITION : p;
}
 
Example #20
Source File: AsyncResult.java    From incubator-tuweni with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a result that, after the given blocking function executes asynchronously on
 * {@link ForkJoinPool#commonPool()} and returns a result, completes when the returned result completes, with the same
 * value or exception.
 *
 * @param fn The function returning a result.
 * @param <T> The type of the returned result's value.
 * @return A new result.
 */
static <T> AsyncResult<T> executeBlocking(Supplier<T> fn) {
  requireNonNull(fn);
  CompletableAsyncResult<T> asyncResult = AsyncResult.incomplete();
  ForkJoinPool.commonPool().execute(() -> {
    try {
      asyncResult.complete(fn.get());
    } catch (Throwable ex) {
      asyncResult.completeExceptionally(ex);
    }
  });
  return asyncResult;
}
 
Example #21
Source File: CompletableFuture.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/**
 * Null-checks user executor argument, and translates uses of
 * commonPool to asyncPool in case parallelism disabled.
 */
static Executor screenExecutor(Executor e) {
    if (!useCommonPool && e == ForkJoinPool.commonPool())
        return asyncPool;
    if (e == null) throw new NullPointerException();
    return e;
}
 
Example #22
Source File: ForkJoinTask8Test.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testInvoke(ForkJoinPool pool) {
    RecursiveAction a = new CheckedRecursiveAction() {
        protected void realCompute() {
            AsyncFib f = new AsyncFib(8);
            assertNull(f.invoke());
            f.checkCompletedNormally();
        }};
    testInvokeOnPool(pool, a);
}
 
Example #23
Source File: ArrayPrefixHelpers.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/** Root task constructor */
public DoubleCumulateTask(DoubleCumulateTask parent,
                          DoubleBinaryOperator function,
                          double[] array, int lo, int hi) {
    super(parent);
    this.function = function; this.array = array;
    this.lo = this.origin = lo; this.hi = this.fence = hi;
    int p;
    this.threshold =
            (p = (hi - lo) / (ForkJoinPool.getCommonPoolParallelism() << 3))
            <= MIN_PARTITION ? MIN_PARTITION : p;
}
 
Example #24
Source File: FluxSubscribeOnTest.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Test
public void classicEmptyBackpressured() throws Exception {
	AssertSubscriber<Integer> ts = AssertSubscriber.create(0);

	Flux.<Integer>empty().subscribeOn(Schedulers.fromExecutorService(ForkJoinPool.commonPool())).subscribe(ts);

	ts.await(Duration.ofSeconds(5));

	ts.assertNoValues()
	.assertNoError()
	.assertComplete();
}
 
Example #25
Source File: ForkJoinPoolTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * A task submitted after shutdown is rejected
 */
public void testSubmitAfterShutdown() {
    ForkJoinPool p = new ForkJoinPool(1);
    try (PoolCleaner cleaner = cleaner(p)) {
        p.shutdown();
        assertTrue(p.isShutdown());
        try {
            ForkJoinTask<Integer> f = p.submit(new FibTask(8));
            shouldThrow();
        } catch (RejectedExecutionException success) {}
    }
}
 
Example #26
Source File: ArrayBase.java    From morpheus-core with Apache License 2.0 5 votes vote down vote up
@Override
public final Array<Long> mapToLongs(ToLongFunction<ArrayValue<T>> mapper) {
    final Array<Long> result = Array.of(Long.class, length());
    final MapValues<Long> action = new MapValues<>(0, length() - 1, mapper, result);
    if (isParallel()) {
        ForkJoinPool.commonPool().invoke(action);
        return result;
    } else {
        action.compute();
        return result;
    }
}
 
Example #27
Source File: PrimeNumbersUnitManualTest.java    From tutorials with MIT License 5 votes vote down vote up
@Benchmark
public void newWorkStealingPoolBenchmark() {
    PrimeNumbers primes = new PrimeNumbers(10000);
    int parallelism = ForkJoinPool.getCommonPoolParallelism();
    ForkJoinPool stealer = (ForkJoinPool) Executors.newWorkStealingPool(parallelism);
    stealer.invoke(primes);
    stealer.shutdown();
}
 
Example #28
Source File: TestAsyncTableScan.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Override
protected List<Result> doScan(Scan scan) throws Exception {
  AsyncTable<ScanResultConsumer> table =
    ASYNC_CONN.getTable(TABLE_NAME, ForkJoinPool.commonPool());
  SimpleScanResultConsumer consumer = new SimpleScanResultConsumer();
  table.scan(scan, consumer);
  List<Result> results = consumer.getAll();
  if (scan.getBatch() > 0) {
    results = convertFromBatchResult(results);
  }
  return results;
}
 
Example #29
Source File: TestDependencies.java    From spring-cloud-dataflow with Apache License 2.0 5 votes vote down vote up
@Bean
public AppRegistryController appRegistryController(
		Optional<StreamDefinitionRepository> streamDefinitionRepository,
		Optional<StreamService> streamService,
		AppRegistryService appRegistry,
		ApplicationConfigurationMetadataResolver metadataResolver,
		StreamDefinitionService streamDefinitionService) {
	return new AppRegistryController(streamDefinitionRepository, streamService, appRegistry, metadataResolver,
			new ForkJoinPool(2), streamDefinitionService);
}
 
Example #30
Source File: ArrayPrefixHelpers.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/** Root task constructor */
public CumulateTask(CumulateTask<T> parent,
                    BinaryOperator<T> function,
                    T[] array, int lo, int hi) {
    super(parent);
    this.function = function; this.array = array;
    this.lo = this.origin = lo; this.hi = this.fence = hi;
    int p;
    this.threshold =
            (p = (hi - lo) / (ForkJoinPool.getCommonPoolParallelism() << 3))
            <= MIN_PARTITION ? MIN_PARTITION : p;
}