How can we achieve Thread Safety in Java?

Learn: How can we achieve Thread Safety in java? Why Thread Safety is required? Is this term related to synchronization?
By Preeti Jain Last updated : January 26, 2024

Thread Safety in Java

  • Thread Safety concept is introduced in synchronization concepts of java.
  • When multiple people or multiple threads are operating on the same object simultaneously then there may be a chance of inconsistency problem.
  • By using synchronized keyword we can achieve thread safety (i.e. only one thread is allowed to access a particular object).
  • By using synchronization concept we can overcome the problem of inconsistency.

Why Thread Safety is Required?

With the help of example we will understand why Thread safety is required?

Example

In a reservation system

In a train we have only 5 vacant seats. If two person are trying to book tickets online and each person wants three seats. When both of them may get a chance but any one of them will get a message that there are no sufficient space left.

Achieving Thread Safety

  • If a method or block declared as synchronized then at a time only one thread is allowed to execute that method or block on a particular object so data inconsistency problem will be resolved.
  • If thread are executing one by one by this we can achieve data inconsistency and thread safety but it increases waiting time of thread and performance get down from doing this.
  • If we declare method as synchronized it means only one thread is allowed to access that method at a time and remaining threads can access non-synchronized methods simultaneously (i.e. Restriction on synchronized method and No Restriction on non-synchronize method).
  • Thread Safety will be required when working with multiple threads on the same object. In one thread will be in safe state there is no need to implement in a single thread.

Example

In the below, example we will implement synchronization concepts:

class SynchronizeMethodClass {

  public synchronized void fruits(String fruits) {
    for (int i = 0; i < 10; ++i) {
      System.out.println("I am in fruits method :");
      try {
        Thread.sleep(1000);
      } catch (InterruptedException ie) {
        System.out.println("Exception occurs");
      }
      System.out.println(fruits);
    }
  }
}

class FirstThread extends Thread {
  SynchronizeMethodClass SMC;
  String fruits;
  FirstThread(SynchronizeMethodClass SMC, String fruits) {
    this.SMC = SMC;
    this.fruits = fruits;
  }
  public void run() {
    SMC.fruits(fruits);
  }
}

class SecondThread {
  public static void main(String[] args) {
    SynchronizeMethodClass SMC = new SynchronizeMethodClass();
    FirstThread FT = new FirstThread(SMC, "APPLE");
    FirstThread FTT = new FirstThread(SMC, "Orange");
    FirstThread FTTT = new FirstThread(SMC, "Grapes");
    FirstThread FTTTT = new FirstThread(SMC, "Banana");
    FT.start();
    FTT.start();
    FTTT.start();
    FTTTT.start();
  }
}

Output

D:\Java Articles>java SecondThread
I am in fruits method :
Grapes
I am in fruits method :
Grapes
I am in fruits method :
Grapes
I am in fruits method :
Grapes
I am in fruits method :
Grapes
I am in fruits method :
Grapes
I am in fruits method :
Grapes
I am in fruits method :
Grapes
I am in fruits method :
Grapes
I am in fruits method :
Grapes
I am in fruits method :
Banana
I am in fruits method :
Banana
I am in fruits method :
Banana
I am in fruits method :
Banana
I am in fruits method :
Banana
I am in fruits method :
Banana
I am in fruits method :
Banana
I am in fruits method :
Banana
I am in fruits method :
Banana
I am in fruits method :
Banana
I am in fruits method :
Orange
I am in fruits method :
Orange
I am in fruits method :
Orange
I am in fruits method :
Orange
I am in fruits method :
Orange
I am in fruits method :
Orange
I am in fruits method :
Orange
I am in fruits method :
Orange
I am in fruits method :
Orange
I am in fruits method :
Orange
I am in fruits method :
APPLE
I am in fruits method :
APPLE
I am in fruits method :
APPLE
I am in fruits method :
APPLE
I am in fruits method :
APPLE
I am in fruits method :
APPLE
I am in fruits method :
APPLE
I am in fruits method :
APPLE
I am in fruits method :
APPLE
I am in fruits method :
APPLE

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.