• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Sychronization

 
Ranch Hand
Posts: 10192
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,

Can anyone please tell me what it means by saying,

"When you synchronize a method, the object used to invoke the method is the object whose lock must be acquired. But when you synchronize a block of code, you specify which object's lock you want to use as the lock.

Guys I'm faling to understand what it means. Can anyone please tell me this with an example for synchronized block code trying to be accessed by multiple threads??

Regards,
Jothi Shankar Kumar. S
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only one thread can access a synchronized method. Where as if you were to synchronize a block of code, many threads can use the method, but only one thread can use that block of code.

Ex:

public synchronized void method() - only one thread can execute here



You are advised to use the block method instead since it is more efficient. That way you have more code that is not synchronized and can be executed in parallel (assuming that code is thread safe).
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear John,
Though what you have stated is extermely true but it seems that your answer is not very specialized towards the question raised by jothi.

When you synchronize a method, the object used to invoke the method is the object whose lock must be acquired


what I think is for executing a syncronized method of an object you need to aquire the lock for the same object in which the method is defined(same line restated).Lets have an example

To execute the method mymethod() by a thread the thread must aquire a lock on the MyLockEx instance.So every time it is the same object in which the syncronized method is defined should be used as a lock to invoke the syncronized method defined within it.
This above code is similar to a syncronized block having this as a argument like

Doing so will make the method more flexible as you are free syncronize the statements of your choice not all the method ,as John stated in this answer.

But when you synchronize a block of code, you specify which object's lock you want to use as the lock.


While using the syncronized block you can use the object of your choice as a lock. .Two benefits of syncronized blocks .Isntit?
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
I am agree with Sanjeev Singh he has poured his knowledge on your problem.
I am going to explain basic of Synchronization.
Synchronization means set threads in a sequence that no more than one thread can access a single resource at a moment.So java people launched this concept.There is no locking ability in java.it is only mechanism .Through java(JVM) you can only tell to operating system that we want lock on this object.So come on main topic

Synchronised a method and Synchronised a block are same having no difference. Same concept will be apply on method synchronisation as well as on block .Actually loading time every information regarding class load in memory.So method is member of class having some name .So it is already set by Java people that if Synchronised a method means lock must be accuired by that accessor object.but you think if need is to safe only 4 lines out of 50 lines of that method. then will you Synchronized whole method? So at this situation java people gave another concept means Synchronized block of code which is thread-unsafe.
By doing this you will get performance inhancement.
i think there is no need to explame it through example because Mr. Sanjeev Singh has explained it very well.

Thanks to John for asking it.
thanking to Sanjeev for nice explanation .
[ November 04, 2006: Message edited by: Prashant kumar Singh ]
 
Your mind is under my control .... your will is now mine .... read this tiny ad
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
    Bookmark Topic Watch Topic
  • New Topic