File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread synchronization ..... CIW 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 "Thread synchronization ..... CIW" Watch "Thread synchronization ..... CIW" New topic

Thread synchronization ..... CIW

R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5382
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

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?


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.

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 ]

"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5382
I think I got what I was looking for ... plz visit this and this too as both are related and give almost full information about thread synchronization block or class monitor.
one request ::: plz read full page ... I remember one of my friends advise that even do not beleive God, they also do mistakes. Do it yourself and then beleive(by SAKKU)
AW there Maha also is wrong initially so read full page else you will have confusion like this one.
After reading those links if you guys still have stamina then go through this as this will work as
refresher as there is nice silent(censored) ego clash is going on
Thanks a lot everyone
[ January 17, 2002: Message edited by: ravish kumar ]
I agree. Here's the link:
subject: Thread synchronization ..... CIW
It's not a secret anymore!