• Post Reply Bookmark Topic Watch Topic
  • New Topic

file lock or synchronized ?

 
Maggie Taylor
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi !

I have this try-catch block in a servlet. Whenever a client makes a call to this servlet, the log file gets updated with a new entry.

Now there can be more than one client making a call at the same time. I want to make sure that, when the log file is getting updated (during first call to the servlet ), there's no new instance that tries to make another entry into the log file (made by a second call to the servlet). Do I need to use synchronized or FileLock ?

Thanks.

 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps it's time to switch to using a real logging API, like log4j?
 
Maggie Taylor
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm still a novice, and I'm trying to figure out how to go about with Synchronization / FileLock. I would be able to appreciate advanced topics like log4j a little later.

thanks for the suggestion..
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then hopefully this code you are writing is just for your own education and is not going to be run in a real environment.

If you want to arrange it so that only one thread of execution can run a certain block of code at one time, then you synchronize that block of code. Here's the Sun tutorial about threads:

http://java.sun.com/docs/books/tutorial/essential/threads/index.html

There's a lot of information there but basically your servlet is running in an environment with many threads. Skip down to the part about synchronization and read that first, but if you're going to be writing servlet code you really need to understand the rest of the tutorial.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!