Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

ThreadFactory factory = Thread.builder().virtual().factory()
try (ExecutorService executor = Executors.newUnboundedExecutornewThreadExecutor(factory)) {
    executor.submit(task1);
    executor.submit(task2);
}

...

try (ExecutorService executor = Executors.newUnboundedExecutornewVirtualThreadExecutor(factory)) {
executor.submit(() -> foo());
executor.submit(() -> bar());
}

void foo() {
    try (ExecutorService executor = Executors.newUnboundedExecutornewVirtualThreadExecutor(factory)) {
        executor.submit(...);
}
}

void bar() {
    try (ExecutorService executor = Executors.newUnboundedExecutornewVirtualThreadExecutor(factory)) {
        executor.submit(...);
    }

}

...

An ExecutorService can be wrapped with a deadline so that 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.newUnboundedExectornewVirtualThreadExector(factory).withDeadline(deadline)) {
    executor.submit(task1);
    executor.submit(task2);
}

...


try (ExecutorService executor1 = Executors.newUnboundedExecutornewVirtualThreadExecutor(factory).withDeadline(deadline)) {
try (ExecutorService executor2 = Executors.newUnboundedExecutornewVirtualThreadExecutor(factory)) {
executor2.submit(task1);
executor2.submit(task2);
}
}

...