• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Threads and Locks: Few Doubts

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Sachin Dravid
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic