package p.studio;

import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

public final class StudioBackgroundTasks {
    private final ExecutorService executorService;

    public StudioBackgroundTasks(ExecutorService executorService) {
        this.executorService = Objects.requireNonNull(executorService, "executorService");
    }

    public Future<?> submit(Runnable task) {
        return executorService.submit(Objects.requireNonNull(task, "task"));
    }

    public void shutdown() {
        executorService.shutdown();
        try {
            if (!executorService.awaitTermination(2, TimeUnit.SECONDS)) {
                executorService.shutdownNow();
            }
        } catch (InterruptedException interruptedException) {
            executorService.shutdownNow();
            Thread.currentThread().interrupt();
        }
    }
}
