File Locks

Some scenarios on Windows
1. using a notepad to edit a txt file, at the same time, a java program is using BufferedReader to read it, what it read is the stale content.
2. when a lock is grabbed, trying to read file using bufferedReader will cause I/O exception, "java.io.IOException: The process cannot access the file because another process has locked a portion of the file".
3. two threads, writer periodically write content to a file(500ms), reader read content every 100ms, there are some times that reader read null.
so explicitly use file lock on windows makes a difference.
4. cannot get read lock from FileInputStream, use RandomAccessFile to get read lock.

and here is some comment from tryLock:
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.

你可能感兴趣的:(java,windows,Access)