@backstreetbrogrammer
--------------------------------------------------------------------------------
Chapter 02 - Virtual Threads - Futures Based OMS
--------------------------------------------------------------------------------
Futures are a standard Java abstraction that allows concurrent code to shift to a more functional flavor in which threads run functions and produce values, while combining the synchronization capabilities of the latch mechanism used earlier.
Essentially, a future represents an asynchronously running function and offers mechanisms for threads to wait for the output of the function.
Java 8's Concurrent API introduced CompletableFuture, a valuable tool for simplifying asynchronous and non-blocking programming.
The CompletableFuture class implements CompletionStage interface and the Future interface.
The implementation of our simple OMS server is fairly straightforward:
Simply create a future for any part of the computation that needs to run asynchronously.
This approach pools threads for reuse, avoids the latch (futures implement their own synchronization), and, importantly, the order is created and populated by a single thread and does not need to be thread-safe anymore.
The new order object is created by the connection-handling thread in parallel with the validation, enrichment and persistence tasks (which no longer need the order).
Thus, the time to process a request and send the order to downstream goes down from 1 + 1 + max(1, 1, 1) + 1 = 4 seconds to 1 + max(1, 1, 1) + 1 = 3 seconds.
However, the fact that threads are blocked on the join method while waiting for futures to be completed remains problematic in two ways:
- This blocking invites the possibility of deadlocks. The issue was addressed here by using two separate thread pools. On larger, more complex systems, however, the problem can become quite tricky. Multiplying pools or increasing pool sizes to guarantee the absence of deadlocks tends to result in a large number of threads which, when they are not blocked, lead to a suboptimal usage of computing resources.
- Blocking and unblocking threads, even in the best of scenarios, has a non-negligible cost. The actual parking and unparking of threads by the operating system take time. Furthermore, parked threads tend to see their data in processor-level caches overwritten by other threads, resulting in cache misses when the threads resume execution. Accordingly, techniques were devised to minimize thread blocking, ideally, to avoid it entirely.
Github: github.com/backstreetbrogramm...
- Upgrade to Java 21 Playlist: • Upgrade to Java 21
- Apache Spark for Java Developers Playlist: • Apache Spark for Java ...
- Top Java Coding Interview Problems Playlist: • Top Java Coding Interv...
- Java Serialization Playlist: • Java Serialization
- Dynamic Programming Playlist: • Dynamic Programming
#java #javadevelopers #javaprogramming
Негізгі бет Ғылым және технология 12 - Futures Based OMS
Пікірлер