Java Code Examples for java.util.concurrent.ThreadPoolExecutor#getCompletedTaskCount()

The following examples show how to use java.util.concurrent.ThreadPoolExecutor#getCompletedTaskCount() . 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: ThreadUtil.java    From datawave with Apache License 2.0 6 votes vote down vote up
/**
 * Waits for all active threads in the thread pool to complete.
 *
 * @param log
 * @param executor
 * @param type
 * @param poolSize
 * @param workUnits
 * @param start
 * @return time taken to complete all tasks
 */
public static long waitForThreads(Logger log, ThreadPoolExecutor executor, String type, int poolSize, long workUnits, long start) {
    long cur = System.currentTimeMillis();
    int active = executor.getActiveCount();
    int qSize = executor.getQueue().size();
    long compl = executor.getCompletedTaskCount();
    long time = 0;
    while (((qSize > 0) || (active > 0) || (compl < workUnits)) && !executor.isTerminated()) {
        if (log != null && (time < (System.currentTimeMillis() - (1000L * 10L)))) {
            log.info(type + " running, T: " + active + "/" + poolSize + ", Completed: " + compl + "/" + workUnits + ", " + ", Remaining: " + qSize + ", "
                            + (cur - start) + " ms elapsed");
            time = System.currentTimeMillis();
        }
        cur = System.currentTimeMillis();
        active = executor.getActiveCount();
        qSize = executor.getQueue().size();
        compl = executor.getCompletedTaskCount();
    }
    if (log != null) {
        log.info("Finished Waiting for " + type + " running, T: " + active + "/" + poolSize + ", Completed: " + compl + "/" + workUnits + ", "
                        + ", Remaining: " + qSize + ", " + (cur - start) + " ms elapsed");
    }
    
    long stop = System.currentTimeMillis();
    return (stop - start);
}
 
Example 2
Source File: RollupTypeCacherTest.java    From blueflood with Apache License 2.0 6 votes vote down vote up
@Test
public void testCacherDoesCacheRollupTypeForPreAggregatedMetrics() throws Exception {
    MetricsCollection collection = createPreaggregatedTestMetrics();

    MetadataCache rollupTypeCache = mock(MetadataCache.class);
    ThreadPoolExecutor tpe =
            new ThreadPoolBuilder().withName("rtc test").build();

    RollupTypeCacher rollupTypeCacher = new RollupTypeCacher(tpe,
            rollupTypeCache);

    rollupTypeCacher.apply(collection);

    // wait till done
    while (tpe.getCompletedTaskCount() < 1) {
        Thread.sleep(1);
    }

    // Confirm that each metric is cached
    for (IMetric m : collection.toMetrics()) {
        verify(rollupTypeCache).put(m.getLocator(), cacheKey,
                m.getRollupType().toString());
    }
}
 
Example 3
Source File: ConnectChannelHandlerTest.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@Test
public void test_Connect_Blocked() throws RemotingException {
    handler = new ConnectionOrderedChannelHandler(new BizChannelHander(false), url);
    ThreadPoolExecutor executor = (ThreadPoolExecutor) getField(handler, "connectionExecutor", 1);
    Assert.assertEquals(1, executor.getMaximumPoolSize());

    int runs = 20;
    int taskCount = runs * 2;
    for (int i = 0; i < runs; i++) {
        handler.connected(new MockedChannel());
        handler.disconnected(new MockedChannel());
        Assert.assertTrue(executor.getActiveCount() + " must <=1", executor.getActiveCount() <= 1);
    }
    //queue.size 
    Assert.assertEquals(taskCount - 1, executor.getQueue().size());

    for (int i = 0; i < taskCount; i++) {
        if (executor.getCompletedTaskCount() < taskCount) {
            sleep(100);
        }
    }
    Assert.assertEquals(taskCount, executor.getCompletedTaskCount());
}
 
