Popular Articles

During the terrific silence of that horrible night, they could not hear the footsteps of death. On the eighteenth night of a great war, they were sleeping
“Bottle, bottle everywhere; not a single one to provide water” Bottled water is the present era acquisition of safe drinking water to homes and offices
Beauty of Kashmir is breathtaking and thus this valley is rightly described as paradise on earth. Carpets of saffron resembling lotus petals are spread

Latest Articles

Beauty of Kashmir is breathtaking and thus this valley is rightly described as paradise on earth. Carpets of saffron resembling lotus petals are spread
Located in culturally important Bhavnagar district of Gujarat, Alang today is known worldwide for ship dismantling. It is world's largest ship dismantling
Our ancestors discovered the technique of cooking food. Earlier they used to consume food in its original state i,e., in its raw form, regardless of whether

Synchronized block is alternative mechanism for "synchronized" methods.If we inherihate the non synchronized blocks either from base class or interface inface into our derived class and the inheriated non-synchronized method is trying to accessed by multiple threads then we get inconsistent result.To avoid this inconsistent result, the derived class programmer is attempting to write "synchronized" keyword before non synchronized method to make the method is synchronized which is not possible because one cannot change the prototype of base class methods in the context of derived class. Hence the synchronized method concept is unable to the solve the above problem therefore to solve the above problem for getting the consistence we use a concept called "synchronized blocks". Synchronized blocks must be always written in the definition of non-synchronized method.

syntax:

synchronized(object of current class)

{

//Block of statements;

}

It is not possible to override static methods but it is possible to override the instance methods.synchronized blocks must be always written in non-synchronized instance methods only.

Example:

package ip;

public interface Account{

void deposit(int amt);

}

class SAccount implements  Account{

int bal=0;

public void deposit(int amt){

synchronized(this){

bal = bal + amt;

System.out.println("user Account bal="+bal);

}

}