Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Unnecessary Synchronization  RSS feed

 
Nicky narayan
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi,
Can anyone tell me how can we avaid Unnecessary Synchronization ?



Thanks in advance.
 
Ninad Kulkarni
Ranch Hand
Posts: 802
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Nicky
It depends on situation. If this post is purely related to threads and synchronization you can check Threads-Synchronization forum
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JVM performance with respect to uncontested locks has become much better over the years, especially so with Java 6. It's now at a point where I would not worry about it unless you're dealing with highly contested locks (at which point you may want to abandon simple synchronized methods and blocks, and use some of the more advanced concurrency capabilities. If you do that, work through one of the Java concurrency books first, either Oaks/Wong or Brian Goetz.)

Are you facing an actual problem that prompts you to ask about this, or is it merely a precaution?
 
Nicky narayan
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Asked me in an interview.

Please clear me one thing-----actually how/where we will get unnecessary Synchronization ?

Please explain with an example.


Thanks in advance.
 
Adam Smolnik
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey.



Adam
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Synchronization is needed when you have mutable state (read: data structures) that are shared between multiple threads. So synchronization is unnecessary if a) the state does not change, or b) there's no chance of the state being accessed concurrently by more than one thread.

So in Adam's example, #2 is particularly silly, since fields declared in a method are not shared between threads.
 
Adam Smolnik
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey.

all the ones are "silly" or almost "silly" and serve ONLY as "bad examples" - every includes unnecessary synchronization.

Adam
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I realize that they are meant to illustrate what not do do. But #2 was different from #1 and #3, and I wanted to draw attention to it. There are reasons why one might have code like #3 (namely, if the code predates Java 5), but there was never a reason to have code like #2.
 
Adam Smolnik
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey.

Yes. #2 was the especially exaggerated example. I intended to highlight a mechanism of that problem.

Adam
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!