Class VirtualThreadsDemo
This class showcases the use of virtual threads, a lightweight concurrency model introduced in Java 21 as part of Project Loom. Virtual threads allow for the creation of thousands (or even millions) of concurrent threads with minimal resource overhead compared to traditional platform threads. This makes high-concurrency programming more scalable and accessible. Virtual threads are significant because they decouple the Java thread from the underlying OS thread, enabling efficient management of concurrent tasks and better utilization of modern hardware.
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
VirtualThreadsDemo
public VirtualThreadsDemo()
-
-
Method Details
-
main
Main method demonstrating how to launch and use virtual threads.The method launches 5 virtual threads. Each thread prints its identifier and thread info, then sleeps for 1 second to simulate some work. The main thread then sleeps for a short time to allow the virtual threads to complete before the program exits. (In production code, proper synchronization or thread joining would be used instead of sleeping.)
- Parameters:
args
- command-line arguments (not used)- Throws:
InterruptedException
- if the main thread is interrupted while sleeping
-