
Green threads are threads that are scheduled by a virtual machine (VM) rather than natively by the underlying operating system.
Example:
// Pseudocode for green thread scheduling
while (true) {
for (Thread t : allThreads) {
if (t.isRunnable()) {
t.run();
}
}
}
With Java 1.2+, the JVM started using native OS threads.
Thread t = new Thread(() -> System.out.println("Hello from native thread!"));
t.start();
Java 5 introduced the java.util.concurrent package, making thread management easier.
import java.util.concurrent.*;
ExecutorService pool = Executors.newFixedThreadPool(4);
pool.submit(() -> System.out.println("Task in pool"));
pool.shutdown();
Tip: Always shut down your thread pools!
Java 19+ (preview) introduces virtual threads, enabling millions of lightweight threads.
Thread.startVirtualThread(() -> {
System.out.println("Hello from a virtual thread!");
});
| Feature | Green Threads | Native Threads | Virtual Threads |
|---|---|---|---|
| Scheduling | JVM | OS | JVM |
| Concurrency | Simulated | True | True |
| Resource Usage | Low | High | Very Low |
| Introduced In | Java 1.0 | Java 1.2+ | Java 19+ |
Further Reading:
Happy coding!