Sridhar Gudipalli|SCJP 6.0
SCWCD objectives
My question is.. whenever s1 got lock on Class object how the main bMethod is getting executed? (I know static synchronized acquires lock on monitor associated with Class and non-static synchronized method acquires lock on monitor associated with instance)
Sridhar Gudipalli|SCJP 6.0
SCWCD objectives
Sridhar Gudipalli wrote:1) Whenever s1 thread started it acquires a lock on class.
2) Whenever s2.bMethod() is called it tries to get the lock on the instance.
My confusion was..... If this s1 holds lock on class, again how come s2 can acquire lock on that instance? Lock on Class means only lock on "static synchronized methods" or all the synchronized methods in class?
Lock on Class means only lock on "static synchronized methods"
Sridhar Gudipalli|SCJP 6.0
SCWCD objectives
Threads is the area that I didnot do well in SCJP 6.0. So, trying to get hold of it.
BEE MBA PMP SCJP-6
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
BEE MBA PMP SCJP-6
.if thread acquires lock on one of the object instance of a given class will not prevent some other thread to acquire lock of a class-object instance
<a href="http://www.sealordresortarnala.com" target="_blank" rel="nofollow">http://www.sealordresortarnala.com</a>
Gladwin Burboz wrote:
Lock is the feature of an object, not of a method or a class or a thread. Thread that needs exclusive access to a particular instance, acquires lock of that object by using synchronized method or a block.
For a given class, you can have multiple object instances each having it's own lock.
For a given class, you can have one and only one class-object instance having only one lock.
Thread acquires lock of an object instance or class-object instance.
Only one thread can acquire lock of a given object at a time.
Thread calling synchronized instance method will acquire lock on that object instance.
Thread calling synchronized static method will acquire lock on class-object instance.
Having said above,.if thread acquires lock on one of the object instance of a given class will not prevent some other thread to acquire lock of a class-object instance
Apurva Goyani wrote:
Hi Gladwin,
I agree to what you said, but just one question ponders me is that when two threads have access to two methods (1 static and other non-static respectively) simultaneously how it will access static field (val from StaticSynchronizedDemo), won't it will lead to violation of integrtiy?
However if you are using Java 5 or above, it will be wise to use Lock object instead.
<a href="http://www.sealordresortarnala.com" target="_blank" rel="nofollow">http://www.sealordresortarnala.com</a>
<a href="http://www.sealordresortarnala.com" target="_blank" rel="nofollow">http://www.sealordresortarnala.com</a>
Gladwin Burboz wrote:
Apurva Goyani wrote:
Hi Gladwin,
I agree to what you said, but just one question ponders me is that when two threads have access to two methods (1 static and other non-static respectively) simultaneously how it will access static field (val from StaticSynchronizedDemo), won't it will lead to violation of integrtiy?
You can synchronize access to this variable using synchronized block that should attempt to acquire lock on class object as shown below.
But this has not been the case in the example StaticSynchronizedDemo(the very first thread) which has not used such lock for accessing the static variable.
BEE MBA PMP SCJP-6
Jim Hoglund wrote:Gladwin : My question is about the "since Java 5" part of your comment,
since object locking has been around much longer than that.
Apurva Goyani wrote:But this has not been the case in the example StaticSynchronizedDemo(the very first thread) which has not used such lock for accessing the static variable.
BEE MBA PMP SCJP-6
Jim Hoglund wrote:Mike : Thanks. I missed the "Lock" and read "lock" instead. But this leads to another
question. Given the original post, why should Lock be used over the simpler object
locking mechanism (ref: the "keep-it-simple" principle)?
BEE MBA PMP SCJP-6
BEE MBA PMP SCJP-6
LOOK! OVER THERE! (yoink) your tiny ad is now my tiny ad.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|