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

The following examples show how to use java.util.concurrent.ThreadPoolExecutor#purge() . 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: ThreadPoolExecutorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * purge removes cancelled tasks from the queue
 */
public void testPurge() throws InterruptedException {
    final CountDownLatch threadStarted = new CountDownLatch(1);
    final CountDownLatch done = new CountDownLatch(1);
    final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10);
    final ThreadPoolExecutor p =
        new ThreadPoolExecutor(1, 1,
                               LONG_DELAY_MS, MILLISECONDS,
                               q);
    try (PoolCleaner cleaner = cleaner(p, done)) {
        FutureTask[] tasks = new FutureTask[5];
        for (int i = 0; i < tasks.length; i++) {
            Callable task = new CheckedCallable<Boolean>() {
                public Boolean realCall() throws InterruptedException {
                    threadStarted.countDown();
                    await(done);
                    return Boolean.TRUE;
                }};
            tasks[i] = new FutureTask(task);
            p.execute(tasks[i]);
        }
        await(threadStarted);
        assertEquals(tasks.length, p.getTaskCount());
        assertEquals(tasks.length - 1, q.size());
        assertEquals(1L, p.getActiveCount());
        assertEquals(0L, p.getCompletedTaskCount());
        tasks[4].cancel(true);
        tasks[3].cancel(false);
        p.purge();
        assertEquals(tasks.length - 3, q.size());
        assertEquals(tasks.length - 2, p.getTaskCount());
        p.purge();         // Nothing to do
        assertEquals(tasks.length - 3, q.size());
        assertEquals(tasks.length - 2, p.getTaskCount());
    }
}
 
Example 2
Source File: ThreadPoolExecutorSubclassTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * purge removes cancelled tasks from the queue
 */
public void testPurge() throws InterruptedException {
    final CountDownLatch threadStarted = new CountDownLatch(1);
    final CountDownLatch done = new CountDownLatch(1);
    final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10);
    final ThreadPoolExecutor p =
        new CustomTPE(1, 1,
                      LONG_DELAY_MS, MILLISECONDS,
                      q);
    try (PoolCleaner cleaner = cleaner(p, done)) {
        FutureTask[] tasks = new FutureTask[5];
        for (int i = 0; i < tasks.length; i++) {
            Callable<Boolean> task = new CheckedCallable<Boolean>() {
                public Boolean realCall() throws InterruptedException {
                    threadStarted.countDown();
                    await(done);
                    return Boolean.TRUE;
                }};
            tasks[i] = new FutureTask(task);
            p.execute(tasks[i]);
        }
        await(threadStarted);
        assertEquals(tasks.length, p.getTaskCount());
        assertEquals(tasks.length - 1, q.size());
        assertEquals(1L, p.getActiveCount());
        assertEquals(0L, p.getCompletedTaskCount());
        tasks[4].cancel(true);
        tasks[3].cancel(false);
        p.purge();
        assertEquals(tasks.length - 3, q.size());
        assertEquals(tasks.length - 2, p.getTaskCount());
        p.purge();         // Nothing to do
        assertEquals(tasks.length - 3, q.size());
        assertEquals(tasks.length - 2, p.getTaskCount());
    }
}
 
Example 3
Source File: ThreadPoolExecutorTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * purge removes cancelled tasks from the queue
 */
public void testPurge() throws InterruptedException {
    final CountDownLatch threadStarted = new CountDownLatch(1);
    final CountDownLatch done = new CountDownLatch(1);
    final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
    final ThreadPoolExecutor p =
        new ThreadPoolExecutor(1, 1,
                               LONG_DELAY_MS, MILLISECONDS,
                               q);
    try (PoolCleaner cleaner = cleaner(p, done)) {
        FutureTask[] tasks = new FutureTask[5];
        for (int i = 0; i < tasks.length; i++) {
            Callable task = new CheckedCallable<Boolean>() {
                public Boolean realCall() throws InterruptedException {
                    threadStarted.countDown();
                    await(done);
                    return Boolean.TRUE;
                }};
            tasks[i] = new FutureTask(task);
            p.execute(tasks[i]);
        }
        await(threadStarted);
        assertEquals(tasks.length, p.getTaskCount());
        assertEquals(tasks.length - 1, q.size());
        assertEquals(1L, p.getActiveCount());
        assertEquals(0L, p.getCompletedTaskCount());
        tasks[4].cancel(true);
        tasks[3].cancel(false);
        p.purge();
        assertEquals(tasks.length - 3, q.size());
        assertEquals(tasks.length - 2, p.getTaskCount());
        p.purge();         // Nothing to do
        assertEquals(tasks.length - 3, q.size());
        assertEquals(tasks.length - 2, p.getTaskCount());
    }
}
 
Example 4
Source File: ThreadPoolExecutorSubclassTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * purge removes cancelled tasks from the queue
 */
public void testPurge() throws InterruptedException {
    final CountDownLatch threadStarted = new CountDownLatch(1);
    final CountDownLatch done = new CountDownLatch(1);
    final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
    final ThreadPoolExecutor p =
        new CustomTPE(1, 1,
                      LONG_DELAY_MS, MILLISECONDS,
                      q);
    try (PoolCleaner cleaner = cleaner(p, done)) {
        FutureTask[] tasks = new FutureTask[5];
        for (int i = 0; i < tasks.length; i++) {
            Callable task = new CheckedCallable<Boolean>() {
                public Boolean realCall() throws InterruptedException {
                    threadStarted.countDown();
                    await(done);
                    return Boolean.TRUE;
                }};
            tasks[i] = new FutureTask(task);
            p.execute(tasks[i]);
        }
        await(threadStarted);
        assertEquals(tasks.length, p.getTaskCount());
        assertEquals(tasks.length - 1, q.size());
        assertEquals(1L, p.getActiveCount());
        assertEquals(0L, p.getCompletedTaskCount());
        tasks[4].cancel(true);
        tasks[3].cancel(false);
        p.purge();
        assertEquals(tasks.length - 3, q.size());
        assertEquals(tasks.length - 2, p.getTaskCount());
        p.purge();         // Nothing to do
        assertEquals(tasks.length - 3, q.size());
        assertEquals(tasks.length - 2, p.getTaskCount());
    }
}
 
Example 5
Source File: PurgeTask.java    From vethrfolnir-mu with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void run()
{
	for(ThreadPoolExecutor serv : _services)
		serv.purge();
}