Java 21 Virtual Threads

Java 21 Virtual Threads

An introduction using a real-life analogy


A clever new way of multi-threading called 'virtual threads' was introduced in Java version 21. Let us find out what it is and how it is different.


Thread is an instance of service. Multi-threading helps serve multiple customers simultaneously.?


Consider a busy restaurant. After a customer gets seated at a table, one of the Servers greets the customer and hands over the menu. The Server will then wait for the customer to order. For efficient service in the restaurant, there must be as many Servers as the number of customers. One Server per customer is ideal. However, this is very expensive to manage.?


Traditional multi-threading works similarly. A thread serves a client request, like a Server attending a customer at the restaurant. There is a limit to the number of threads, like how there is a limit to the number of Servers at a restaurant.


To serve a high number of customers with minimal expense, the restaurants usually follow one of these three methods -?

  1. Token?number?or a buzzing device given to the customer.
  2. Separate counters?specific to different cuisines.
  3. Order form?handed over to the customer.


1. Token number

Tokens are commonplace in fast-food restaurants and pubs.

In a fast-food restaurant, the customer pays for the order and then waits for a token number announcement to collect the ordered food.


The customer must keep track of the token number and collect food from the counter at the appropriate time. Also, the customer must safeguard the table when going to the counter to pick up the order. Often, customers compete or even quarrel for a table. Restaurants must employ bouncers or specialised staff for resolving such situations.


Reactive Programming?model supported by various languages, including Java, is analogous to this?token?method. Reactive programming saw widespread adoption in the past decade and was considered by many as the go-to for super scalability. The pitfall of this model is that the client code must handle the complexity of subscribing and responding to the server-side events. There must also be a mediator component like a service bus, a message queue, or an event grid to handle competing clients.



2. Separate counters

Buffet lunch restaurants and food courts in shopping malls have separate counters for different food items. (Example: Dosa from South Indian counter, Naan & curry from North Indian counter, Coffee from Tea/Coffee counter, etc.). Customer visits counters separately to collect all the food items of interest.


The service is very efficient, but the customer needs to make numerous trips to various counters. Crowding at the counters serving favourite items is another problem.


Go language's?go-routine?is analogous to this?separate-counters?method. It offers high efficiency for client requests distributed uniformly across the available resources. If too many requests are for one specific favourite resource, then a separate logic must be coded for handling that requirement.



3. Order form

In some restaurants, the Server hands over an order form (a paper form or a tablet device or a QR code for an app) showing the food menu to the customer and asks the customer to tick the items to order. This order form is a?Virtual Server. While the customer is busy deciding on the order or connecting to Wi-Fi, the real Server attends to another customer.


Server focuses on the duty of delivering food to the tables, while the virtual server (form / app) engages the customer most of the time. Server does the extra work of switching from table to table, but the customer can sit back and relax without worrying about the competition with other customers.


Java 21 Virtual Threading?is analogous to this?order-form?method.

It allows a single thread to cater to multiple clients by switching between clients when clients are busy with other activities like I/O operations. It also does not put any additional burden on the clients. The existing multi-threaded applications in Java need very little code change to get elevated to virtual threading. Clever!

要查看或添加评论,请登录

Srinivas Gopinath Parimi的更多文章