Java / Java 21 Virtual Threads Interview questions
List the ways to create virtual threads in Java 21?
There are four common entry points for getting a virtual thread running, all producing the same underlying kind of thread:
Thread.startVirtualThread(Runnable)- creates and starts one immediately.Thread.ofVirtual().start(Runnable)- builder style, with optional naming or exception handling.Thread.ofVirtual().unstarted(Runnable)then.start()- build now, run later.Executors.newVirtualThreadPerTaskExecutor()- anExecutorServicethat starts a new virtual thread for every submitted task.
The executor-based approach is the most common in application code because it integrates with existing ExecutorService-based designs.
More Related questions...