@backstreetbrogrammer
--------------------------------------------------------------------------------
Chapter 02 - Virtual Threads - Thread Pool Based OMS
--------------------------------------------------------------------------------
Suppose, we have an Order Management System (OMS) which is working as a TCP server and receiving stock trading orders from various clients.
Once the order is received, following processing is done on the Order object before it is sent down to algorithmic trading engine or directly to exchange (DMA):
- Validate the order client's wallet if enough funds
- Enrich the order with latest market data (best bid / best ask)
- Update the latest order state to persistence (log or database)
Each of the following takes around 1 second:
- parse the request
- create an Order from the request
- validate the order client's wallet if enough funds
- enrich the order with latest market data
- update the latest order state to persistence
- send the order to downstream
The connection listening thread that calls accept() creates and starts a new Thread to handle the connection and quickly goes back to accepting more connections.
The Executor Pattern aims to fix the above-mentioned issues:
- by creating pools of ready-to-use threads
- passing task to this pool of threads that will execute it
The threads in this pool will be kept alive as long as this pool is alive.
It means that the single thread will execute the submitted task =: once the task finishes, the thread will return to the pool and wait for a new task to be submitted for execution.
As compared to Runnable pattern, Executor pattern does NOT create a new thread.
This is a better design as now we have a limited number of threads as guaranteed in the thread pool size.
However, the time to process a request is still same: 1 + 1 + max(1, 1, 1) + 1 = 4 seconds.
Few of the caveats here are:
- order is a shared object amongst all the threads and needs to be synchronized
- code is more complex and using helper objects like CountDownLatch
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
Негізгі бет Ғылым және технология 11 - Thread Pool Based OMS
Пікірлер