import java.util.List;
import java.util.concurrent.*;

class Scratch {

    static final int TIME_OUT = 1000;
    static final TimeUnit TIME_OUT_UNIT = TimeUnit.MILLISECONDS;
    static final int TASK_SLEEP = 2000;

    public static void main(String[] args) {

        System.out.println("JVM: " + System.getProperty("java.version"));

        ExecutorService threadPool = new ForkJoinPool(3); // incorrect behaviour in Java 17
// ExecutorService threadPool = Executors.newWorkStealingPool(); // incorrect behaviour in Java 17
// ExecutorService threadPool = Executors.newFixedThreadPool(3); // works in Java 17

        Callable<String> simpleCallableTask = () -> {
            Thread.sleep(TASK_SLEEP); // task duration
            return "Return task result";
        };

        List<Callable<String>> callableList = List.of(simpleCallableTask, simpleCallableTask, simpleCallableTask);

        try {
            List<Future<String>> futureList = threadPool.invokeAll(callableList, TIME_OUT, TIME_OUT_UNIT);
            for (Future<String> future : futureList) {
                if (future.isCancelled()) {
                    System.out.println("TASK TIMED OUT");
                } else {
                    System.out.println("TASK SUCCESSFULLY EXECUTED");
                }
            }
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
} 