I am trying to find answer for the following ,for the past couple of days ,but couldnt find comprehensive answer
I have a custom JSP tag class which handles a web form submission ,captures data and write it to same file in the filesystem.
As all web applications,this can be triggeredsimultaneosly ,and i fear that multiple threads would be in action handling each of the submission (we all know thats how Servlet works.)
ISSUE: - A File object can be opened by one thread for writing and at same time another thread might try to access and fail and throw an exception
So i thought of synchronizing (on the object ) should solve the issue , but read some where that jsp engine would have pool of jsp tag objects, so i am afraid that synchronized (this) wont work and it should be changed to
synchronized (this.getClass() [/b]
Can anyone please provide any light on this .
Thanks a bunch in advance
Else you can perform file system locking. Create a lock file when proceeding to logging action. Other calls will be blocked till the lock is cleared.
When logging is done, release lock. Depends on your scenario, is there any read on these log files from jsp?
Joined: Dec 20, 2007
Appreciate the reply.
As per j2 api,
File locks are held on behalf of the entire Java virtual machine. They are not suitable for controlling access to a file by multiple threads within the same virtual machine.
File-lock objects are safe for use by multiple concurrent threads.
So following are my questions
1. what is the difference between multiple threads and multiple concurrent threads?
2. In the API its mentioned that resultant lock is system dependant.So does this work well in UNIX?
3. Implementing File lock will the threads wait till the they acquire the lock or will they just quit processing when a file is locked ?
Firstly, writing to a file from a jsp is truly against an MVC architecture (in case you bother about it) and really not advisable.
Coming to your question:
Writing to the same file from different threads essentially means that one of the thread has to wait till the time the other has finished writing. This can be achieved in 2 ways:
1) Make the code in such a way that only one thread writes to the file at a time (Thats what you are trying to do by putting a sync block)
2) Have a different class that writes to the file. All the other (HTTP) threads just queue their requests to this class and does not actually write it to the file in the same HTTP thread. A different thread clears the queue and persist the information to the file.
If you follow 2) and assuming you only have one worker thread per file, you can use FileLock to prevent concurrent access to this file.
Any kind of locking done in code (aka synchronize block) breaks off in a clustered scenario so it is not really the way to go.
Sudhakar Duraiswamy wrote: 3. Implementing File lock will the threads wait till the they acquire the lock or will they just quit processing when a file is locked ?
There is a method "trylock" that does not block as opposed to "lock" which blocks till the lock is available.