avatar
Help on file locking# Java - 爪哇娇娃
s*n
1
Hi,
I am writing an applicaiton which may have multiple threads operating on a
single file (e.g. myfile.txt). I found an example like this:
http://javaalmanac.com/egs/java.nio/SetFileLock.html
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
try {
// Get a file channel for the file
File file = new File("myfile.txt");
FileChannel channel = new RandomAccessFile(file, "rw").getChannel();

// Use the file channel to create a lock
avatar
j*l
2
You can put your file operation codes in the try block if tryLock() returns a
non-null value and without throwing any exception.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
thread
file

【在 s*********n 的大作中提到】
: Hi,
: I am writing an applicaiton which may have multiple threads operating on a
: single file (e.g. myfile.txt). I found an example like this:
: http://javaalmanac.com/egs/java.nio/SetFileLock.html
: ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
: try {
: // Get a file channel for the file
: File file = new File("myfile.txt");
: FileChannel channel = new RandomAccessFile(file, "rw").getChannel();
:

avatar
g*y
3
I am not a NIO expert, but your code looks weird.
Channel.lock() is block-mode lock; Channel.tryLock() is unblock-mode lock. I
don't see the point to call them together.
As to my imagination, put your code between lock() and release(); it should
work.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
thread
file

【在 s*********n 的大作中提到】
: Hi,
: I am writing an applicaiton which may have multiple threads operating on a
: single file (e.g. myfile.txt). I found an example like this:
: http://javaalmanac.com/egs/java.nio/SetFileLock.html
: ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
: try {
: // Get a file channel for the file
: File file = new File("myfile.txt");
: FileChannel channel = new RandomAccessFile(file, "rw").getChannel();
:

avatar
s*n
4
The code is not written by me but an example on the internet. I have tried to
put my code between tryLock() (or lock()) and release(), but I got an
exception like this:
16:11:54,203 DEBUG testFileLock:36 - locking the file...
java.io.IOException: The process cannot access the file because another
process has locked a portion of the file
at java.io.FileInputStream.readBytes(Native Method)
at java.io.FileInputStream.read(FileInputStream.java:194)
at java.io.BufferedInputStream.fill(BufferedInputSt

【在 g**********y 的大作中提到】
: I am not a NIO expert, but your code looks weird.
: Channel.lock() is block-mode lock; Channel.tryLock() is unblock-mode lock. I
: don't see the point to call them together.
: As to my imagination, put your code between lock() and release(); it should
: work.
:
: ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
: ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
: thread
: file

avatar
g*y
5
Java seems have not so perfect FileLock mechanism --
1. lock() will physically lock the file in that OS. The second call lock()
will cause Exception thrown. Ideally, if the second call comes from the same
thread, it should be fine. However, JVM failed here.
I write a piece of code:
RandomAccessFile raf = new RandomAccessFile("test.txt");
FileChannel fc = raf.getChannel();
fc.lock();
System.out.println(raf.readLine()); // (1)
readFile(); // (2)
fc.release();
private
avatar
s*n
6
I got it. I have decided to change to another approach instead of file locking
which is not safe in multithreading env.
Thanks very much for the details!

when
on
On
to
asking,

【在 g**********y 的大作中提到】
: Java seems have not so perfect FileLock mechanism --
: 1. lock() will physically lock the file in that OS. The second call lock()
: will cause Exception thrown. Ideally, if the second call comes from the same
: thread, it should be fine. However, JVM failed here.
: I write a piece of code:
: RandomAccessFile raf = new RandomAccessFile("test.txt");
: FileChannel fc = raf.getChannel();
: fc.lock();
: System.out.println(raf.readLine()); // (1)
: readFile(); // (2)

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。