About
thread synchronization I have deduced these things
CORRECT ME IF I AM WRONG: 1. method synchronization:
1) No other synchronized method can be executed of the
same object if any one synchronized method has acquired monitor.
2. object synchronization:
1) object synchronization is obtained when we acquire a lock on a object i.e
Once we acquire a monitor on object then no other synchronized method or synchronized block of
same object can be executed till monitor is released.
3.
method synchronization and
object synchronization is
same i.e they obtain object monitor , only way of implementation is different.
4. class synchronization is achived by synchronizing
static method of class i.e.
5. when class monitor is obtained then we can not executed any other
synchronized static method of that class, but we can execute any instant method, that instant method could be synchronized and we can execute synchronized block as these monitors are object monitor.
====================================================================
If I am right till now then I have some doubts(again please Correct If Wrong - CIW), PLEASE clear these doubts:
Doubt01. If I have a static member object reference and I get a lock on it then is it a objec monitor or class monitor
exa:
i) IS THIS(block01) A class monitor OR object monitor , as obj is static?
ii) while executing block01 can method01() be executed ?
iii) while executing block01 can staticMethod01() be executed ?
iv) can any other object of MyClass execute thses methods and block?
Doubt02.
i) let us say I have created an object of Class01 and assigned it to object ref 'obj01' of Class01. Now while executing 'block02' can I execute method01() of obj01? I think YES
ii) I think while executing block02 , we can not execute any synchronized method or block of object refernced by oc.
iii) What if I assigned object to some other refernce var and try to execute synchronized method while it is in monitor? I think it will go in wait state as object is same.
Doubt03.
As per my knowledge of point 5.
if we can execute instant synchronized methods of class monitor then if we change any static varible in instant method then there will be data corruption?
I mean:
so when staticMethod01() is in monitor then method01() can be executed by some other thread and as they are sharing data then there will be corruption? Am I right ?
Correct me if I am wrong ....
I know it has become very long ... but i want to clear alll my doubts abt synchronization. For me also it was tiring... hope you all there will understand me.
THANKS in advance !
[ January 17, 2002: Message edited by: ravish kumar ]