由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - Help on file locking
相关主题
求Java8大牛们帮忙看看这个multithread的面试题。 (转载)[转载] 哪位先进用过MYSQL里面的LOCK TABLE
Talk a little more about How to lock a file请教一个多线程lock机制的问题
同一个Lock锁两次,性能比较差[合集] How to check whether a file is locked?
Apply lock on a class.[合集] Java interview question, Thanks.
How to append something on a file?interview question:
最近node.js real time web 很火synchronized method does lock the object that passed into the method as a parameter?
多线程真头疼,但也挺有趣昨天面试的一道题
memcached请问hibernate这个功能如何实现?
相关话题的讨论汇总
话题: file话题: lock话题: locking话题: channel
进入Java版参与讨论
1 (共1页)
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)

1 (共1页)
进入Java版参与讨论
相关主题
请问hibernate这个功能如何实现?How to append something on a file?
java ,wait ,notify notifyall最近node.js real time web 很火
thread 在进入 timed_waiting 或 waiting之前是不是要释放它拥有的所有lock?多线程真头疼,但也挺有趣
CC150 16.6答案是不是有问题? (转载)memcached
求Java8大牛们帮忙看看这个multithread的面试题。 (转载)[转载] 哪位先进用过MYSQL里面的LOCK TABLE
Talk a little more about How to lock a file请教一个多线程lock机制的问题
同一个Lock锁两次,性能比较差[合集] How to check whether a file is locked?
Apply lock on a class.[合集] Java interview question, Thanks.
相关话题的讨论汇总
话题: file话题: lock话题: locking话题: channel