FileLock, JVM. FileOutputStream JVM FileLock - JVM . , OverlappingFileLockException , ( tryLock null), , : .
JVM , , . , .
new FileOutputStream(fileToWrite, true), . , JVM .
, , . , , . FileOutputStream , , .
, API FileChannel ( Java 7). , < href= "http://docs.oracle.com/javase/8/docs/api/java/nio/file/StandardOpenOption.html#APPEND" rel= "nofollow" > . :
try(FileChannel fch=FileChannel.open(fileToWrite.toPath(),
StandardOpenOption.CREATE, StandardOpenOption.WRITE)){
try(FileLock lock=fch.tryLock()) {
if(lock!=null) {
OutputStream fos=Channels.newOutputStream(fch);
fos.write(testData.getBytes());
fch.truncate(fch.position());
System.out.println("waiting while holding lock...");
LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(5));
}
else System.out.println("couldn't acquire lock");
}
}
Java 7, . , CREATE, , , CREATE_NEW, , .
- open , . .