开发者

In Java, Is there a way to read a file when that file is locked by other thread?

开发者 https://www.devze.com 2023-02-25 08:50 出处:网络
So i used the following to create a lock on a file so that I can edit it exclusively: File file = new File(filename);

So i used the following to create a lock on a file so that I can edit it exclusively:

 File file = new File(filename);
 channel = new RandomAccessFile(file, "rw").getChannel();
 lock = channel.tryLock();

Now I have a 2nd thread want to access the s开发者_如何学Goame file - just to read, not edit. How do i do that? Right now the 2nd thread will throw an io exception and inform me the file is locked.

Is this doable? Any suggestions? Thanks


You could try asking for a shared lock using the three argument version of tryLock.

Here is the appropriate javadoc: http://download.oracle.com/javase/1.4.2/docs/api/java/nio/channels/FileChannel.html#tryLock%28long,%20long,%20boolean%29

Basically instead of doing lock=channel.tryLock() you would do lock = channel.trylock(0, Long.MAX_VALUE, true)

As an aside, you should be careful with file locking in java. While you can guarantee the locks behave as expected within the JVM you can't necessarily be sure that they will behave as expected accross multiple processes.


Normally locking a file is on operating system basis, and when you take a write lock it is exclusive by the thread in which you take it. One thing you could do however is share the file object between the threads(but be careful with racing conditions).File locking


Maybe this helps!

public abstract FileLock tryLock(long position,
                                 long size,
                                 boolean shared)
                         throws IOException

Attempts to acquire a lock on the given region of this channel's file.

This method does not block. An invocation always returns immediately, either having acquired a lock on the requested region or having failed to do so. If it fails to acquire a lock because an overlapping lock is held by another program then it returns null. If it fails to acquire a lock for any other reason then an appropriate exception is thrown.

The region specified by the position and size parameters need not be contained within, or even overlap, the actual underlying file. Lock regions are fixed in size; if a locked region initially contains the end of the file and the file grows beyond the region then the new portion of the file will not be covered by the lock. If a file is expected to grow in size and a lock on the entire file is required then a region starting at zero, and no smaller than the expected maximum size of the file, should be locked. The zero-argument tryLock() method simply locks a region of size Long.MAX_VALUE.

Some operating systems do not support shared locks, in which case a request for a shared lock is automatically converted into a request for an exclusive lock. Whether the newly-acquired lock is shared or exclusive may be tested by invoking the resulting lock object's isShared method.

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.

Parameters: position - The position at which the locked region is to start; must be non-negative size - The size of the locked region; must be non-negative, and the sum position + size must be non-negative shared - true to request a shared lock, false to request an exclusive lock Returns: A lock object representing the newly-acquired lock, or null if the lock could not be acquired because another program holds an overlapping lock Throws: IllegalArgumentException - If the preconditions on the parameters do not hold ClosedChannelException - If this channel is closed OverlappingFileLockException - If a lock that overlaps the requested region is already held by this Java virtual machine, or if another thread is already blocked in this method and is attempting to lock an overlapping region of the same file IOException - If some other I/O error occurs See Also: lock(), lock(long,long,boolean), tryLock()

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号