Author | Topic: Synchronized block |
javaBiginner unregistered |
posted March 30, 2000 11:44 AM
In Marcus' tuitorial, it said: When a synchronized block is executed, it's object is locked and it cannot be called by any other code until the lock is freeed. Here, we know for sure that the synchronized block can't be called. But how about the unsynchronized methods? Can any other code call the unsync methods whild the sync block is called?
|
Jim Yingst sheriff |
posted March 30, 2000 12:38 PM
Yup, absolutely. If this is a problem, make sure the data can only be accessed through synchronized methods.
|
Paul Keohan greenhorn |
posted March 30, 2000 06:14 PM
This confuses me. If one method out of three is synchronized, why do we say the object is locked? I would say just that method's data is locked.
|
maha anna bartender |
posted March 31, 2000 09:48 AM
Paul, I tried to answer. Please read the foll. I also handgrafted an example program (after scratched my head a lot ). Participate in this discussion. I post the concept in this post and the Example program in the very next post so that it doesn't look too long at first. regds maha anna Obtaining lock on an object --------------------------- - Each object has an associated lock with it. - This lock is useful ONLY for executing SYNCHRONIZED INSTANCE METHODS as foll. class MyClass { synchronized void instSyncMethod1() {} synchronized void instSyncMethod2() {} void ordinaryMethod() {} } - Whenever a thread tries to execute a synchronized block it shd FIRST obtain this mutually exclusive lock. I am saying MUTUALLY EXCLUSIVE here meaning, at any point of time ONLY ONE thread can have this lock associated with this object. - So assuming, Thread1 obtains the lock for an object (object1) of this "Myclass" class and starts executing the code inside the instSyncMethod1(). So now the lock is with Thread1 Scenario - 1 Scenario - 2 Scenario - 3 Scenario - 4 static synchronized void staticSyncMethod1() {} } [This message has been edited by maha anna (edited March 31, 2000).]
|
maha anna bartender |
posted March 31, 2000 09:50 AM
Here comes the example program.
[This message has been edited by maha anna (edited May 12, 2000).]
|
Sushma greenhorn |
posted May 11, 2000 10:02 AM
Hi, Maha Anna's explanation is very good. But, what if Thread1 is executing one of the 3 methods of that object, and can Thread2 access the non sycronized method of that same object, i mean 'this' object??? Please explain this. Regards, Sushma
|
Herbert Maosa greenhorn |
posted May 11, 2000 11:28 AM
Maha, I clearly understand your explanations and it has helped clear off a lot of problems I had in percieving this. I however want to understand one more thing : When the synchronized method is static, does this mean that once one thread say Thread1 is executing in this method, then another thread can not execute even the non synchronized methods of this object ? Thanks in advance.
|
maha anna bartender |
posted May 11, 2000 01:25 PM
Sushma,Herbert, You got the case I left out Herbert. Nothing restricts a thread executing a non_sync_method of any object of any class.. As you asked I included this case also. Here is the output. Sushma, regds
|
Sushma greenhorn |
posted May 12, 2000 12:18 PM
Maha Anna, Thanks you very much for ut detailed explanation. I just want to summarize some facts about synchronization, can u pl look into this and lemme know if i'm correct?? i)Synchronized Methods::: A thread can access a non synchronized methods of an object even if some other thread has the lock on that object. ii) Synchronized Blocks::: A thread can't access any data whether it be synchronized /non-synchronized methods of an object, if it doesn't have the lock of that object(since this object is locked as a whole). iii) Synchronized static methods:: A thread can't access any data whether it be synchronized /non-synchronized methods of an object,and all the objects of that class if some other thread has the lock of this object thru the static synchronized method(since this class is locked as a whole). Thanks a lot and looking forward for ur help...
|
maha anna bartender |
posted May 12, 2000 01:29 PM
Sushma, Take a cup of steaming coffee before going down (i)Synchronized Methods::: A thread can access a non synchronized methods of an object even if some other thread has the lock on that object. This statement is true. I presume , you are referring to synchronized instance methods here. This statement is ALSO true even static synchronized methods. As I said before nothing restricts a thread running a ordinary (non_sync) method whetehr that object's lock/object's Class is currently with someone else or not. ii) Synchronized Blocks::: A thread can't access any data whether it be synchronized /non-synchronized methods of an object, if it doesn't have the lock of that object(since this object is locked as a whole). This statement is not clearly worded. Hmmm.. A synchronized block means what? Like the foll. I have given the explanation as part od the code. I think you are missing this point. A synchronized block means it may be synchronized on the current object/any other object/you can obtain the lock of the Class of this current object / or in fact any Class level object. Go through the example below.
iii) Synchronized static methods:: A thread can't access any data whether it be synchronized /non-synchronized methods of an object,and all the objects of that class if some other thread has the lock of this object thru the static synchronized method(since this class is locked as a whole).
While understaning the lock concepts is important R&H also says in their book that class level locks are not covered by the SCJP2 exan regds [This message has been edited by maha anna (edited May 12, 2000).]
|
Sushma greenhorn |
posted May 12, 2000 02:37 PM
Thanks a lot maha Anna... I think i got my concepts cleared. Thanks a lot for ur patience ... Sushma
|
| | |