• 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

Class Level Vs Object Level Synchronization

 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the below example

1. Static Variable Lock

In this we are obtaining lock at the static object "object" level. Now

2. Class level Lock

I think both 1 and 2 are same. The static variable "object" is thread safe.
Then what is the difference?

I think both are same. If we have more then one static variable to be synchronized then we need to go
for class level lock or we can go for static variable level lock right?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They are not exactly the same - you're locking on different objects in each of the examples.

In the first example, you're locking on object.

In the first part of the second example, you're locking on the Test object that you'd call the obtainLock method on.

In the second part of the second example, you're locking on the Class object of class Test.

If there's nothing else in class Test, then in the end all three variants have the same effect - you're locking so that only one thread at a time will be performing the code that's synchronized. Note that since you're using ConcurrentHashMap, the locking is unnecessary - ConcurrentHashMap is already designed to be thread-safe.

Chandra shekar M wrote:If we have more then one static variable to be synchronized then we need to go for class level lock or we can go for static variable level lock right?


The answer to this question is not a simple yes or no; it depends on what the expected functionality of the class is. If you need to do two actions that always have to be done together, without the chance of another thread doing something else in between the two actions, then you'll need to synchronize.
 
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

Jesper de Jong wrote:If there's nothing else in class Test, then in the end all three variants have the same effect


@Chandra: It may be worth adding that the only reason they are is because 'object' is static, and static variables are almost invariably bad.

Winston
 
Chandra shekar M
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If you need to do two actions that always have to be done together, without the chance of another thread doing something else in between the two actions, then you'll need to synchronize


at Class Level right?

1.If we have multiple variables doing different things. Then we can breakup in to different methods and still do it at the object level.
2.If we want both the actions to be done together by a single thread as said in the above quote. Yes then it has to be at the class level.

Thanks Jesper and Winston
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, there is no reason why you would need to synchronize "at class level" for the points that you mention.
reply
    Bookmark Topic Watch Topic
  • New Topic