Blogs

Writing Thread-Safe Programs – Analyze Cost Of Locking

One way of writing a thread-safe class is to use synchronization when its shared variables are being modified so that a lock is obtained by the executing thread leaving all the other threads in the waiting state to get the access of the shared variables. This way we ensure that only one thread can modify the shared variable and no race condition occurs.

Topics: Thread-Safe Programs Java Cost of Locking Multithreading Technology DAO

Writing Thread-Safe Programs Using Atomic Variables

Race condition never occurs in a thread-safe program. What is a race condition? When variables are shared by multiple threads, there is a possibility that more than one thread get access to a variable and try to modify its value at the same time. This could lead to a loss of data modification done by a thread and leave the program in an inconsitent state. Such hazards should be avoided by allowing only one thread to access the shared variables when they are being modified. Synchronized methods and synchronized blocks are used to lock an object so that only one thread can access the object’s state and other threads keep waiting for the access until the lock is released.

Topics: Thread-Safe Programs Java SE 5 Java Multithreading Atomic Variables Technology

Writing Thread-Safe Programs Using ConcurrentHashMap

When your program can be accessed by multiple threads concurrently, you need to ensure that it is thread-safe. What is thread safety? A thread-safe program behaves correctly and is always in a consistent state when accessed by multiple threads regardless of the interleaving of the thread execution. A thread-safe program runs as consistently as if it is run by a single thread.

Topics: Thread-Safe Programs Java Multithreading Concurrency API ConcurrentHashMap Technology

e-Zest Solutions is digital experience engineering company with facilities in the United States (Detroit & San Jose), Germany (Hannover), United Kingdom (London UK) and India (Pune) with global clientele. Our services include custom software development, offshore software development, UX consulting, BigData, Managed cloud Services (Azure & AWS), SharePoint consulting/Migration, Enterprise Java application development, Automated software testing services.