Advertisement



< Prev
Next >



A Synchronized block




A class method marked with synchronized keyword avoids the conflict between two threads wanting to execute it at the same time, by allowing the first thread executing it to have its sole access, while the other thread waits for its turn. But what if we don't have access to its code of class to modify its method with 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 object of B class, where, ob is the reference to the object being synchronized. This avoid the conflict between the two thread that are trying to call a non_Synchronized_Method() method on its same object of B class.

Synchronizing the object of B class 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.


Advertisement




Program with a synchronized block.


We are creating two threads that are going to compete over to execute a method meth() of B class.
class B
{
public void meth()
{
	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.
{
	//Synchronized block
	synchronized(obj) 	//synchronizing the object of B class
	{			
		obj.meth();	//call to meth() is synchronized
	}			
}

public static void main(String... ar)
{
B ob= new B();

A thread1= new A(ob);
A thread2= new A(ob);

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

t1.start();
t2.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





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 with synchronized keyword.



Please share this article -





< Prev
Next >
< Synchronization
Unsynchronized multiple threads >



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