FileChannel tryLock/lock method

 写了几个测试程序for tryLock/lock method,分成几种情况:

Case 1:  your java app lock file, then other software (e.g. notepad) can't modify and save the locked file

 

Case 2:  When another software (e.g. Excel) lock a file, if your java app try to lock it, will throw FileNotFoundException

 

Case 3: if you try to lock file  more than once in same thread or different thread, it will throw OverlappingFileLockException

 

Case 4: when one JVM is locking a file, if u try to call tryLock/Lock method in another JVM, tryLock method returns null immediately, lock method blocks until the previous JVM release lock.

 

 

Example for Case 1:

  1.         File f = new File(filename);
  2.         RandomAccessFile raf;
  3.         try {
  4.             raf = new RandomAccessFile(f, "rw");
  5.             FileChannel fc = raf.getChannel();
  6.             FileLock fl = fc.tryLock();
  7.             System.out.println("locked");
  8.             if (fl.isValid()) {
  9.                 // do something here, use notepad to modify and try to save the
  10.                 // lock file, Fail!
  11.                 Thread.sleep(10000);
  12.                 fl.release();
  13.                 System.out.println("release lock");
  14.             }
  15.             raf.close();
  16.         } catch (IOException e) {
  17.             e.printStackTrace();
  18.         } catch (InterruptedException e) {
  19.             e.printStackTrace();
  20.         }

 

Example for Case 2:

  1. //before run this class, you must use excel to open e:/test.csv first, because excel can lock file
  2.         File f = new File(filename);
  3.         RandomAccessFile raf = null;
  4.         
  5.         System.out.println("**************Test lock method************");
  6.         try {
  7.             raf = new RandomAccessFile(f, "rw");
  8.             FileChannel fc = raf.getChannel();
  9.             FileLock fl = fc.lock();
  10.             System.out.println("locked");
  11.             if (fl.isValid()) {
  12.                 fl.release();
  13.                 System.out.println("release lock");
  14.             }
  15.         } catch (IOException e) {
  16.             System.out.println(e);
  17.         } finally {
  18.             if (raf != null)
  19.                 try {
  20.                     raf.close();
  21.                 } catch (IOException e) {
  22.                     e.printStackTrace();
  23.                 }
  24.         }
  25.         System.out.println("**************Test tryLock method************");
  26.         try {
  27.             raf = new RandomAccessFile(f, "rw");
  28.             FileChannel fc = raf.getChannel();
  29.             FileLock fl = fc.tryLock();
  30.             System.out.println("locked");
  31.             if (fl.isValid()) {
  32.                 fl.release();
  33.                 System.out.println("release lock");
  34.             }
  35.         } catch (IOException e) {
  36.             System.out.println(e);
  37.         } finally {
  38.             if (raf != null)
  39.                 try {
  40.                     raf.close();
  41.                 } catch (IOException e) {
  42.                     e.printStackTrace();
  43.                 }
  44.         }

Example for Case 3:

  1.     public void testLockInSameThread() {
  2.         System.out
  3.                 .println("*********************Test lock in the same thread***************");
  4.         RandomAccessFile raf1 = null;
  5.         RandomAccessFile raf2 = null;
  6.         try {
  7.             raf1 = new RandomAccessFile(filename, "rw");
  8.             FileChannel fc1 = raf1.getChannel();
  9.             raf2 = new RandomAccessFile(filename, "rw");
  10.             FileChannel fc2 = raf2.getChannel();
  11.             System.out.println("Grabbing first lock");
  12.             fc1.lock();
  13.             System.out.println("Grabbing second lock");
  14.             fc2.tryLock();
  15.         } catch (Exception e) {
  16.             e.printStackTrace();
  17.         } finally {
  18.             try {
  19.                 if (raf1 != null)
  20.                     raf1.close();
  21.                 if (raf2 != null)
  22.                     raf2.close();
  23.             } catch (Exception e) {
  24.             }
  25.         }
  26.         System.out.println("Exiting");
  27.     }
  28.     public void testLockInDifferentThread() throws Exception {
  29.         System.out
  30.                 .println("*********************Test lock in the different thread***************");
  31.         RandomAccessFile raf1 = new RandomAccessFile(filename, "rw");
  32.         FileChannel fc1 = raf1.getChannel();
  33.         System.out.println("Grabbing first lock");
  34.         fc1.lock();
  35.         new Thread() {
  36.             public void run() {
  37.                 RandomAccessFile raf2;
  38.                 try {
  39.                     raf2 = new RandomAccessFile(filename, "rw");
  40.                     FileChannel fc2 = raf2.getChannel();
  41.                     System.out.println("Grabbing second lock");
  42.                     fc2.tryLock();
  43.                 } catch (Exception e) {
  44.                     e.printStackTrace();
  45.                 }
  46.             }
  47.         }.start();
  48.         System.out.println("Done");
  49.     }

 

Example for Case 4:

  1. public class TryLockReturnNullAndLockBlockDemoStep1 {
  2.     public static void main(String args[]) {
  3.         String fileName="e://locktest.txt";
  4.         try {
  5.             RandomAccessFile r = new RandomAccessFile(fileName, "rw");
  6.             FileChannel channel = r.getChannel();
  7.             FileLock fl = channel.tryLock();
  8.             System.out.println("FileLock obj is valid? " + fl.isValid());
  9.             Thread.sleep(10000);
  10.             System.out.println("release lock");
  11.             channel.close();
  12.         } catch (IOException e) {
  13.             e.printStackTrace();
  14.         } catch (InterruptedException e) {
  15.             e.printStackTrace();
  16.         }
  17.     }
  18. }
  19. /**
  20.  * !!NOTE: before run TryLockReturnNullAndLockBlockDemoStep2, you must run
  21.  * TryLockReturnNullAndLockBlockDemoStep1 first
  22.  * 
  23.  * this class is used to test when one JVM is locking a file, what happen when
  24.  * try to call tryLock/Lock method in another JVM
  25.  * 
  26.  * following codes show that:
  27.  * 
  28.  * 1. when u call tryLock method, the method return (return value is null)
  29.  * immediately
  30.  * 
  31.  * 2. when u call Lock method, the method will block until
  32.  * TryLockReturnNullAndLockBlockDemoStep1 release lock (after 30 secconds)
  33.  * 
  34.  */
  35. public class TryLockReturnNullAndLockBlockDemoStep2 {
  36.     public static void main(String args[]) {
  37.         try {
  38.             RandomAccessFile r = new RandomAccessFile("e://locktest.txt""rw");
  39.             FileChannel channel = r.getChannel();
  40.             System.out.println("calling tryLock method...");
  41.             FileLock f1 = channel.tryLock();
  42.             System.out
  43.                     .println("tryLock return value is null?  " + (f1 == null));
  44.             System.out
  45.                     .println("calling Lock method... (it will block until TryLockReturnNullAndLockBlockDemoStep1 release lock (after 30 secconds)");
  46.             FileLock f2 = channel.lock();
  47.             System.out.println("lock method is not blocked");
  48.             System.out.println("lock return value is null?  " + (f2 == null));
  49.         } catch (IOException e) {
  50.             e.printStackTrace();
  51.         }
  52.     }
  53. }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值