Chinaunix首页 | 论坛 | 博客
  • 博客访问: 529695
  • 博文数量: 135
  • 博客积分: 3568
  • 博客等级: 中校
  • 技术积分: 1942
  • 用 户 组: 普通用户
  • 注册时间: 2006-10-19 17:52
文章分类

全部博文(135)

文章存档

2012年(29)

2011年(41)

2010年(26)

2009年(12)

2008年(9)

2007年(12)

2006年(6)

分类: Java

2011-04-13 13:07:06

在Java中如何给文件加锁?如何给确认文件是否被加锁?做个测试先。

Test.java
  1. package me.test;

  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.nio.channels.FileLock;
  6. import java.nio.channels.OverlappingFileLockException;

  7. public class Test {
  8.     public static void main(String[] args) throws InterruptedException {
  9.         // 先启动读取
  10.         R r = new R();
  11.         r.start();

  12.         // 再启动写入
  13.         Thread.sleep(1000 * 2);
  14.         W w = new W();
  15.         w.start();

  16.         Thread.sleep(1000 * 2);
  17.         System.exit(0);
  18.     }

  19.     // 检查文件是否被锁(通过尝试对该文件追加排他锁)
  20.     public static boolean i***clusivelyLocked(File f) throws IOException {
  21.         FileOutputStream out = new FileOutputStream(f);
  22.         FileLock lock = null;
  23.         try {
  24.             lock = out.getChannel().tryLock();
  25.             if (lock != null) {
  26.                 lock.release();
  27.                 return false; // Has not been locked yet.
  28.             }
  29.         } catch (OverlappingFileLockException e) {
  30.             return true; // Already locked by same JVM
  31.         } finally {
  32.             out.close();
  33.         }

  34.         return true; // Already locked by ohter JVM or applications
  35.     }

  36.     // 写入一个文件并加锁()
  37.     public static class W extends Thread {
  38.         @Override
  39.         public void run() {
  40.             try {
  41.                 File f = new File("C:/123.txt");
  42.                 FileOutputStream out = new FileOutputStream(f);
  43.                 // 加锁(独占)
  44.                 FileLock lock = out.getChannel().lock();
  45.                 System.out.println("W: lock.isShared() = " + lock.isShared());
  46.                 System.out.println("W: lock.isValid() = " + lock.isValid());
  47.                 out.write("Hello World~".getBytes());
  48.                 while (true) {
  49.                     Thread.sleep(1000);
  50.                 }
  51.                 // out.close(); // 一旦close,lock.isValid() == false;
  52.             } catch (Exception e) {
  53.                 e.printStackTrace();
  54.             }
  55.         }
  56.     }

  57.     // 读取一个文件并加锁
  58.     public static class R extends Thread {
  59.         @Override
  60.         public void run() {
  61.             try {
  62.                 File f = new File("C:/123.txt");
  63.                 while (true) {
  64.                     System.out.println("R: canRead() = " + f.canRead()
  65.                             + ", isLocked() = " + i***clusivelyLocked(f));
  66.                     Thread.sleep(1000);
  67.                 }
  68.             } catch (Exception e) {
  69.                 e.printStackTrace();
  70.             }
  71.         }
  72.     }
  73. }

命令行输出结果:
  1. R: canRead() = true, isLocked() = false
  2. R: canRead() = true, isLocked() = false
  3. W: lock.isShared() = false
  4. W: lock.isValid() = true
  5. R: canRead() = true, isLocked() = true
  6. R: canRead() = true, isLocked() = true
  7. R: canRead() = true, isLocked() = true

阅读(1286) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~