Sachin Dravid wrote:Hello All, warm greetings to all.
I have gone through SCJP - Kathy Sierra book and online articles, still the below doubts still puzzle me. It would be helpful if geeks here can throw some light on it:
1) Suppose we have a synchronized block in a object (say an instance of class ABC), and thread A is accessing it. There is a method call from this synchronized block. The method belongs to a different object (say an instance of class XYZ) (i.e. the method does not belongs to the class in which synchronized block is present.) In such case:
a) If the method in object XYZ is marked as 'synchronized', I guess thread A will obtain a lock on XYZ also. Am I correct here?
b) If the method in object XYZ is not marked as 'synchronized', what will happen then? Will thread A still acquire the lock on XYZ?
2) Consider a class DEF where a static field is being accessed by a non-static synchronized method. We create 2 instances of the class - DEF_01 and DEF_02. Now, thread B is accessing the non-static method of DEF_01 and hence has obtained a lock on DEF_01, and another thread C is accessing non-static method of DEF_02 and so has obtained a lock on DEF_02. However, the method is trying to change the value of same static variable. What will happen in such case?
3) Another scenario is somewhat inverse of the above scenario: class JKL has an instance variable which is accessed by a synchronized non-static method as well as synchronized static method.
Now since the locks acquired by these 2 methods - object level lock and class level lock - will not interfere with each other. What will the result of two threads are independently these methods? One thread accessing static method and another accessing non-static method and both of them trying to change the value? What will be the result?
Steve
Sachin Dravid wrote:3) Another scenario is somewhat inverse of the above scenario: class JKL has an instance variable which is accessed by a synchronized non-static method as well as synchronized static method. Now since the locks acquired by these 2 methods - object level lock and class level lock - will not interfere with each other. What will the result of two threads are independently these methods? One thread accessing static method and another accessing non-static method and both of them trying to change the value? What will be the result?
Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
2) Consider a class DEF where a static field is being accessed by a non-static synchronized method. We create 2 instances of the class - DEF_01 and DEF_02. Now, thread B is accessing the non-static method of DEF_01 and hence has obtained a lock on DEF_01, and another thread C is accessing non-static method of DEF_02 and so has obtained a lock on DEF_02. However, the method is trying to change the value of same static variable. What will happen in such case?
Since the data being accessed is shared (by virtue of being static) but the locks being used is not (because the two threads are synchronized using different objects) the data is unprotected, and problems can arise.
Sachin Dravid wrote:So, my question is, will the instance variable be corrupted, if both the methods (static and non-static methods) are changing the value of that variable? Is there any strategy to prevent this from happening?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Consider Paul's rocket mass heater. |