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 Synchronization 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 "Synchronization" Watch "Synchronization" New topic
Author

Synchronization

Vinu Menon
Greenhorn

Joined: Jan 26, 2009
Posts: 20
I have few doubts in Synchronization.

1)When we enter a synchronized non-static method we acquire a lock for an object.Does that mean the entire object is locked like both synchronized and non-synchronized methods?and what does object lock exactly mean?

2)Does a call to a non-synchronized method from a synchronized method make the calledmethod synchronized? for example



does getName method become sychronized?



Thanks
Vinay.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

Vinu Menon wrote:I have few doubts in Synchronization.

1)When we enter a synchronized non-static method we acquire a lock for an object.Does that mean the entire object is locked like both synchronized and non-synchronized methods?and what does object lock exactly mean?


When you call a synchronized non-static method, the current thread acquires a lock on the object with which the method was called. This means that now no other thread will be able to execute synchronized non-static methods on that object. Other threads can however execute non-synchronized methods on the object. So the lock basically means that on that object no other thread can now execute synchronized non-static methods unless the current thread releases the lock either by calling wait or end of execution of the synchronized non-static method...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

Vinu Menon wrote:Does a call to a non-synchronized method from a synchronized method make the calledmethod synchronized? for example



does getName method become sychronized?


No getName will not be synchronized. Other threads can still call getName on the same object with which doStuff was called by some other thread. But remember that when getName is called from doStuff, then the thread that called doStuff still holds the lock on the object with which doStuff was called. The lock is not released when getName is called as doStuff has not completed. So you can call other synchronized methods from getName. This is what I mean

Vinu Menon
Greenhorn

Joined: Jan 26, 2009
Posts: 20
ok now i got it..Thanks Ankit.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Synchronization