Example 4
Source File: RollupTypeCacherTest.java    From blueflood with Apache License 2.0 6 votes vote down vote up
@Test
public void testCacher() throws Exception {
    MetricsCollection collection = createTestData();

    MetadataCache rollupTypeCache = mock(MetadataCache.class);
    ThreadPoolExecutor tpe = 
        new ThreadPoolBuilder().withName("rtc test").build();

    RollupTypeCacher rollupTypeCacher = new RollupTypeCacher(tpe,
        rollupTypeCache);

    rollupTypeCacher.apply(collection);

    // wait till done
    while (tpe.getCompletedTaskCount() < 1) {
        Thread.sleep(1);
    }

    verifyZeroInteractions(rollupTypeCache);
}
 
Example 5
Source File: Schedulers.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
/**
 * Scan an {@link Executor} or {@link ExecutorService}, recognizing several special
 * implementations. Unwraps some decorating schedulers, recognizes {@link Scannable}
 * schedulers and delegates to their {@link Scannable#scanUnsafe(Scannable.Attr)}
 * method, introspects {@link ThreadPoolExecutor} instances.
 * <p>
 * If no data can be extracted, defaults to the provided {@code orElse}
 * {@link Scannable#scanUnsafe(Scannable.Attr) scanUnsafe}.
 *
 * @param executor the executor to introspect in a best effort manner.
 * @param key the key to scan for. CAPACITY and BUFFERED mainly.
 * @return an equivalent of {@link Scannable#scanUnsafe(Scannable.Attr)} but that can
 * also work on some implementations of {@link Executor}
 */
@Nullable
static final Object scanExecutor(Executor executor, Scannable.Attr key) {
	if (executor instanceof DelegateServiceScheduler.UnsupportedScheduledExecutorService) {
		executor = ((DelegateServiceScheduler.UnsupportedScheduledExecutorService) executor).get();
	}
	if (executor instanceof Scannable) {
		return ((Scannable) executor).scanUnsafe(key);
	}

	if (executor instanceof ExecutorService) {
		ExecutorService service = (ExecutorService) executor;
		if (key == Scannable.Attr.TERMINATED) return service.isTerminated();
		if (key == Scannable.Attr.CANCELLED) return service.isShutdown();
	}

	if (executor instanceof ThreadPoolExecutor) {
			final ThreadPoolExecutor poolExecutor = (ThreadPoolExecutor) executor;
			if (key == Scannable.Attr.CAPACITY) return poolExecutor.getMaximumPoolSize();
			if (key == Scannable.Attr.BUFFERED) return ((Long) (poolExecutor.getTaskCount() - poolExecutor.getCompletedTaskCount())).intValue();
			if (key == Scannable.Attr.LARGE_BUFFERED) return poolExecutor.getTaskCount() - poolExecutor.getCompletedTaskCount();
	}

	return null;
}
 
Example 6
Source File: TypeAndUnitProcessorTest.java    From blueflood with Apache License 2.0 6 votes vote down vote up
@Test
public void test() throws Exception {
    MetricsCollection collection = createTestData();

    IncomingMetricMetadataAnalyzer metricMetadataAnalyzer =
        mock(IncomingMetricMetadataAnalyzer.class);

    ThreadPoolExecutor tpe = 
        new ThreadPoolBuilder().withName("rtc test").build();

    TypeAndUnitProcessor typeAndUnitProcessor = new TypeAndUnitProcessor(
            tpe, metricMetadataAnalyzer);

    typeAndUnitProcessor.apply(collection);

    // wait till done
    while (tpe.getCompletedTaskCount() < 1) {
        Thread.sleep(1);
    }

    verify(metricMetadataAnalyzer).scanMetrics(collection.toMetrics());
}
 
