You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/main/asciidoc/index.adoc
+34-47Lines changed: 34 additions & 47 deletions
Original file line number
Diff line number
Diff line change
@@ -4,6 +4,18 @@ Use virtual threads to write Vert.x code that looks like it is synchronous.
4
4
5
5
You still write the traditional Vert.x code processing events, but you have the opportunity to write synchronous code for complex workflows and use thread locals in such workflows.
6
6
7
+
== Introduction
8
+
9
+
One of the key advantages of Vert.x over many legacy application platforms is that it is almost entirely non-blocking (of kernel threads) - this allows it to handle a lot of concurrency (e.g. handle many connections, or messages) using a very small number of kernel threads, which allows it to scale very well.
10
+
11
+
The non-blocking nature of Vert.x leads to asynchronous APIs. Asynchronous APIs can take various forms including callback style, promises or Rx-style. Vert.x uses futurese in most places (although, it also supports Rx).
12
+
13
+
In some cases, programming using asynchronous APIs can be more challenging than using a direct synchronous style, in particular if you have several operations that you want to do in sequence. Also, error propagation is often more complex when using asynchronous APIs.
14
+
15
+
Vertx virtual threads allows you to work with asynchronous APIs, but using a direct synchronous style that you're already familiar with.
16
+
17
+
It does this by using Java 21 virtual threads. Virtual threads are very lightweight threads that do not correspond to underlying kernel threads. When they are blocked they do not block a kernel thread.
18
+
7
19
== Usage
8
20
9
21
To use the virtual threads with Vert.x add the following dependency to the _dependencies_ section of your build descriptor:
@@ -27,92 +39,67 @@ dependencies {
27
39
}
28
40
----
29
41
30
-
== Getting started
42
+
[#_virtual_thread_verticle]
43
+
== Virtual thread verticle
44
+
45
+
A virtual thread verticle is a worker verticle that needs a *single* instance to run the application.When the verticle *awaits* for a future result, this verticle can still process events like an event-loop verticle.
You can block using the {@link io.vertx.virtualthreads.await.Async#await} method to suspend the current virtual thread until the awaited result is available.
55
55
56
-
Using virtual threads with Vert.x requires to run application tasks on a virtual threads
This project implements virtual threads with Vert.x with a race free model. Events are dispatched to a virtual thread, when this virtual thread awaits an asynchronous result, the pending events are not dispatched until the virtual thread is resumed.
When a virtual thread awaits a future, a new virtual thread can be started to handle new events and avoid blocking the appliction or potential self deadlocks, e.g. in the following example, awaiting the response does not prevent the timer to fire
70
+
You can acquire the ownership of a `java.util.concurrent.locks`
If you block a virtual thread without `{@link io.vertx.virtualthreads.await.Async`, your application then will behave like a regular worker and events will not be processed until the current task ends.
78
-
79
-
== Verticles
77
+
=== Field visibility
80
78
81
-
Virtual thread verticles are actually worker verticles, however a single worker instance is enough to execute it.
79
+
A virtual thread verticle can interact safely with fields before an `await` call. However, if you are reading a field before an `await` call and reusing the value after the call, you should keep in mind that this value might have changed.
When your application blocks without `await`, the Vert.x scheduler cannot schedule events on the verticle, it behaves like a regular worker verticle, yet using a virtual thread.
107
98
108
-
You can lock a `java.util.concurrent.locks`
99
+
This use case is not precluded, however the verticle should be deployed with several instances to deliver the aimed concurrency.
0 commit comments