Project Loom is intended to explore, incubate and deliver Java VM features and APIs built on top of them for the purpose of supporting easy-to-use, high-throughput lightweight concurrency and new programming models on the Java platform. It is part of JDK 19.
Currently Java uses the OS kernel threads and this causes the following limitations:
- The implementation of the java.lang.Thread type is called platform thread. The problem with platform threads is that they are expensive from a lot of points of view. First, they are costly to create. Whenever a platform thread is made, the OS must allocate a large amount of memory (megabytes) in the stack to store the thread context, native, and Java call stacks.
- Applications can serve millions of transactions, and it is not feasible for one jvm to accommodate the one Thread per operation model.
- Synchronization doesn't come for free especially when io is involved.
With the current tools, Java offer the ability to develop the code in a reactive fashion (e.g. RxJava) but this has the following drawbacks:
- Debugging is a nightmare.
- Error handling is not that obvious.
- K(eep) I(u) S(imple) S(tupid) principle is difficult to apply since code becomes complex.
- Use simple blocking I/O apis
- Avoid Pinning (using synchronized blocks). This can lead to deadlocks (-Djdk.tracePinnedThreads to detect them)
- Don't use ThreadLocal to cache objects
- From shared thread pools -> new virtual thread per task