1.1 Overview--File Lock
File locking may seem confusing at first. It seems to refer to preventing a program or user from accessing a particular file. In fact, file locks are like regular Java object locks-they are advisory (advisory) locks. They do not block any form of data access, instead they allow different parts of the system to coordinate with each other through lock sharing and acquisition.
You can lock the entire file or part of the file. If you get an exclusive lock, the other person cannot get a lock on the same file or part of the file. If you get a shared lock, the other person can get a shared lock on the same file or part of the file, but cannot get an exclusive lock. File locking is not always for the purpose of protecting data. For example, you might temporarily lock a file to ensure that a specific write operation is atomic, without interference from other programs.
Most operating systems provide file system locks, but they do not all take the same approach. Some implementations provide shared locks, while others provide only exclusive locks. In fact, some implementations make the locking portion of a file inaccessible, although most implementations do not.
In this section, you'll learn how to perform a simple file lock process in NIO, and we'll explore some ways to ensure that the files that are locked are as portable as possible.
1.2 file locking and portability
File locking can be a complex operation, especially given the fact that different operating systems implement locks in different ways. The following guidelines will help you keep your code as portable as possible:
Use exclusive locks only.
All locks are treated as advised (advisory).
Public classusefilelocks{Static Private Final intStart = 10; Static Private Final intEnd = 20; Static Public voidMain (String args[])throwsException {NewThread (NewRunnable () { Public voidrun () {//Get file ChannelRandomaccessfile RAF =NULL; Try{RAF=NewRandomaccessfile ("Usefilelocks.txt", "RW" ); } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } FileChannel FC=Raf.getchannel (); //Get LockSystem.out.println ("Trying to get lock" ); Filelock Lock=NULL; Try{lock= Fc.lock (Start, End,false ); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } System.out.println ("Got lock!" ); //PauseSystem.out.println ("pausing" ); Try{Thread.Sleep (5000);}Catch(Interruptedexception IE) {}//Release LockSystem.out.println ("Going to release lock" ); Try{lock.release (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } System.out.println ("Released Lock" ); Try{raf.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }}). Start (); NewThread (NewRunnable () { Public voidrun () {//Get file ChannelRandomaccessfile RAF =NULL; Try{RAF=NewRandomaccessfile ("Usefilelocks.txt", "RW" ); } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } FileChannel FC=Raf.getchannel (); //Get LockSystem.out.println ("Trying to get lock" ); Filelock Lock=NULL; Try{lock= Fc.lock (Start, End,false ); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } System.out.println ("Got lock!" ); //Release LockSystem.out.println ("Going to release lock" ); Try{lock.release (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } System.out.println ("Released Lock" ); Try{raf.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }}). Start (); } }
Operation Result:
Trying to get lock
Trying to get lock
Got lock!
Going to release lock
Released lock
Exception in Thread "Thread-0" java.nio.channels.OverlappingFileLockException
At Sun.nio.ch.SharedFileLockTable.checkList (Unknown Source)
At Sun.nio.ch.SharedFileLockTable.add (Unknown Source)
At Sun.nio.ch.FileChannelImpl.lock (Unknown Source)
At Zhongqiu.common.base.nio.usefilelocks$1.run (usefilelocks.java:28)
At Java.lang.Thread.run (Unknown Source)
"Java NiO base 3" file lock