This week's giveaway is in the EJB and other Java EE Technologies forum. We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line! See this thread for details.
Can Some buddy help me to know the difference between a synchronized Method /synchronized block ? what different ways to declare a synchronized block ? and why do we call it a lock on object were we declare a method as Synchronized?
I am sure googling it will give you a lot of answers.
In short synchronizing blocks will try to obtain the lock of an object that you specify before it executes code. You can reduce the span of synchronized code this way and attempt to improve efficiency. A synchronized method will lock on the instance of the current object or the Class object that the current object belongs to. This depends on whether the method is static / non-static.
Good question, and a cause of much confusion. There is really only one form of
synchronization - the synchronized block of code. All you need to remember is
that before you operate on an object that another thread can also change, you
must secure its lock using synchronized. Marking a method as "synchronized" is
handy (less typing) but also misleading. We talk about synchronized methods
but it is really the current object that is locked, not the code.