Saturday, December 8, 2018

What is Thread Safety in Java?

In this post you will understand Thread Safety in Java. Java provide multi-threaded environment support using Java Threads, we know that multiple threads created from same Object share object variables and this can lead to data inconsistency when the threads are used to read and update the shared data.
Here is an example of non thread-safe code, look at the code and find out why this code is not thread safe ?

/*

* Non Thread-Safe Class in Java

*/

public class Counter {

private int count;

/*

* This method is not thread-safe because ++ is not an atomic operation

*/

public int getCount(){

return count++;

}

}

Above example is not thread-safe because ++ (increment operator) is not an atomic operation and can be broken down into read, update and write operation. if multiple thread call getCount() approximately same time each of these three operation may coincide or overlap with each other for example while thread 1 is updating value , thread 2 reads and still gets old value, which eventually let thread 2 override thread 1 increment and one count is lost because multiple thread called it concurrently.



Thread safety in java is the process to make our program safe to use in multithreaded environment, there are different ways through which we can make our program thread safe.

Synchronization is the easiest and most widely used tool for thread safety in java.
Use of Atomic Wrapper classes from java.util.concurrent.atomic package. For example AtomicInteger
Use of locks from java.util.concurrent.locks package.
Using thread safe collection classes, check this post for usage of ConcurrentHashMap for thread safety.
Using volatile keyword with variables to make every thread read the data from memory, not read from thread cache.
Thread safety is the ability to have multiple threads execute a method or a block of code without causing any side effect to shared resources / objects.

In java you can mark a block of code or method with synchronized keyword in order make that a thread safe which makes sure only one thread executes this block at a time. Basically, it locks the object or class when a thread is executing the synchronized block, So other threads will have to wait in queue for first thread to finish in order to get access to the locked object.

The packages java.util.concurrent and java.util.concurrent.atomic has been introduced in Java 5 with multiple utilities and wrappers in order to make thread safe programming easy.

There won’t be thread safety issues if there are no shared resources. Lets say your class doesn’t have any instance variables, its automatically thread safe. Final variables, Immutable Objects (popular one is String class), variables defined inside the method, Wrappers defined in concurrent.atomic package are all thread safe.


No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...