java.util.concurrent.ThreadPoolExecutor.DiscardOldestPolicy Java Examples

The following examples show how to use java.util.concurrent.ThreadPoolExecutor.DiscardOldestPolicy. 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 Project: tutorials   Author: eugenp   File: SaturationPolicyUnitTest.java    License: MIT License 5 votes vote down vote up
@Test
public void givenDiscardOldestPolicy_WhenSaturated_ThenExecutorDiscardsTheOldestTask() {
    executor = new ThreadPoolExecutor(1, 1, 0, MILLISECONDS, new ArrayBlockingQueue<>(2), new DiscardOldestPolicy());
    executor.execute(() -> waitFor(100));

    BlockingQueue<String> queue = new LinkedBlockingDeque<>();
    executor.execute(() -> queue.offer("First"));
    executor.execute(() -> queue.offer("Second"));
    executor.execute(() -> queue.offer("Third"));

    waitFor(150);
    List<String> results = new ArrayList<>();
    queue.drainTo(results);
    assertThat(results).containsExactlyInAnyOrder("Second", "Third");
}