Popular Articles

  I was a small boy when John Wayne died in 1979. But even at that age  I loved the films of John Wayne and watched them in cinema halls with
It is imperative that we have a relaxing atmosphere within the four walls that we call our home that has a calming effect on us and remains Stress-Free! How
Role of Inner Strength in our Life The inner strength should be the most important part of every human being since no one can do a thing properly without

Latest Articles

Life is not that easy Most of us cannot tolerate untidiness around us be it our home or office. In fact we hate our foolishness and we curse ourselves
Role of Inner Strength in our Life The inner strength should be the most important part of every human being since no one can do a thing properly without
  I was a small boy when John Wayne died in 1979. But even at that age  I loved the films of John Wayne and watched them in cinema halls with

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