We have several Java processes that write to one text file from time to time. These are not threads of the same virtual machine, but separate java.exe
processes that are running from command line. These processes write to the same log file. We used canWrite
...
while (!Log.canWrite()) {
System.out.println("File: " + LogPath + " is locked, waiting...");
Thread.sleep(2000);
}
... but it seems don't work. We get the following error:
The process cannot access the file because it is being used by another process) at java.io.FileOutputStream.openAppend(Native Method).
The Question:
what are the best practices for organizing semaphors in sutiations like this? It would be great if the solution wasn't too resource consuming, thus hugely affecting the total performance.