aspose file tools*
The moose likes Threads and Synchronization and the fly likes To declare Syncronized or Not, always the question! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "To declare Syncronized or Not, always the question!" Watch "To declare Syncronized or Not, always the question!" New topic
Author

To declare Syncronized or Not, always the question!

Paul Duer
Ranch Hand

Joined: Oct 10, 2002
Posts: 98
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

Joined: Dec 10, 2001
Posts: 7023
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...


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Paul Duer
Ranch Hand

Joined: Oct 10, 2002
Posts: 98
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

Joined: Jul 03, 2001
Posts: 2120

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.


SCJP2. Please Indent your code using UBB Code
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: To declare Syncronized or Not, always the question!