import java.util.concurrent.*;

public class Main {
    public static void main(String[] args) {
        BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>(1);
        ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 10, 60, TimeUnit.SECONDS, queue);

        // Run 11 threads = maxPoolSize + queueCapacity (1)
        run11Threads(executor, "First ");

        sleep("wait");
        // wait until the end
        while (executor.getActiveCount() > 0);
        System.out.println("First 11 finished");

        run11Threads(executor, "Second ");
    }

    private static void run11Threads(ThreadPoolExecutor executor, String name) {
        for (int i = 0; i < 11; i++) {
            int finalI = i;
            try {
                executor.execute(() -> sleep(name + finalI));
            } catch (RejectedExecutionException e) {
                System.out.println(name + finalI + " rejected");
            }
        }
    }

    private static void sleep(String name) {
        try {
            System.out.println("Start " + name);
            Thread.sleep(1_000);
            System.out.println("End " + name);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

}