java 对文件进行加锁


  1. ReentrantReadWriteLock 文件读写锁应用
  2.  jdk 查看 java.util.concurrent.locks包下的ReentrantReadWriteLock
方法摘要
protected Thread
getOwner()
返回当前拥有写入锁的线程,如果没有这样的线程,则返回 null
protectedCollection<Thread>getQueuedReaderThreads()
返回一个 collection,它包含可能正在等待获取读取锁的线程。
protectedCollection<Thread>getQueuedThreads()
返回一个 collection,它包含可能正在等待获取读取或写入锁的线程。
protectedCollection<Thread>getQueuedWriterThreads()
返回一个 collection,它包含可能正在等待获取写入锁的线程。
intgetQueueLength()
返回等待获取读取或写入锁的线程估计数目。
intgetReadHoldCount()
查询当前线程在此锁上保持的重入读取锁数量。
intgetReadLockCount()
查询为此锁保持的读取锁数量。
protectedCollection<Thread>getWaitingThreads(Condition condition)
返回一个 collection,它包含可能正在等待与写入锁相关的给定条件的那些线程。
intgetWaitQueueLength(Condition condition)
返回正等待与写入锁相关的给定条件的线程估计数目。
intgetWriteHoldCount()
查询当前线程在此锁上保持的重入写入锁数量。
booleanhasQueuedThread(Thread thread)
查询是否给定线程正在等待获取读取或写入锁。
booleanhasQueuedThreads()
查询是否所有的线程正在等待获取读取或写入锁。
booleanhasWaiters(Condition condition)
查询是否有些线程正在等待与写入锁有关的给定条件。
booleanisFair()
如果此锁将公平性设置为 ture,则返回 true
booleanisWriteLocked()
查询是否某个线程保持了写入锁。
booleanisWriteLockedByCurrentThread()
查询当前线程是否保持了写入锁。
ReentrantReadWriteLock.ReadLockreadLock()
返回用于读取操作的锁。
StringtoString()
返回标识此锁及其锁状态的字符串。
ReentrantReadWriteLock.WriteLockwriteLock()
返回用于写入操作的锁。



  1. package com.file.test;  
  2.   
  3. import java.io.File;  
  4. import java.net.URI;  
  5. import java.util.Hashtable;  
  6. import java.util.concurrent.locks.ReentrantReadWriteLock;  
  7.   
  8. public class LockFile extends File{  
  9.   
  10.     private static Hashtable<String , ReentrantReadWriteLock> locks = new Hashtable<String , ReentrantReadWriteLock>();  
  11.     private ReentrantReadWriteLock lock = null;  
  12.       
  13.     public LockFile(File arg0, String arg1) {  
  14.         super(arg0, arg1);  
  15.         lock = initLock(this.getAbsolutePath());  
  16.     }  
  17.   
  18.     public LockFile(String arg0, String arg1) {  
  19.         super(arg0, arg1);  
  20.         lock = initLock(this.getAbsolutePath());  
  21.     }  
  22.   
  23.     public LockFile(String arg0) {  
  24.         super(arg0);  
  25.         lock = initLock(this.getAbsolutePath());  
  26.     }  
  27.   
  28.     public LockFile(URI arg0) {  
  29.         super(arg0);  
  30.         lock = initLock(this.getAbsolutePath());  
  31.     }  
  32.       
  33.     /* 
  34.      * 这里要注意使用 static synchronized,不然可能同时有多个进行初始化这个文件 
  35.      */  
  36.     private static synchronized ReentrantReadWriteLock initLock(String path) {  
  37.         ReentrantReadWriteLock lock = locks.get(path);  
  38.         if (lock == null) {  
  39.             lock = new ReentrantReadWriteLock();  
  40.             locks.put(path, lock);  
  41.         }  
  42.         return lock;  
  43.     }  
  44.       
  45.     public ReentrantReadWriteLock getLock() {  
  46.         return lock;  
  47.     }  




  1. package com.file.test;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.BufferedWriter;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.FileReader;  
  7. import java.io.FileWriter;  
  8. import java.io.IOException;  
  9. import java.util.Random;  
  10. import java.util.concurrent.locks.ReentrantReadWriteLock;  
  11.   
  12. public class LockFileTest {  
  13.       
  14.     private static final String FILE = "text.txt";  
  15.       
  16.     public static void main(String args[]) {  
  17.         LockFileTest instance = new LockFileTest();  
  18.         instance.run();  
  19.     }  
  20.       
  21.     private void run() {  
  22.         ReaderThread reader1 = new ReaderThread();  
  23.         ReaderThread reader2 = new ReaderThread();  
  24.         WriterThread writer1 = new WriterThread();  
  25.         WriterThread writer2 = new WriterThread();  
  26.           
  27.         reader1.start();  
  28.         reader2.start();  
  29.         writer1.start();  
  30.         writer2.start();  
  31.     }  
  32.       
  33.     class ReaderThread extends Thread {  
  34.           
  35.         public void run() {  
  36.               
  37.             while (true) {  
  38.                 String pre = null;  
  39.                   
  40.                 LockFile file = new LockFile(FILE);  
  41.                 if (!file.exists()) continue;  
  42.                 ReentrantReadWriteLock lock = file.getLock();  
  43.                 lock.readLock().lock();  
  44.                 try {  
  45.                     BufferedReader reader = new BufferedReader(new FileReader(file));  
  46.                     String line = reader.readLine();  
  47.                     while (line != null) {  
  48.                         if (pre == null) {  
  49.                             pre = line;  
  50.                         }  
  51.                         else {  
  52.                             if (!pre.equals(line)) {  
  53.                                 System.out.println("read wrong");  
  54.                             }  
  55.                         }  
  56.                         line = reader.readLine();  
  57.                     }  
  58.                 } catch (FileNotFoundException e) {  
  59.                     e.printStackTrace();  
  60.                 } catch (IOException e) {  
  61.                     e.printStackTrace();  
  62.                 }  
  63.                 lock.readLock().unlock();  
  64.             }  
  65.         }  
  66.     }  
  67.       
  68.     class WriterThread extends Thread {  
  69.         Random random = new Random();  
  70.         public void run() {  
  71.             String s = String.valueOf(Math.abs(random.nextInt()));  
  72.               
  73.             LockFile file = new LockFile(FILE);  
  74.             ReentrantReadWriteLock lock = file.getLock();  
  75.             lock.writeLock().lock();  
  76.             try {  
  77.                 BufferedWriter writer = new BufferedWriter(new FileWriter(file));  
  78.                 for (int i=0 ; i<1000 ; i++) {  
  79.                           
  80.                     for (int j=0 ; j<1000 ; j++) {  
  81.                         writer.write(s + System.getProperty("line.separator"));  
  82.                     }  
  83.                 }  
  84.                 writer.flush();  
  85.                 writer.close();  
  86.             } catch (IOException e) {  
  87.                 e.printStackTrace();  
  88.             }  
  89.             lock.writeLock().unlock();  
  90.         }  
  91.     }  
  92. }  
转载地址http://blog.csdn.net/sunnybuer/article/details/8263056 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值