• Post Reply Bookmark Topic Watch Topic
  • New Topic

A question on multithreading  RSS feed

 
manish ghildiyal
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In my class there is a static method and a non-static method, both synchronized.
Now as locks for them would be different, so it is very much possible that different threads
are executing them simultaneously. If inside both methods I am trying to manipulate same data, doesn't
it means that synchronization fails here?

Or my understanding of locks and synchronization is flawed?

Manish
 
Chan Ag
Rancher
Posts: 1090
14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general, it is not a good idea to have synchronized methods that access static as well as non-static data. You have already stated the reason why.

doesn't it means that synchronization fails here?


Yes, in general and in most cases.

However, the actual impact of it may also depend on how you are accessing the shared data and how the shared data is declared and used. It may also greatly depend on the design of your class - other factors, the simplest of which is the other methods of your class.

Chan.
 
Khuzema Dharwala
Ranch Hand
Posts: 61
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your understanding is correct,static and non-statc methods synchronize on different locks hence they will never block each other accessing shared data thus causing synchronization failure.The rule to be kept in mind while implementing synchronization is non-static method should not be allowed to access static data because of the reason stated earlier.

Thanks...
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
manish ghildiyal wrote:If inside both methods I am trying to manipulate same data, doesn't it means that synchronization fails here?

No, it works flawlessly.

It just doesn't synchronise on the same object. Static methods synchronise on the class; instance methods on the object they're called on.

And just to add to Chan's advice:
In general, synchronization should be as brief and as focused as possible. So, if your method involves lots of logic to change something (which is generally why you're synchronizing), you may be better off executing the logic in an unsynchronized method or block, and passing the result(s) to something like a synchronized setter.

For more "transaction"-like changes, I'd advise using a ReentrantLock.

And better still might be to make your class immutable. Then you don't have to worry about synchronization at all.

Winston
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All the previous is true, but it is not impossible to use the same lock in both the static and instance contexts - but to do so you sacrifice the instance context locking (ie the synchronization scheme becomes static, and all instances of the same class become mutually exclusive.)

All locks (static or not) are associated with an Object. For the non-static synchronized methods the lock is this object. In the static synchronized methods the lock is the class object (ie, MyClass.class). So a static synchronized method is like this:

If you want the instance methods to become mutually exclusive with the static methods you would need to use the same object for locking. That object would have to be in the static scope (so the static method can see it). And it would be natural to continue to use the Class object. But that means you have to change your instance methods from being synchronized methods to using synchronized blocks:


You could (and I probably would) use a specific and dedicated object to lock on, just to be clear:

 
manish ghildiyal
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all for the inputs..it seems there is too much to learn as far as multi threading is concerned.

Manish
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
manish ghildiyal wrote:Thanks to all for the inputs..it seems there is too much to learn as far as multi threading is concerned.

Manish

Not too much to learn, it just takes some time and practice. Keep at it and you will get it all.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!