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

}

}


Like it on Facebook, Tweet it or share this article on other bookmarking websites.

Comments (0)

There are no comments posted here yet

iPhone 5 is worth for the price?

iPhone 5 is worth for the price? share your oponion  

No answer selected. Please try again.
Please select either existing option or enter your own, however not both.
Please select minimum 0 answer(s) and maximum 3 answer(s).
/polls/computers-and-technology/6116-iphone-5-is-worth-for-the-price.json?task=poll.vote
6116
radio
[{"id":"21061","title":"yes its worth","votes":"47","type":"x","order":"0","pct":43.93,"resources":[]},{"id":"21062","title":"No, not at all","votes":"33","type":"x","order":"0","pct":30.84,"resources":[]},{"id":"21063","title":"i don't know","votes":"27","type":"x","order":"0","pct":25.23,"resources":[]}] ["#ff5b00","#4ac0f2","#b80028","#eef66c","#60bb22","#b96a9a","#62c2cc"] ["rgba(255,91,0,0.7)","rgba(74,192,242,0.7)","rgba(184,0,40,0.7)","rgba(238,246,108,0.7)","rgba(96,187,34,0.7)","rgba(185,106,154,0.7)","rgba(98,194,204,0.7)"] 350
bottom 200
No married couple wants to end up getting divorced. It is not like they have planned for it. They try to put up with their partners for as long as they
Due to our modern lifestyle, we feel that digestion related disorders are a common problem. Thus, we neither give importance to them nor seek any help
The bond of marriage brings the two people together. Initially, everything may seem okay and both of them slowly start discovering each other in the journey.