• 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

Unnecessary Synchronization

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



Thanks in advance.
 
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey.



Adam
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey.

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

Adam
 
it's a teeny, tiny, wafer thin ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic