What are Virtual Thread in Java 21?
Virtual threads are JVM-managed lightweight threads that will help in writing high-throughput concurrent applications (throughput means how many units of information a system can process in a given amount of time). [JEP-425, JEP-436 and JEP-444] In Java 21, virtual threads are ready for production use.
With the introduction of virtual threads, it becomes possible to execute millions of virtual threads using only a few operating system threads. The most advantageous aspect is that there is no need to modify existing Java code. All that is required is instructing our application framework to utilize virtual threads in place of platform threads.
To create a virtual thread, using the Java APIs, we can use the Thread or Executors.
Example:
Runnable runnable = () -> System.out.println("Inside Runnable");
//1
Thread.startVirtualThread(runnable);
//2
Thread virtualThread = Thread.ofVirtual().start(runnable);
//3
var executor = Executors.newVirtualThreadPerTaskExecutor();
executor.submit(runnable);
Zum Wiederholen speichern
Zum Wiederholen speichern
Speichere diesen Eintrag als Lesezeichen, markiere ihn als schwierig oder lege ihn in einem Wiederholungsset ab.
Melde dich an, um Lesezeichen, schwierige Fragen und Wiederholungssets zu speichern.