s*********n 发帖数: 38 | 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 |
j*****l 发帖数: 2 | 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(); :
|
g**********y 发帖数: 14569 | 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(); :
|
s*********n 发帖数: 38 | 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
|
g**********y 发帖数: 14569 | 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 |
s*********n 发帖数: 38 | 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)
|