APIs to create Java Virtual Thread

Java virtual thread is a new feature available from JDK 19. It has potential to improve an application’s availability, throughput and code quality on top of reducing memory consumption. Here is a quick introduction to Java Virtual Thread, if you are interested in learning more about it.


Video: To see the visual walk-through of this post, click below:


In this post, let’s learn the APIs through which virtual threads can be created. 

Note: At the time (Feb’ 2023) of writing this post, virtual thread is a preview feature available in JDK 19. Preview features may be removed in a future release, or upgraded to permanent features of the Java platform.

1. Thread.startVirtualThread()

1: 

2: Runnable task = () -> { System.out.println("Hello Virtual Thread!"); };

3:

4: Thread.startVirtualThread(task);

‘Thread.startVirtualThread()’ is one of the APIs to create Java virtual threads. Probably the easiest of all as well. If you notice, in the above program in line #4, we are invoking ‘Thread.startVirtualThread()’ method by passing a Runnable object as an argument, which is created in line #2. 

Note: In line #2 we are creating a Runnable task written in lambda style (i.e. with ->), equivalent of that line of code, when written in ‘unfolded’ format is given below:

1: private class Task implements Runnable {

2: 

3:   @Override

4:   public void run() {

5:

6:    System.out.println("Hello! I am a Virtual Thread");

7:  }

8: }

2. Thread.ofVirtual().start()

1: 

2: Runnable task = () -> { System.out.println("Hello Virtual Thread!"); };

3: 

4: Thread vThread = Thread.ofVirtual().start(task);

Thread.ofVirtual().start()’ is another API to create Java virtual threads. If you notice, in the above program in line #4, we are invoking ‘Thread.ofVirtual().start()’ method by passing a Runnable object as an argument, which is created in line #2. 

3. Thread.ofVirtual().unstarted()

1: 

2: Runnable task = () -> { System.out.println("Hello Virtual Thread!"); };

3: 

4: Thread vThread = Thread.ofVirtual().unstarted(task);

5: vThread.start();

 Here is a two step approach to create Java virtual thread. Basically in line #4, we are invoking ‘Thread.ofVirtual().unstarted()’ API to create the thread object. But in this step, we are not launching the thread. However in line #5, we are invoking the ‘Thread.start()’ API to launch the thread. You can use this approach, if you want to create a thread object with proper attributes name, priority, …. & then launch the thread. You can also use this API if you want to build a thread in one part of the program and launch it in a different part of the program.

4. Executors.newVirtualThreadPerTaskExecutor()

1: 

2: Runnable task = () -> { System.out.println("Hello Virtual Thread!"); };

3: 

4: Executor vExecutor = Executors.newVirtualThreadPerTaskExecutor();

5: vExecutor.execute(task);

Executor is a powerful mechanism to create, manage and shutdown thread pools in the application. You can also create virtual threads through an Executor using the ‘Executors.newVirtualThreadPerTaskExecutor()’ API. Actually, this API starts a new virtual thread for each task. The number of virtual threads that is created by the Executor is unbounded in this case.

5. Executors.newThreadPerTaskExecutor()

1: 

2: Runnable task = () -> { System.out.println("Hello Virtual Thread!"); };

3: 

4: ThreadFactory vThreadFactory = Thread.ofVirtual().name("vt-", 1).factory();

5: Executor vExecutor = Executors.newThreadPerTaskExecutor(vThreadFactory);

6: vExecutor.execute(task);

This API is a minor enhancement to the Executor approach discussed in the earlier section. In this Executors.newThreadPerTaskExecutor() API, you can pass ThreadFactory as an input argument and then create virtual threads. In the Threadfactory, you can set virtual thread attributes such as thread name, thread priority, … If you notice, in the above program in line #4, we are creating a ThreadFactory. Any threads that are created from this factory will have the names like: vt-1, vt-2, vt-3, …

Conclusion

In this post, we learnt various APIs to create virtual threads. You may also consider learning the benefits of virtual threads and its performance impacts to appreciate virtual thread architecture further.

9 thoughts on “APIs to create Java Virtual Thread

Add yours

  1. Hello
    Great article for Virtual Thread.. Thanks for sharing this article.
    I want to draw attention towards Virtual Thread priority.
    As per draft currently “Virtual threads have a fixed priority of Thread.NORM_PRIORITY. The Thread.setPriority(int) method has no effect on virtual threads.
    Ref: https://openjdk.org/jeps/8303683

Leave a Reply

Powered by WordPress.com.

Up ↑

%d