Popular Articles

At one time Russia rivaled the USA as a global power, but then came Gorbochov and we all know that Russia broke up into 18 states. This was also the end
From the 8th century the Muslim invaders began to make forays into Hindustan and by the 12th century the entire North India and the throne at Delhi had
Privacy is an important thing in every section. Same goes for mobile phones too. People always have something to hide from others on their mobile phone

Latest Articles

Subhas Chandra Bose was a rival of Gandhi and during the war he threw his lot with the Fascist powers and joined them. He had a one point agenda to overthrow
At one time Russia rivaled the USA as a global power, but then came Gorbochov and we all know that Russia broke up into 18 states. This was also the end
I had in an earlier post pointed out that the great Hindu warrior  Hemu was struck by an arrow in his eye in the third battle of Panipat and lost

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);

}

}

  • No comments found