Example 7
Source File: ConnectChannelHandlerTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
public void test_Connect_Blocked() throws RemotingException{
    handler = new ConnectionOrderedChannelHandler(new BizChannelHander(false), url);
    ThreadPoolExecutor executor = (ThreadPoolExecutor)getField(handler, "connectionExecutor", 1);
    Assert.assertEquals(1, executor.getMaximumPoolSize());
    
    int runs = 20;
    int taskCount = runs * 2;
    for(int i=0; i<runs;i++){
        handler.connected(new MockedChannel());
        handler.disconnected(new MockedChannel());
        Assert.assertTrue(executor.getActiveCount() + " must <=1" ,executor.getActiveCount() <= 1);
    }
    //queue.size 
    Assert.assertEquals(taskCount -1 , executor.getQueue().size());
    
    for( int i=0;i<taskCount; i++){
        if (executor.getCompletedTaskCount() < taskCount){
            sleep(100);
        }
    }
    Assert.assertEquals(taskCount, executor.getCompletedTaskCount());        
}
 
Example 8
Source File: ConnectChannelHandlerTest.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
@Test
public void test_Connect_Blocked() throws RemotingException{
    handler = new ConnectionOrderedChannelHandler(new BizChannelHander(false), url);
    ThreadPoolExecutor executor = (ThreadPoolExecutor)getField(handler, "connectionExecutor", 1);
    Assert.assertEquals(1, executor.getMaximumPoolSize());
    
    int runs = 20;
    int taskCount = runs * 2;
    for(int i=0; i<runs;i++){
        handler.connected(new MockedChannel());
        handler.disconnected(new MockedChannel());
        Assert.assertTrue(executor.getActiveCount() + " must <=1" ,executor.getActiveCount() <= 1);
    }
    //queue.size 
    Assert.assertEquals(taskCount -1 , executor.getQueue().size());
    
    for( int i=0;i<taskCount; i++){
        if (executor.getCompletedTaskCount() < taskCount){
            sleep(100);
        }
    }
    Assert.assertEquals(taskCount, executor.getCompletedTaskCount());        
}
 
Example 9
Source File: ThreadPoolExecutorTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * getCompletedTaskCount increases, but doesn't overestimate,
 * when tasks complete
 */
public void testGetCompletedTaskCount() throws InterruptedException {
    final ThreadPoolExecutor p =
        new ThreadPoolExecutor(2, 2,
                               LONG_DELAY_MS, MILLISECONDS,
                               new ArrayBlockingQueue<Runnable>(10));
    try (PoolCleaner cleaner = cleaner(p)) {
        final CountDownLatch threadStarted = new CountDownLatch(1);
        final CountDownLatch threadProceed = new CountDownLatch(1);
        final CountDownLatch threadDone = new CountDownLatch(1);
        assertEquals(0, p.getCompletedTaskCount());
        p.execute(new CheckedRunnable() {
            public void realRun() throws InterruptedException {
                threadStarted.countDown();
                assertEquals(0, p.getCompletedTaskCount());
                threadProceed.await();
                threadDone.countDown();
            }});
        await(threadStarted);
        assertEquals(0, p.getCompletedTaskCount());
        threadProceed.countDown();
        threadDone.await();
        long startTime = System.nanoTime();
        while (p.getCompletedTaskCount() != 1) {
            if (millisElapsedSince(startTime) > LONG_DELAY_MS)
                fail("timed out");
            Thread.yield();
        }
    }
}
 
Example 10
Source File: ScheduledExecutorTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * getCompletedTaskCount increases, but doesn't overestimate,
 * when tasks complete
 */
public void testGetCompletedTaskCount() throws InterruptedException {
    final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(2);
    try (PoolCleaner cleaner = cleaner(p)) {
        final CountDownLatch threadStarted = new CountDownLatch(1);
        final CountDownLatch threadProceed = new CountDownLatch(1);
        final CountDownLatch threadDone = new CountDownLatch(1);
        assertEquals(0, p.getCompletedTaskCount());
        p.execute(new CheckedRunnable() {
            public void realRun() throws InterruptedException {
                threadStarted.countDown();
                assertEquals(0, p.getCompletedTaskCount());
                threadProceed.await();
                threadDone.countDown();
            }});
        await(threadStarted);
        assertEquals(0, p.getCompletedTaskCount());
        threadProceed.countDown();
        threadDone.await();
        long startTime = System.nanoTime();
        while (p.getCompletedTaskCount() != 1) {
            if (millisElapsedSince(startTime) > LONG_DELAY_MS)
                fail("timed out");
            Thread.yield();
        }
    }
}
 
