File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes synchronised(this) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "synchronised(this)" Watch "synchronised(this)" New topic
Author

synchronised(this)

Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
Hello Ranchers,

Can anyone explain me what is difference between synchronise(this) and block/method synchronisation with example please...


Thanks<br />Dinesh
Karthick Ravi
Greenhorn

Joined: Feb 27, 2008
Posts: 13
Synchronzed method

If multiple threads may access a method concurrently and you want the method to be accessed one by one then you synchronize that full method.



Synchronized block

If multiple threads may access a resource (primitives or objects) or a code and you want the resource to be accessed one by one then you lock them using the synchronized block based on an object. Another thread may access that piece of code only if the current thread releases the object.

ex


String s = new String("Hello ");


//somewhere in a method
synchronized(s)
{
s = Thread.currentThread.getName(); //1
System.out.print(s + " "); //2
}


And you start 3 threads Thread1, Thread2, Thread3, with the above block you can be sure that the three threads will execute the above block one by one. The output can be "Thread1 Thread2 Thread3" or which ever thread manages to get the lock. If there is no lock, an example scenario might be Thread1 running "1" and Thread2 running "2" but the output is "Thread1" even though thread2 executes "2".



Think of this more like a crown for a king.


There is only one crown (object) but there can be many princes want to become a king (thread). But only one prince can wear that crown and become the king (object lock). And other princes can become the king only if the current king loses the crown (object release).


Hope this helps. Chilax dude.

Thulasi Arasu
Greenhorn

Joined: Apr 20, 2009
Posts: 10
Karthick

That is very cool analogy for thread and object lock.
It helps me.
Thank you.
Bob Wheeler
Ranch Hand

Joined: Apr 24, 2009
Posts: 317
Dinesh Tahiliani wrote:Hello Ranchers,

Can anyone explain me what is difference between synchronise(this) and block/method synchronisation with example please...

Do you mean this syntax (with "z")?

But this IS block synchronisation. So no difference.
I have never heard of a method synchronise(this).

cheers
Bob


SCJP 6 - SCJD - SCWCD 5 - SCBCD 5
JavaEnterpriseEditionFaq - TomcatFaq
Karthick Ravi
Greenhorn

Joined: Feb 27, 2008
Posts: 13
Thulasi Arasu wrote:Karthick

That is very cool analogy for thread and object lock.
It helps me.
Thank you.


No worries dude. Anytime.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: synchronised(this)