This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes Synchronized read writes to a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Synchronized read writes to a file" Watch "Synchronized read writes to a file" New topic
Author

Synchronized read writes to a file

Shridhar Raghavan
Ranch Hand

Joined: May 27, 2010
Posts: 71
Hi,

Had a query with synchronization in servlets. Had a requirement of making file accesses through a servlet synchronized. What i can think of making the read/writes in a synchronized block with the lock being the servlet class name. Is that fine and any other ways?

i.e.

synchronized(MyServlet.class){
// whatever
}
sudhir nim
Ranch Hand

Joined: Aug 29, 2007
Posts: 212

You can use singleThreadModal, or just don't have any class level variables in your servlet and you wouldn't need to synchronize.
or you should be able to use 'this' for synchronized block. But i would suggest, dont write synchronized blocks, instead make your servlet stateless.


[Servlet tutorial] [Servlet 3.0 Cook Book]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41068
    
  43
sudhir nim wrote:You can use singleThreadModal, or just don't have any class level variables in your servlet and you wouldn't need to synchronize. or you should be able to use 'this' for synchronized block. But i would suggest, dont write synchronized blocks, instead make your servlet stateless.

There are multiple problems with these bits of advice.

Firstly, don't use SingleThreadModel, ever. It does not guarantee thread safety, and completely kills concurrency.

Secondly, not having class level variables by itself does not guarantee thread-safety, either, and is actually not even required if you know what you're doing.

Thirdly, there's nothing wrong with using synchronized blocks as such. Just don't put the entire code in one because that kills concurrency.

Lastly, I'm not quite sure what you mean by "stateless" servlets, but assuming you mean no class variables - see #2.

To answer the question: synchronizing on the servlet class is fine. Since servlets are generally instantiated once, it amounts to the same as synchronizing on "this", but using the class makes sure the right thing happens.

And finally: If you're serious about multi-threaded code -and these days, who can afford not to be? web apps *are* multi-threaded- I strongly advise to work through one of the eminent books on Java concurrency (either Oaks/Wong or Goetz et al.). There are a *lot* more options than this post mentions and you really need to understand what's available to write effective and efficient concurrent code.


Ping & DNS - my free Android networking tools app
Shridhar Raghavan
Ranch Hand

Joined: May 27, 2010
Posts: 71
synchronizing on the servlet class is fine.


So the synchronization on the class file should serve my purpose presently? Also the other options in this thread have been discouraged?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41068
    
  43
Also the other options in this thread have been discouraged?

Not at all. But you were asking about synchronizing the file I/O on the servlet class (not class file), and that is perfectly viable.
sudhir nim
Ranch Hand

Joined: Aug 29, 2007
Posts: 212

I believe that having no class level variables is sufficient in most of the cases, especially in web applications, and if there's a situation where this isn't sufficient, the person will need good understanding of thread safety to handle it properly.

I keep hearing that dont ever use scriptlets, dont ever use expressions, and SingleThreadModal, why guyz at sun just dont remove it and lighten the burden on the guyz who provide implementations if all of these things are never supposed to be used.


But I agree with your view on not using SingleThreadModal though, and after reading again, 'no class variables' wouldn't be sufficient here.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12759
    
    5
SingleThreadModel, the invoker etc etc are in the API because of that famous reason "It Seemed Like The Thing To Do At The Time."

When all this was being worked out, the designers were working in totally new territory so it is not surprising that some ideas turned out not as useful as expected.

Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Synchronized read writes to a file
 
Similar Threads
FileWriter.write(String str)
about log() of GenericServlet
NX:should lock the database during the creating?
servlet context log info
Use a singleton or a "purely static" class?