Example 11
Source File: ScheduledExecutorSubclassTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * getCompletedTaskCount increases, but doesn't overestimate,
 * when tasks complete
 */
public void testGetCompletedTaskCount() throws InterruptedException {
    final ThreadPoolExecutor p = new CustomExecutor(2);
    try (PoolCleaner cleaner = cleaner(p)) {
        final CountDownLatch threadStarted = new CountDownLatch(1);
        final CountDownLatch threadProceed = new CountDownLatch(1);
        final CountDownLatch threadDone = new CountDownLatch(1);
        assertEquals(0, p.getCompletedTaskCount());
        p.execute(new CheckedRunnable() {
            public void realRun() throws InterruptedException {
                threadStarted.countDown();
                assertEquals(0, p.getCompletedTaskCount());
                threadProceed.await();
                threadDone.countDown();
            }});
        await(threadStarted);
        assertEquals(0, p.getCompletedTaskCount());
        threadProceed.countDown();
        threadDone.await();
        long startTime = System.nanoTime();
        while (p.getCompletedTaskCount() != 1) {
            if (millisElapsedSince(startTime) > LONG_DELAY_MS)
                fail("timed out");
            Thread.yield();
        }
    }
}
 
Example 12
Source File: ThreadPoolExecutorSubclassTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * getCompletedTaskCount increases, but doesn't overestimate,
 * when tasks complete
 */
public void testGetCompletedTaskCount() throws InterruptedException {
    final ThreadPoolExecutor p =
        new CustomTPE(2, 2,
                      LONG_DELAY_MS, MILLISECONDS,
                      new ArrayBlockingQueue<Runnable>(10));
    try (PoolCleaner cleaner = cleaner(p)) {
        final CountDownLatch threadStarted = new CountDownLatch(1);
        final CountDownLatch threadProceed = new CountDownLatch(1);
        final CountDownLatch threadDone = new CountDownLatch(1);
        assertEquals(0, p.getCompletedTaskCount());
        p.execute(new CheckedRunnable() {
            public void realRun() throws InterruptedException {
                threadStarted.countDown();
                assertEquals(0, p.getCompletedTaskCount());
                threadProceed.await();
                threadDone.countDown();
            }});
        await(threadStarted);
        assertEquals(0, p.getCompletedTaskCount());
        threadProceed.countDown();
        threadDone.await();
        long startTime = System.nanoTime();
        while (p.getCompletedTaskCount() != 1) {
            if (millisElapsedSince(startTime) > LONG_DELAY_MS)
                fail("timed out");
            Thread.yield();
        }
    }
}
 
Example 13
Source File: FsDatasetAsyncDiskService.java    From big-c with Apache License 2.0 5 votes vote down vote up
synchronized long countPendingDeletions() {
  long count = 0;
  for (ThreadPoolExecutor exec : executors.values()) {
    count += exec.getTaskCount() - exec.getCompletedTaskCount();
  }
  return count;
}
 
Example 14
Source File: TaskQueueImpl.java    From Android-Next with Apache License 2.0 5 votes vote down vote up
private static void logExecutor(final String name, final ThreadPoolExecutor executor) {
    final int corePoolSize = executor.getCorePoolSize();
    final int poolSize = executor.getPoolSize();
    final int activeCount = executor.getActiveCount();
    final long taskCount = executor.getTaskCount();
    final long completedCount = executor.getCompletedTaskCount();
    final boolean isShutdown = executor.isShutdown();
    final boolean isTerminated = executor.isTerminated();
    Log.v(TAG, name + " CorePoolSize:" + corePoolSize + " PoolSize:" + poolSize);
    Log.v(TAG, name + " isShutdown:" + isShutdown + " isTerminated:" + isTerminated);
    Log.v(TAG, name + " activeCount:" + activeCount + " taskCount:" + taskCount
            + " completedCount:" + completedCount);
}
 
