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:    License: MIT License 5 votes vote down vote up
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"));

    List<String> results = new ArrayList<>();
    assertThat(results).containsExactlyInAnyOrder("Second", "Third");