• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

To declare Syncronized or Not, always the question!

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,

Here's a quick question about Syncronized methods.

Here's the method:


I call this code from a servlet, I call the method statically and I am sure that I am always passing different resources. Does this need to be syncronized?

[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ May 27, 2004: Message edited by: Dirk Schreckmann ]
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's a static method that doesn't use the state of any object. What's to synchronize?

I'm moving this to the Threads and Synchronization forum...
 
Paul Duer
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Dirk!

What I don't understand is this. So I have this static method. It saves a string of data to the filesystem. It creates a FileWriter, then it saves it, then it closes it.

So if it's Static, that means there's only ever 1 copy of this code in memory to serve all requests. In my servlets, I call this method at the end, to save my file that I work on in the servlet.

My question is this, with multiple users calling this method, and different servlets running it possibly at the same time, what's to stop data from mixing? If there's only one version of it running, is it a bottleneck?
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


So if it's Static, that means there's only ever 1 copy of this code in memory to serve all requests.



The bytecodes comprising both instance and static methods will be loaded only once per loaded class.



If several threads call an unsynchronized method, they will be able to execute it concurrently. Because there is no shared data that might be modified by a thread in an inconvenient moment for another thread, the only source of problem might be the attempt of writing to the same file. But you you said it was not the case.
 
We're being followed by intergalactic spies! Quick! Take this tiny ad!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic