21 August 2024

Handling concurrent access to shared resources in Java - by Sebastian Konieczek


Multithreaded applications are to be found everywhere. Think of RESTful micro services for example. The very nature of a classic Webserver is Multithreading as every Request is processed concurrently by an available worker thread. This means a lot of concurrent access to shared resources. And with Java’s virtual threads, there will be even more concurrency. Most of the time we rely on Frameworks and Tools to handle concurrent access correctly. But there are times when we need to handle this ourselves and this is where the trouble starts. Think of concurrent writes or updates to a shared file in the files system. Another example may be, having a shared state in memory that is mutated concurrently or the use of stateful singletons. In software development, controlling access to shared resources is often solved with mutexes or semaphores. But limiting resource access has certain drawbacks to consider. In this session you will learn what kind of mutex or locking strategies exist and when to choose which. I will give a brief introduction to the techniques Java has to offer and what the locking strategy behind each approach is. You will learn, what to consider when using mutexes in virtual threads. In the end you will know how to choose the fitting key for your lock and get the best performance whilst preserving data integrity.


Sebastian Konieczek

Sebastian is a passionate Full Stack Software Engineer from OpenValue Düsseldorf. He is an expert in the requirement analysis of complex systems in the field of investment advice and pension provision, but his long years of experience he covers the entire software life cycle from analysis to commissioning and support. Sebastian's interests lie in architecture and design issues, but coding remains his passion. When there is time left over from work and family, he does weight training and fitness, plays games and enjoys working on hobby projects.