< Prev
Next >

Java - A Synchronized block

A class method marked with synchronized keyword avoids a conflict between two threads wanting to execute it at the same time, by giving its sole access to the first thread executing it, while the other thread waits for its turn.

But what if we don't have access to the code of a class to modify its method and mark it with the synchronized keyword? In such a situation, we will achieve a synchronized access of a class by synchronizing its object, within a synchronized block.

By synchronizing the access to an object of class, we avoid the conflict between two threads competing to execute the same method of an object, i.e. only one thread can access the object's methods at a time.

Synchronizing an access to the an object :

synchronized(B ob)			//Synchronized the access of B's object, ob.
{					//Synchronized block starts
	ob.non_Synchronized_Method();   //Call to non-synchronized method of object, ob.
}	    				//Synchronized block ends 

Here, we have synchronized the access to an object of class B, where, ob is the reference to the object being synchronized. This avoid a conflict between the two thread that are trying to call a non_Synchronized_Method() method on a same object of class B.

Synchronizing the access to an object of class B would ensure that the first thread to enter the synchronized block, gets the lock over the object and this thread will be the only thread to make a successful call to other non-synchronized method on this object, until this thread exits the synchronized block.


Program with a synchronized block.

We are creating two threads that are going to compete over to execute a method, meth() of class B.
//Java - A synchronized block

class B
public void meth()
	for(int i=0;i<3;i++)
		System.out.println(Thread.currentThread()+" " + i);


//Creating a class, A, which implements Runnable interface
class A implements Runnable
B obj;

A(B ob)

public void run()		//Entry point of the thread.
	//A synchronized block
	synchronized(obj) 	//synchronizing the object of B class
		obj.meth();	//call to meth() is synchronized

public static void main(String... ar)
	//Creating an object of class B
	B b = new B();

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

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

	//Starting the first thread
	//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

When to synchronize a block?

Synchronizing a block is useful in a situation when we want to synchronize an access to a class that doesn't have any synchronized method, and nor do we have access to its code to modify its method and mark it with the synchronized keyword.

Please share this article -

< Prev
Next >
< Synchronization
Unsynchronized multiple threads >


Please Subscribe

Please subscribe to our social media channels for daily updates.

Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page



Please check our latest addition