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 threads synchronized methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "threads synchronized methods" Watch "threads synchronized methods" New topic
Author

threads synchronized methods

girish
Greenhorn

Joined: Apr 03, 2000
Posts: 19
I am a bit confused about locks obtained by threads.
My doubt is:
When a thread obtains lock on particular object ,
can other thread execute another method of that object which is not declared synchronized
Please help
Gary Smith
Greenhorn

Joined: Apr 03, 2000
Posts: 16
In short, yes.
Be clear about the difference between synchronised methods and synchronised blocks. The former lock the monitor of the object (i.e. 'this') whereas the latter can synchronise on 'this' or any other object you specify.
So, if you have a thread running in a synchronised method of an object, then other threads that attempt to synchronise on 'this' or enter other synchronised methods in the object will be blocked _but_ other threads can enter non-synchronised methods without a bother, as would happen if no synchronisation was used at all. That is, it is the threads that are blocked not the object.
girish
Greenhorn

Joined: Apr 03, 2000
Posts: 19
Thanks Gary.That really solves my problem.
Sushma
Ranch Hand

Joined: Feb 24, 2000
Posts: 36
Hi,
Can u pl throw more light on 'locking on the monitor of the object'?
A synchronised method can call a non synchronised method, so if this is true, what if some other thread which could access thsi method changed the data in this and this was called by the synchronized method???
I'm really confused....Pl help me.
Regards,
Sushma
[This message has been edited by Sushma (edited May 11, 2000).]
[This message has been edited by Sushma (edited May 11, 2000).]
bongadi
Greenhorn

Joined: Apr 17, 2000
Posts: 23
And you are right. A thread without the lock can run
unsynchronized method "and change the data".

So, all the methods which are modifying and accessing the data in
critical section should be synchronized to prevent
data corruption.

Originally posted by Sushma:
Hi,
Can u pl throw more light on 'locking on the monitor of the object'?
A synchronised method can call a non synchronised method, so if this is true, what if some other thread which could access thsi method changed the data in this and this was called by the synchronized method???
I'm really confused....Pl help me.
Regards,
Sushma
[This message has been edited by Sushma (edited May 11, 2000).]
[This message has been edited by Sushma (edited May 11, 2000).]


[This message has been edited by bongadi (edited May 11, 2000).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: threads synchronized methods