Advertisement



< Prev
Next >



Synchronization in Java



When two or more running threads have to access a shared resource such as in-memory data (instance or class variables) and external objects such as files, there could be a conflict. Java provides us a smooth access of such shared resource by a process of synchronization. Synchronization ensures that only one thread is accessing the shared resource at a time, avoiding the conflict between threads.




How synchronization is achieved?


Synchronization is achieved by synchronizing the code in two ways-:



Concept of synchronizing a method.







A class without a synchronized method.


We are creating two threads that are going to compete over a non-synchronized method. The first thread to enter the non-synchronized method does not get its sole access, this thread executes the method for a while before it is replaced another thread wanting to execute this method.
class B
{
public void sync() //a non-synchronized method
{
	for(int i=0;i<3;i++)
	{
		System.out.println(Thread.currentThread()+" " + i);
	}

}
}


class A implements Runnable 
{
B obj;

A(B ob)
{
	obj=ob;
}

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

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

B ob= new B();

A a1= new A(ob);

A a2= new A(ob);

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

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

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




A class with a synchronized method.


We are creating two threads that are going to compete over a method marked with synchronized keyword. The first thread to enter the synchronized method gets its sole access until it exits the synchronized method, thereby avoiding the collision between two threads competing for a method.
class B
{
synchronized public void sync() //synchronized method
{
	for(int i=0;i<3;i++)
	{
		System.out.println(Thread.currentThread()+" " + i);
	}

}
}


class A implements Runnable 
{
B obj;

A(B ob)
{
	obj=ob;
}

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

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

B ob= new B();

A a1= new A(ob);

A a2= new A(ob);

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

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


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

PYTHON


Advertisement