Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

To declare Syncronized or Not, always the question!

 
Paul Duer
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • 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 ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic