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

file lock or synchronized ?

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic