import java.util.concurrent.*;

public class ThreadPoolOverloadVerification {
public static void main(String[] args) throws InterruptedException {
var gate = new CountDownLatch(1);
var pool = new ThreadPoolExecutor(
    2, 4, 30, TimeUnit.SECONDS,
    new ArrayBlockingQueue<>(2),
    new ThreadPoolExecutor.AbortPolicy());
Runnable task = () -> {
    try {
        gate.await();
    } catch (InterruptedException ex) {
        Thread.currentThread().interrupt();
    }
};
try {
    for (int i = 1; i <= 7; i++) {
        try {
            pool.execute(task);
        } catch (RejectedExecutionException ex) {
            System.out.println("rejected task " + i);
        }
    }
} finally {
    gate.countDown();
    pool.shutdown();
    if (!pool.awaitTermination(5, TimeUnit.SECONDS)) {
        pool.shutdownNow();
    }
}
}
}
