- Loading...
...
An ExecutorService can be wrapped with a deadline so that its shutdowNow method is invoked if the deadline running tasks are stopped (by interruption) when the deadline expires before the executor has terminated.
ThreadFactory factory = Thread.builder().virtual().factory()var deadline = Instant.now().plusSeconds(2);try (ExecutorService executor = Executors.newUnboundedExector(factory).withDeadline(deadline, null)) {
executor.submit(task1);executor.submit(task2);}
Deadlines also work with nested usages by specifying the The current thread as the owner. The owner is interrupted when the deadline expiresalso interrupted which allows deadlines to apply to nested usages.
try (ExecutorService executor1 = Executors.newUnboundedExecutor(factory)
.withDeadline(deadline, Thread.currentThread())) {
try (ExecutorService executor2 = Executors.newUnboundedExecutor(factory)) {
executor2.submit(task1);
executor2.submit(task2);
}
}
...