我们有几个 Java 进程不时写入一个文本文件。这些不是同一虚拟机的线程,而是java.exe
从命令行运行的独立进程。这些进程写入同一个日志文件。我们用canWrite
...
while (!Log.canWrite()) {
System.out.println("File: " + LogPath + " is locked, waiting...");
Thread.sleep(2000);
}
...但它似乎不起作用。我们收到以下错误:
The process cannot access the file because it is being used by another process) at java.io.FileOutputStream.openAppend(Native Method).
The Question:
在这样的情况下组织信号量的最佳实践是什么?如果解决方案不太消耗资源,那就太好了,从而极大地影响整体性能。