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:
- Using Synchronization.
- Using Volatile Keyword.
- Using Atomic Variable.
- Using Final Keyword.
Using Synchronization
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.
Java
class A {
synchronized void sum( int n)
{
Thread t = Thread.currentThread();
for ( int i = 1 ; i <= 5 ; i++) {
System.out.println(
t.getName() + " : " + (n + i));
}
}
}
class B extends Thread {
A a = new A();
public void run()
{
a.sum( 10 );
}
}
class Test {
public static void main(String[] args)
{
B b = new B();
Thread t1 = new Thread(b);
Thread t2 = new Thread(b);
t1.setName( "Thread A" );
t2.setName( "Thread B" );
t1.start();
t2.start();
}
}
|
Output:
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.
Java
public class VolatileExample {
static volatile int a = 0 , b = 0 ;
static void method_one()
{
a++;
b++;
}
static void method_two()
{
System.out.println(
"a=" + a + " b=" + b);
}
public static void main(String[] args)
{
Thread t1 = new Thread() {
public void run()
{
for ( int i = 0 ; i < 5 ; i++)
method_one();
}
};
Thread t2 = new Thread() {
public void run()
{
for ( int i = 0 ; i < 5 ; i++)
method_two();
}
};
t1.start();
t2.start();
}
}
|
Output:
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.
Java
import java.util.concurrent.atomic.AtomicInteger;
class Counter {
AtomicInteger count
= new AtomicInteger();
public void increment()
{
count.incrementAndGet();
}
}
public class TestCounter {
public static void main(
String[] args) throws Exception
{
Counter c = new Counter();
Thread t1 = new Thread(
new Runnable() {
public void run()
{
for ( int i = 1 ; i <= 2000 ; i++) {
c.increment();
}
}
});
Thread t2 = new Thread(
new Runnable() {
public void run()
{
for ( int i = 1 ; i <= 2000 ; i++) {
c.increment();
}
}
});
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println(c.count);
}
}
|
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.
Java
public class FinalTest {
final String str
= new String( "hello" );
void method()
{
str = "world" ;
}
}
|
Output:
Compilation Error in java code :-
prog.java:14: error: cannot assign a value to final variable str
str = "world";
^
1 error
Feeling lost in the vast world of Backend Development? It's time for a change! Join our
Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
- Comprehensive Course
- Expert Guidance for Efficient Learning
- Hands-on Experience with Real-world Projects
- Proven Track Record with 100,000+ Successful Geeks
Last Updated :
24 Jun, 2021
Like Article
Save Article