• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

synchronised(this)

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ranchers,

Can anyone explain me what is difference between synchronise(this) and block/method synchronisation with example please...
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Karthick

That is very cool analogy for thread and object lock.
It helps me.
Thank you.
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Karthick Ravi
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thulasi Arasu wrote:Karthick

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



No worries dude. Anytime.
reply
    Bookmark Topic Watch Topic
  • New Topic