aspose file tools*
The moose likes Threads and Synchronization and the fly likes Threads and Locks: Few Doubts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Threads and Locks: Few Doubts" Watch "Threads and Locks: Few Doubts" New topic
Author

Threads and Locks: Few Doubts

Sachin Dravid
Greenhorn

Joined: Jan 21, 2013
Posts: 12
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 Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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?

Yes, that is correct
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?

No, the thread will enter the method without accessing any synchronized locks. The thread will still hold the lock it had from the synchronized block in ABC, but will do nothing with any other locks.

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.

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.

How exactly is that achieved? How do you access the instance value from a static context?

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?

Hard to say, show us some code so we can better tell the scenario you are describing.


Steve
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

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?

As Steve has already pointed out, you cannot access instance variables via static method.

However, what if you have a static variable - which can be modified via two methods - one is just synchronized (instance method) and another is static synchronized (class method)? I guess this is your question.

The answer is: since static synchronized method obtains a lock on '<classname>.class', and synchronized method obtains lock on 'this', those two methods do not block each other.

That is - it is possible to write two separate synchronized methods (static and non-static) which modify static variable - and since those methods do not block each other, data corruption is possible.

I hope I've answered your question.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Sachin Dravid
Greenhorn

Joined: Jan 21, 2013
Posts: 12
Thanks a lot Steve and Anayonkar. Those were really helpful.

Steve:
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.


Is there any way to prevent to prevent the static field from getting corrupted? I know that the best practice is to access static fields via static synchronized methods and non-static fields via non-static synchronized method. But if the situation arises where I two threads are accessing the same static field through different instances of same class, is there any workaround to prevent the static data from being corrupted?

Anayonkar:

Thanks man. Although that was not exactly my question. By "static method accessing instance variables", I meant static methods accessing instance variable via instance of the class. Sorry for not stating it clearly.

Since the variable is instance variable, it can be accessed by synchronized static methods via the instance (E.g. objectReference.fieldName), while the same instance variable can be accessed by non-static synchronized method also. Now my question is, Suppose thread A is accessing the synchronized static method, the thread will obtain a class level lock. At the same time, another thread B is accessing the non-static synchronized method, so thread B will get object level lock. Now, as per SCJP - Kathy Sierra book, "a static synchronized method and a non-static synchronized method will not block each other—they can run at the same time"

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?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

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?

Many, but the easiest is to have only one way (ie, one method) of updating the variable, so if it's a static variable, you have a static setter method, and all other code (including the non-static method) uses that method to update.
Unfortunately, the language can't enforce that, so you have to do it yourself.

PS: This is just one more reason why static variables are not usually a good idea.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads and Locks: Few Doubts