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

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 same 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

7
задан Yet Another Geek 15 April 2011 в 18:11
поделиться