Java / Java 21 Virtual Threads Interview questions
Describe the Thread.Builder API?
The Thread.Builder API is a fluent way to configure and create threads, introduced alongside virtual threads. It comes in two flavors: Thread.ofPlatform() and Thread.ofVirtual(), both returning a builder.
Thread t = Thread.ofVirtual() .name("order-processor-", 0) .uncaughtExceptionHandler((th, ex) -> log(ex)) .start(() -> processOrder());
Common builder methods include .name(...), .unstarted(runnable), .start(runnable), and .factory(), which produces a ThreadFactory you can hand to an ExecutorService.
More Related questions...