Monday, 13 July 2026

Thread.ofPlatform() vs Thread.ofVirtual() Made Simple

  

Java has been evolving rapidly, and one of the most exciting additions in recent releases is Virtual Threads (part of Project Loom). If you’ve ever struggled with managing threads in Java, this new feature will feel like a game changer. In this post, we’ll understand the difference between:

 

·      Thread.ofPlatform()

·      Thread.ofVirtual()

 

We’ll also see why virtual threads matter, and how you can use them in your Java applications.

 

Why Threads Matter?

Threads let you run multiple tasks concurrently. For example, one thread might handle a user request, while another writes data to a file. Traditionally, Java used platform threads, these are backed by the operating system (OS). While powerful, they are expensive to create and manage. Each thread requires memory, and starting thousands of them can slow down or even crash your program.

 

This is where virtual threads come in.

 

Creating Threads in Java

Using Thread.ofPlatform(): This creates a normal, OS backed thread (just like the ones Java has always had).

 

PlatformThreadCreation.java

package com.sample.app.virtual.threads.creation;

public class PlatformThreadCreation {

    public static void main(String[] args) {
        Thread platformThread = Thread.ofPlatform().start(() -> {
            System.out.println("Running in a platform thread: " + Thread.currentThread());
        });

        try {
            platformThread.join(); // wait for the thread to finish
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        
        System.out.println("Finished Execution of Platform Thread work....");
    }

}

Output

Running in a platform thread: Thread[#25,Thread-0,5,main]
Finished Execution of Platform Thread work....

   

Using Thread.ofVirtual()

This creates a lightweight virtual thread.

 

VirtualThreadExample.java

package com.sample.app.virtual.threads.creation;

public class VirtualThreadExample {
    public static void main(String[] args) {
        Thread virtualThread = Thread.ofVirtual().start(() -> {
            System.out.println("Running in a virtual thread: " + Thread.currentThread());
        });

        try {
            virtualThread.join(); // wait for the thread to finish
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        
        System.out.println("Finished Execution of Virtual Thread work....");
    }
}

   

Output

Running in a virtual thread: VirtualThread[#26]/runnable@ForkJoinPool-1-worker-1
Finished Execution of Virtual Thread work....

   

In summary:

·      Use platform threads when you’re doing CPU-heavy work (like processing huge numbers or image processing).

·      Use virtual threads when you’re doing I/O-heavy work (like calling APIs, reading databases, or handling many client requests).

 

Virtual threads don’t replace platform threads, they complement them. Think of virtual threads as giving you the ability to scale concurrency without hitting OS limits.

   

Previous                                                    Next                                                    Home

No comments:

Post a Comment