As we know Java has a feature, Multithreading, which is a process of running multiple threads simultaneously. When multiple threads are working on the same data, and the value of our data is changing, that scenario is not thread-safe and we will get inconsistent results. When a thread is already working on an object and preventing another thread on working on the same object, this process is called Thread-Safety.
How to achieve Thread Safety
There are four ways to achieve Thread Safety in Java. These are:
Synchronization is the process of allowing only one thread at a time to complete the particular task. It means when multiple threads executing simultaneously, and want to access the same resource at the same time, then the problem of inconsistency will occur. so synchronization is used to resolve inconsistency problem by allowing only one thread at a time.
Synchronization uses a synchronized keyword. Synchronized is the modifier that creates a block of code known as a critical section.
Thread A : 11 Thread A : 12 Thread A : 13 Thread A : 14 Thread A : 15 Thread B : 11 Thread B : 12 Thread B : 13 Thread B : 14 Thread B : 15
Using Volatile keyword
A volatile keyword is a field modifier that ensures that the object can be used by multiple threads at the same time without having any problem. volatile is one good way of ensuring that the Java program is thread-safe. a volatile keyword can be used as an alternative way of achieving Thread Safety in Java.
a=5 b=5 a=5 b=5 a=5 b=5 a=5 b=5 a=5 b=5
Using Atomic Variable
Using an atomic variable is another way to achieve thread-safety in java. When variables are shared by multiple threads, the atomic variable ensures that threads don’t crash into each other.
Using Final keyword
Final Variables are also thread-safe in java because once assigned some reference of an object It cannot point to reference of another object.
Compilation Error in java code :- prog.java:14: error: cannot assign a value to final variable str str = "world"; ^ 1 error
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Naming a thread and fetching name of current thread in Java
- Difference between Thread.start() and Thread.run() in Java
- How a thread can interrupt an another thread in Java?
- Java.lang.Thread class in Java
- Lifecycle and States of a Thread in Java
- isAlive() and join() methods of Thread Class in Java
- Thread Interference and Memory Consistency Errors in Java
- Inter-thread Communication in Java
- Java Thread Priority in Multithreading
- Daemon thread in Java
- Main thread in Java
- Thread Pools in Java
- Implement Runnable vs Extend Thread in Java
- Lock framework vs Thread synchronization in Java
- Thread UncaughtExceptionHandler in Java with Examples
- Overriding of Thread class start() method
- Overloading of Thread class run() method
- Java.util.TreeMap.descendingMap() and descendingKeyset() in Java
- Java.util.TreeMap.firstEntry() and firstKey() in Java
- Java.util.TreeMap.containskey() and containsValue() in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.