wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes synchronized mess up Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "synchronized mess up" Watch "synchronized mess up" New topic
Author

synchronized mess up

amrit singh
Ranch Hand

Joined: Nov 03, 2000
Posts: 42
hi all
could ne one pls explain these lines if possible with an example:
1)As long as a thread possesses the monitor or lock of an object no other thread can get the lock for that object. (Another thread may be able to lock a different object in the same class, however.)
2)(Static methods can also be synchronized.
In this case, the lock is placed on the java.lang.Class object associated with the object's class.)
Since there's only one lock per object, it's not just the synchronized method that can't be invoked by other threads.
It's any other synchronized method or block of code in that object.
Thanxxxxxxxx all
Santhosh Kumar
Ranch Hand

Joined: Nov 07, 2000
Posts: 242
Hi Amrith,
1. As long as a thread possesses the monitor or lock of an object no other thread can get the lock for that object
Once a thread get the lock for an object, no other threads can get the lock untill unless, lock be released explicitly from first thread. In below example, First thread get the lock and execute the infinite loop. So other thread trying to execute the same method will be in waiting for release of the lock (will not get that, so sad!!!).

1a. Another thread may be able to lock a different object in the same class, however.
See, there will be one lock for each object of a class. If you create two instances of a class, there are two locks. So first thread can get the lock for first object and goes to infinite loop. And mean while second thread can get the lock for second object and goes to infinte loop. Check out the below ex. little modification of above ex.

2. Static methods can also be synchronized.
In this case, the lock is placed on the java.lang.Class object associated with the object's class
Each and every class loaded into the JVM will be having an instacne of the class java.lang.Class. So when you try to access class members (static), you need to get the lock of this Class object not the lock of object of the class in question. So after that you are still free to get the lock on object of the class in question. See ex.

3. Since there's only one lock per object, it's not just the synchronized method that can't be invoked by other threads.
It's any other synchronized method or block of code in that object.
There will be a lock for an object not for a method in the class. If you have three synch methods, you need to get the lock of object before invoking the any synch methods. See ex.

I hope you got bit.
Santhosh.
[I added UBB CODE tags to your source code to make it more readable. Please try to use
them in the future. Learn more about UBB codes here - Ajith ]
[This message has been edited by Ajith Kallambella (edited December 06, 2000).]
Vidya Selvaraj
Greenhorn

Joined: Nov 21, 2000
Posts: 29
Amrit, This example was picked from Maha's Collection of Thread!. I added some more scenario's for my own clarification. Scenario 11 & 12 might be what you are looking for!

Correct me if this is wrong!
Thanx
Vidya
[This message has been edited by Vidya Selvaraj (edited December 06, 2000).]
amrit singh
Ranch Hand

Joined: Nov 03, 2000
Posts: 42
Thanxxxxxxxx guys.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: synchronized mess up