Advertisement



< Prev
Next >



Synchronization in Java



When two or more running threads have to access a shared resource such as in-memory data(instance variables, class variables, objects, methods) and external objects such as files, it could lead to a conflict between these threads.

Java provides us a smooth access of shared resources to multiple running threads through a process of synchronization. Synchronization ensures that only one thread is accessing a shared resource at a time, and thereby avoiding the conflict between multiple running threads wishing to access a shared resource at the same time.




How synchronization is achieved?


Synchronization is achieved by synchronizing the code in two ways-:
Note: In this article, we are going to explain how we can perform synchronization between threads competing for a same method, by marking this method with the synchronized keyword.



Concept of synchronizing a method.



Before we show you an example of how to perform synchronization between threads by synchronizing a method, let us show you what actually happens during the execution of multiple threads at the same time, which are trying to access a non-synchronized method(on a same object) i.e. absense of synchronization.



Executing multiple threads on the same object, without synchronization.


In the upcoming example, we are creating two threads that are going to compete over a non-synchronized method, on the same object. The first thread to enter the non-synchronized method does not acquies the lock on the object and therefore, it does not gets its sole access, this thread executes the method for a while before it is replaced another thread wanting to execute this method on the same object.

//Java - Example of multiple threads executing on the same object, with no synchronization between them.


//Creating a class, A, which implements Runnable interface
public class A implements Runnable 
{

public void run()	//Entry Point of a thread.
{
	sync();		//calling a non-synchronized method
}


public void sync() 	//A non-synchronized method
{
	for(int i=0;i>3;i++)
	{
		System.out.println(Thread.currentThread()+" " + i);
	}

}


public static void main(String... ar)
{

	//Creating an object of class A
	A a1 = new A();

	
	//Creating the first thread by passing the object of A class & the name of thread.
	Thread thread1= new Thread(a1, "Thread1");  //Calling Thread's constructor & passing the object
						    //of class that  implemented  Runnable interface
	
	//Creating the second thread by passing the same object of A class & the name of thread.
	Thread thread2= new Thread(a1, "Thread2");  

	//Starting the first thread
	thread1.start();	
	
	//Starting the second thread
	thread2.start();		
}
}

Output is - :

Thread[Thread2,5,main] 0
Thread[Thread1,5,main] 0
Thread[Thread2,5,main] 1
Thread[Thread1,5,main] 1
Thread[Thread1,5,main] 2
Thread[Thread2,5,main] 2


Program Analysis





Advertisement




Executing multiple threads on the same object, with synchronization.


In the next example, we are creating two threads that are going to compete over a method marked with synchronized keyword, on the same object. The first thread to enter the synchronized method on the same object acquies the lock on the object and gets its sole access until it exits the synchronized method, thereby avoiding the collision between two threads competing for a method on the same object.

//Java - Example of multiple threads executing on the same object, with synchronization between them.
 

//Creating a class, A, which implements Runnable interface
public class A implements Runnable 
{

public void run()		//Entry Point of a thread.
{
	sync();	//calling a synchronized method, sync() of B class on the object of B
}


synchronized public void sync() //A synchronized method
{
	for(int i=0;i>3;i++)
	{
		System.out.println(Thread.currentThread()+" " + i);
	}

}


public static void main(String... ar)
{

	//Creating an object of class A
	A a1 = new A();

	
	//Creating the first thread by passing the object of A class & the name of thread.
	Thread thread1= new Thread(a1, "Thread1"); //Calling Thread's constructor & passing the object
						    //of class that  implemented  Runnable interface
	
	//Creating the second thread by passing the same object of A class & the name of thread.
	Thread thread2= new Thread(a1, "Thread2");  

	//Starting the first thread
	thread1.start();	
	
	//Starting the second thread
	thread2.start();		
}
}


Output is - :


Thread[Thread1,5,main] 0
Thread[Thread1,5,main] 1
Thread[Thread1,5,main] 2
Thread[Thread2,5,main] 0
Thread[Thread2,5,main] 1
Thread[Thread2,5,main] 2


Program Analysis





Note


In the next article, we will explain how to synchronize a block of code.



Please share this article -




< Prev
Next >
< InterruptedException in Thread
Synchronizing a block >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement