I am in a situation where multiple threads (from the same JVM) are writing to the same file (logging by using Logger). I need to delete this file at some point, and next use of logger will create the file and log.
The logging library is synchronized, therefore I do not need to worry about concurrent logging to the same file.
But... I want to add an external operation which operates this file, and this operation is to delete the file, therefore I have to somehow synchronize the logging (Logger) with this delete operation because I do not want to delete the file while the Logger is doing work.
Things I thought of:
- Use FileChannel.lock to lock the file, something Logger does, as well. I decided against this, because of this:
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.
Which means in my case (same JVM, multiple threads) this will not cause the effect I want.
What are my options?
Am I missing something vital here?
Perhaps there is a way to do this using the already existing stuff in the Logger?