Example 15
Source File: ConfigChanges.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
static void awaitIdleness(ThreadPoolExecutor tpe, long taskCount) {
    restart: for (;;) {
        // check twice to make chance of race vanishingly small
        for (int i = 0; i < 2; i++) {
            if (tpe.getQueue().size() != 0 ||
                tpe.getActiveCount() != 0 ||
                tpe.getCompletedTaskCount() != taskCount) {
                Thread.yield();
                continue restart;
            }
        }
        return;
    }
}
 
Example 16
Source File: ThreadPoolExecutorSubclassTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * getCompletedTaskCount increases, but doesn't overestimate,
 * when tasks complete
 */
public void testGetCompletedTaskCount() throws InterruptedException {
    final ThreadPoolExecutor p =
        new CustomTPE(2, 2,
                      LONG_DELAY_MS, MILLISECONDS,
                      new ArrayBlockingQueue<Runnable>(10));
    try (PoolCleaner cleaner = cleaner(p)) {
        final CountDownLatch threadStarted = new CountDownLatch(1);
        final CountDownLatch threadProceed = new CountDownLatch(1);
        final CountDownLatch threadDone = new CountDownLatch(1);
        assertEquals(0, p.getCompletedTaskCount());
        p.execute(new CheckedRunnable() {
            public void realRun() throws InterruptedException {
                threadStarted.countDown();
                assertEquals(0, p.getCompletedTaskCount());
                threadProceed.await();
                threadDone.countDown();
            }});
        await(threadStarted);
        assertEquals(0, p.getCompletedTaskCount());
        threadProceed.countDown();
        threadDone.await();
        long startTime = System.nanoTime();
        while (p.getCompletedTaskCount() != 1) {
            if (millisElapsedSince(startTime) > LONG_DELAY_MS)
                fail("timed out");
            Thread.yield();
        }
    }
}
 
Example 17
Source File: ScheduledExecutorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * getCompletedTaskCount increases, but doesn't overestimate,
 * when tasks complete
 */
public void testGetCompletedTaskCount() throws InterruptedException {
    final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(2);
    try (PoolCleaner cleaner = cleaner(p)) {
        final CountDownLatch threadStarted = new CountDownLatch(1);
        final CountDownLatch threadProceed = new CountDownLatch(1);
        final CountDownLatch threadDone = new CountDownLatch(1);
        assertEquals(0, p.getCompletedTaskCount());
        p.execute(new CheckedRunnable() {
            public void realRun() throws InterruptedException {
                threadStarted.countDown();
                assertEquals(0, p.getCompletedTaskCount());
                threadProceed.await();
                threadDone.countDown();
            }});
        await(threadStarted);
        assertEquals(0, p.getCompletedTaskCount());
        threadProceed.countDown();
        threadDone.await();
        long startTime = System.nanoTime();
        while (p.getCompletedTaskCount() != 1) {
            if (millisElapsedSince(startTime) > LONG_DELAY_MS)
                fail("timed out");
            Thread.yield();
        }
    }
}
 
Example 18
Source File: ScheduledExecutorSubclassTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * getCompletedTaskCount increases, but doesn't overestimate,
 * when tasks complete
 */
