I'm developing a high-throughput Java application that requires strict data consistency but I want to minimize the performance issues associated with synchronized blocks. I'm debating whether to utilize StampedLock or VarHandles with CAS, as opposed to traditional locking methods. Additionally, I'm looking for tips on how to effectively combine CompletableFuture with custom thread pools for this situation. Any practical advice would be much appreciated!
1 Answer
To get the best advice, it would help to know more about your use case. You might want to consider using concurrent data structures to serve as the source of truth for your threads, like a linked blocking queue. The less interaction your threads have over the same data, the fewer locks you’ll need. If you're dealing with CPU-bound tasks that can be processed independently and then recombined later, you might not need much locking at all! Each thread can focus on its own segment, merging results afterward.
I'm trying to create a service that processes large volumes of time-sensitive financial data in parallel. Some streams can be handled independently, but others need to sync before being saved to shared storage. Should I break everything into isolated pipelines with their own queues and then merge the results, or stick with a shared structure and use CAS operations?