Home » Java programming language

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?
Submitted by Preeti Jain, on February 09, 2018

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.
  • With the help of example we will understand why Thread safety is required?

Example: In a reservation system

  1. In a train we have only 5 vacant seats.
  2. If two person are trying to book tickets online and each person wants three seats .
  3. When both of them may get a chance but any one of them will get a message that there are no sufficient space left.
  • 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

Read more...

  1. Explain life cycle of a thread in java.
  2. How to Create Java Threads (java examples to create threads)?
  3. Thread Synchronisation in Java with Example.
  4. Java program to demonstrate example of thread.
  5. Java program to Join Threads.





Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.