public void testGetCompletedTaskCount() throws InterruptedException {
    final ThreadPoolExecutor p = new CustomExecutor(2);
    try (PoolCleaner cleaner = cleaner(p)) {
        final CountDownLatch threadStarted = new CountDownLatch(1);
        final CountDownLatch threadProceed = new CountDownLatch(1);
        final CountDownLatch threadDone = new CountDownLatch(1);
        assertEquals(0, p.getCompletedTaskCount());
        p.execute(new CheckedRunnable() {
            public void realRun() throws InterruptedException {
                threadStarted.countDown();
                assertEquals(0, p.getCompletedTaskCount());
                threadProceed.await();
                threadDone.countDown();
            }});
        await(threadStarted);
        assertEquals(0, p.getCompletedTaskCount());
        threadProceed.countDown();
        threadDone.await();
        long startTime = System.nanoTime();
        while (p.getCompletedTaskCount() != 1) {
            if (millisElapsedSince(startTime) > LONG_DELAY_MS)
                fail("timed out");
            Thread.yield();
        }
    }
}
 
Example 19
Source File: ThreadPoolExecutorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * getCompletedTaskCount increases, but doesn't overestimate,
 * when tasks complete
 */
public void testGetCompletedTaskCount() throws InterruptedException {
    final ThreadPoolExecutor p =
        new ThreadPoolExecutor(2, 2,
                               LONG_DELAY_MS, MILLISECONDS,
                               new ArrayBlockingQueue<Runnable>(10));
    try (PoolCleaner cleaner = cleaner(p)) {
        final CountDownLatch threadStarted = new CountDownLatch(1);
        final CountDownLatch threadProceed = new CountDownLatch(1);
        final CountDownLatch threadDone = new CountDownLatch(1);
        assertEquals(0, p.getCompletedTaskCount());
        p.execute(new CheckedRunnable() {
            public void realRun() throws InterruptedException {
                threadStarted.countDown();
                assertEquals(0, p.getCompletedTaskCount());
                threadProceed.await();
                threadDone.countDown();
            }});
        await(threadStarted);
        assertEquals(0, p.getCompletedTaskCount());
        threadProceed.countDown();
        threadDone.await();
        long startTime = System.nanoTime();
        while (p.getCompletedTaskCount() != 1) {
            if (millisElapsedSince(startTime) > LONG_DELAY_MS)
                fail("timed out");
            Thread.yield();
        }
    }
}
 
Example 20
Source File: PerformanceTestCase.java    From esigate with Apache License 2.0 4 votes vote down vote up
/**
 * Execute la tache avec plusieurs Threads
 * 
 * @param request
 * @return
 * @throws Exception
 */
private long execute(HttpGetRequestRunnable request, int numberOfRequests, int threads) throws Exception {
    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
    httpClient =
            HttpClientBuilder
                    .create()
                    .setConnectionManager(connectionManager)
                    .setMaxConnTotal(threads)
                    .setMaxConnPerRoute(threads)
                    .setDefaultRequestConfig(
                            RequestConfig.custom().setConnectTimeout(10000).setSocketTimeout(10000).build())
                    .build();
    // Warm up
    request.run();

    BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();
    ThreadPoolExecutor threadPool = new ThreadPoolExecutor(threads, threads, 5, TimeUnit.SECONDS, queue);

    long start = System.currentTimeMillis();
    threadPool.prestartAllCoreThreads();
    for (int i = 0; i < numberOfRequests; i++) {
        threadPool.submit(request);
    }
    threadPool.shutdown();

    // wait maximum 20 s
    threadPool.awaitTermination(200, TimeUnit.SECONDS);
    connectionManager.shutdown();

    if (request.exception != null) {
        throw new AssertionFailedError("Exception for request " + request.url + " after " + request.count
                + " requests", request.exception);
    }
    if (threadPool.getCompletedTaskCount() < threadPool.getTaskCount()) {
        // All task were not executed
        String msg =
                request.url + " : Only " + threadPool.getCompletedTaskCount() + "/" + threadPool.getTaskCount()
                        + " have been renderered " + " => Maybe a performance issue";
        threadPool.shutdownNow();
        fail(msg);
    }

    long end = System.currentTimeMillis();
    long execTime = end - start;
    LOG.debug("Executed request " + request.url + " " + numberOfRequests + " times with " + threads
            + " threads in " + execTime + "ms");
    return execTime;

}