Java Code Examples for java.util.concurrent.CompletionService#poll()

The following examples show how to use java.util.concurrent.CompletionService#poll() . 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: LoggingRegistrySingltonTest.java    From hop with Apache License 2.0 6 votes vote down vote up
/**
 * Test that LoggingRegistry is concurrent-sage initialized over multiple calls. Creating more than 1000 threads can
 * cause significant performance impact.
 *
 * @throws InterruptedException
 * @throws ExecutionException
 */
@Test( timeout = 30000 )
public void testLoggingRegistryConcurrentInitialization() throws InterruptedException, ExecutionException {
  CountDownLatch start = new CountDownLatch( 1 );

  int count = 10;
  CompletionService<LoggingRegistry> drover = registerHounds( count, start );
  // fire!
  start.countDown();

  Set<LoggingRegistry> distinct = new HashSet<LoggingRegistry>();

  int i = 0;
  while ( i < count ) {
    Future<LoggingRegistry> complete = drover.poll( 15, TimeUnit.SECONDS );
    LoggingRegistry instance = complete.get();
    distinct.add( instance );
    i++;
  }
  Assert.assertEquals( "Only one singlton instance ;)", 1, distinct.size() );
}
 
Example 2
Source File: ExecutorCompletionServiceTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * poll returns non-null when the returned task is completed
 */
public void testPoll1()
    throws InterruptedException, ExecutionException {
    CompletionService cs = new ExecutorCompletionService(cachedThreadPool);
    assertNull(cs.poll());
    cs.submit(new StringTask());

    long startTime = System.nanoTime();
    Future f;
    while ((f = cs.poll()) == null) {
        if (millisElapsedSince(startTime) > LONG_DELAY_MS)
            fail("timed out");
        Thread.yield();
    }
    assertTrue(f.isDone());
    assertSame(TEST_STRING, f.get());
}
 
Example 3
Source File: ExecutorCompletionServiceTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * timed poll returns non-null when the returned task is completed
 */
public void testPoll2()
    throws InterruptedException, ExecutionException {
    CompletionService cs = new ExecutorCompletionService(cachedThreadPool);
    assertNull(cs.poll());
    cs.submit(new StringTask());

    long startTime = System.nanoTime();
    Future f;
    while ((f = cs.poll(SHORT_DELAY_MS, MILLISECONDS)) == null) {
        if (millisElapsedSince(startTime) > LONG_DELAY_MS)
            fail("timed out");
        Thread.yield();
    }
    assertTrue(f.isDone());
    assertSame(TEST_STRING, f.get());
}
 
Example 4
Source File: LoggingRegistrySingltonTest.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
/**
 * Test that LoggingRegistry is concurrent-sage initialized over multiple calls. Creating more than 1000 threads can
 * cause significant performance impact.
 * 
 * @throws InterruptedException
 * @throws ExecutionException
 * 
 */
@Test( timeout = 30000 )
public void testLoggingRegistryConcurrentInitialization() throws InterruptedException, ExecutionException {
  CountDownLatch start = new CountDownLatch( 1 );

  int count = 10;
  CompletionService<LoggingRegistry> drover = registerHounds( count, start );
  // fire!
  start.countDown();

  Set<LoggingRegistry> distinct = new HashSet<LoggingRegistry>();

  int i = 0;
  while ( i < count ) {
    Future<LoggingRegistry> complete = drover.poll( 15, TimeUnit.SECONDS );
    LoggingRegistry instance = complete.get();
    distinct.add( instance );
    i++;
  }
  Assert.assertEquals( "Only one singlton instance ;)", 1, distinct.size() );
}
 
Example 5
Source File: ColumnCardinalityCache.java    From presto with Apache License 2.0 4 votes vote down vote up
/**
 * Gets the cardinality for each {@link AccumuloColumnConstraint}.
 * Given constraints are expected to be indexed! Who knows what would happen if they weren't!
 *
 * @param schema Schema name
 * @param table Table name
 * @param auths Scan authorizations
 * @param idxConstraintRangePairs Mapping of all ranges for a given constraint
 * @param earlyReturnThreshold Smallest acceptable cardinality to return early while other tasks complete
 * @param pollingDuration Duration for polling the cardinality completion service
 * @return An immutable multimap of cardinality to column constraint, sorted by cardinality from smallest to largest
 * @throws TableNotFoundException If the metrics table does not exist
 * @throws ExecutionException If another error occurs; I really don't even know anymore.
 */
public Multimap<Long, AccumuloColumnConstraint> getCardinalities(String schema, String table, Authorizations auths, Multimap<AccumuloColumnConstraint, Range> idxConstraintRangePairs, long earlyReturnThreshold, Duration pollingDuration)
{
    // Submit tasks to the executor to fetch column cardinality, adding it to the Guava cache if necessary
    CompletionService<Pair<Long, AccumuloColumnConstraint>> executor = new ExecutorCompletionService<>(executorService);
    idxConstraintRangePairs.asMap().forEach((key, value) -> executor.submit(() -> {
        long cardinality = getColumnCardinality(schema, table, auths, key.getFamily(), key.getQualifier(), value);
        LOG.debug("Cardinality for column %s is %s", key.getName(), cardinality);
        return Pair.of(cardinality, key);
    }));

    // Create a multi map sorted by cardinality
    ListMultimap<Long, AccumuloColumnConstraint> cardinalityToConstraints = MultimapBuilder.treeKeys().arrayListValues().build();
    try {
        boolean earlyReturn = false;
        int numTasks = idxConstraintRangePairs.asMap().entrySet().size();
        do {
            // Sleep for the polling duration to allow concurrent tasks to run for this time
            Thread.sleep(pollingDuration.toMillis());

            // Poll each task, retrieving the result if it is done
            for (int i = 0; i < numTasks; ++i) {
                Future<Pair<Long, AccumuloColumnConstraint>> futureCardinality = executor.poll();
                if (futureCardinality != null && futureCardinality.isDone()) {
                    Pair<Long, AccumuloColumnConstraint> columnCardinality = futureCardinality.get();
                    cardinalityToConstraints.put(columnCardinality.getLeft(), columnCardinality.getRight());
                }
            }

            // If the smallest cardinality is present and below the threshold, set the earlyReturn flag
            Optional<Entry<Long, AccumuloColumnConstraint>> smallestCardinality = cardinalityToConstraints.entries().stream().findFirst();
            if (smallestCardinality.isPresent()) {
                if (smallestCardinality.get().getKey() <= earlyReturnThreshold) {
                    LOG.info("Cardinality %s, is below threshold. Returning early while other tasks finish", smallestCardinality);
                    earlyReturn = true;
                }
            }
        }
        while (!earlyReturn && cardinalityToConstraints.entries().size() < numTasks);
    }
    catch (ExecutionException | InterruptedException e) {
        if (e instanceof InterruptedException) {
            Thread.currentThread().interrupt();
        }
        throw new PrestoException(UNEXPECTED_ACCUMULO_ERROR, "Exception when getting cardinality", e);
    }

    // Create a copy of the cardinalities
    return ImmutableMultimap.copyOf(cardinalityToConstraints);
}