9

Java Virtual Threads – Easy Introduction

 1 year ago
source link: https://devm.io/java/java-virtual-threads-intro
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Java virtual threads are a new feature introduced in JDK 19. It has potential to improve an applications availability, throughput and code quality on top of reducing memory consumption.

This article intends to introduce Java virtual threads in an easily understandable manner.

Video: To see the visual walk-through of this article, click here.

Thread Life Cycle

Fig 1: Typical thread’s life cycle

Fig 1: Typical thread’s life cycle

Let’s walkthrough a typical lifecycle of a thread:

  • 1: Thread is created in a thread pool.
  • 2: Thread waits in the pool for a new request to come.
  • 3: Once the new request comes, the thread picks up the request and it makes a backend database call to service this request.
  • 4: Thread waits for response from the backend database.
  • 5: Once the response comes back from the database, the thread processes it, and sends back the response to customer.
  • 6: Thread is returned back to the thread pool.

Steps #2 to #6 will repeat until the application is shutdown.

If you notice, the thread is actually doing real work only in step #3 and #5. In all other steps (i.e., step #1, step #2, step #4, step #6), it is basically waiting (or doing nothing). In most applications, a significant number of threads predominantly waits during most of its lifetime.

Platform threads architecture

Fig 2: Each Platform Thread is mapped to an Operating System Thread

Fig 2: Each Platform Thread is mapped to an Operating System Thread

In the previous JVM (Java Virtual Machine) release, there was only one type of thread. It’s called a ‘classic’ or ‘platform’ thread. Whenever a platform thread is created, an operating system thread is allocated to it. Only when the platform thread exits (i.e., dies) the JVM, this operating system thread is free to do other tasks. Until then, it cannot do any other tasks. Basically, there is a 1:1 mapping between a platform thread and an operating system thread.

According to this architecture, an OS thread will be unnecessarily locked down in step #1, step #2, step #4, and step #6 of the thread’s life cycle, even though it’s not doing anything during these steps. Since OS threads are precious and finite resources, time is extensively wasted in this platform threads architecture.

Virtual threads architecture

Fig 3: OS Threads are not allocated to Platform threads until real work needs to be executed

Fig 3: OS Threads are not allocated to Platform threads until real work needs to be executed

Fig 4: Virtual Threads are mapped to platform threads when it does real work

Fig 4: Virtual Threads are mapped to platform threads when it does real work

In order to efficiently use underlying operating system threads, virtual threads have been introduced in JDK 19. In this new architecture, a virtual thread will be assigned...


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK