-
- ReentrantReadWriteLock 文件读写锁应用
- jdk 查看 java.util.concurrent.locks包下的类ReentrantReadWriteLock
方法摘要 | |
---|---|
| getOwner() 返回当前拥有写入锁的线程,如果没有这样的线程,则返回 null 。 |
protectedCollection<Thread> | getQueuedReaderThreads() 返回一个 collection,它包含可能正在等待获取读取锁的线程。 |
protectedCollection<Thread> | getQueuedThreads() 返回一个 collection,它包含可能正在等待获取读取或写入锁的线程。 |
protectedCollection<Thread> | getQueuedWriterThreads() 返回一个 collection,它包含可能正在等待获取写入锁的线程。 |
int | getQueueLength() 返回等待获取读取或写入锁的线程估计数目。 |
int | getReadHoldCount() 查询当前线程在此锁上保持的重入读取锁数量。 |
int | getReadLockCount() 查询为此锁保持的读取锁数量。 |
protectedCollection<Thread> | getWaitingThreads(Condition condition) 返回一个 collection,它包含可能正在等待与写入锁相关的给定条件的那些线程。 |
int | getWaitQueueLength(Condition condition) 返回正等待与写入锁相关的给定条件的线程估计数目。 |
int | getWriteHoldCount() 查询当前线程在此锁上保持的重入写入锁数量。 |
boolean | hasQueuedThread(Thread thread) 查询是否给定线程正在等待获取读取或写入锁。 |
boolean | hasQueuedThreads() 查询是否所有的线程正在等待获取读取或写入锁。 |
boolean | hasWaiters(Condition condition) 查询是否有些线程正在等待与写入锁有关的给定条件。 |
boolean | isFair() 如果此锁将公平性设置为 ture,则返回 true 。 |
boolean | isWriteLocked() 查询是否某个线程保持了写入锁。 |
boolean | isWriteLockedByCurrentThread() 查询当前线程是否保持了写入锁。 |
ReentrantReadWriteLock.ReadLock | readLock() 返回用于读取操作的锁。 |
String | toString() 返回标识此锁及其锁状态的字符串。 |
ReentrantReadWriteLock.WriteLock | writeLock() 返回用于写入操作的锁。 |
-
- package com.file.test;
- import java.io.File;
- import java.net.URI;
- import java.util.Hashtable;
- import java.util.concurrent.locks.ReentrantReadWriteLock;
- public class LockFile extends File{
- private static Hashtable<String , ReentrantReadWriteLock> locks = new Hashtable<String , ReentrantReadWriteLock>();
- private ReentrantReadWriteLock lock = null;
- public LockFile(File arg0, String arg1) {
- super(arg0, arg1);
- lock = initLock(this.getAbsolutePath());
- }
- public LockFile(String arg0, String arg1) {
- super(arg0, arg1);
- lock = initLock(this.getAbsolutePath());
- }
- public LockFile(String arg0) {
- super(arg0);
- lock = initLock(this.getAbsolutePath());
- }
- public LockFile(URI arg0) {
- super(arg0);
- lock = initLock(this.getAbsolutePath());
- }
- /*
- * 这里要注意使用 static synchronized,不然可能同时有多个进行初始化这个文件
- */
- private static synchronized ReentrantReadWriteLock initLock(String path) {
- ReentrantReadWriteLock lock = locks.get(path);
- if (lock == null) {
- lock = new ReentrantReadWriteLock();
- locks.put(path, lock);
- }
- return lock;
- }
- public ReentrantReadWriteLock getLock() {
- return lock;
- }
- package com.file.test;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.Random;
- import java.util.concurrent.locks.ReentrantReadWriteLock;
- public class LockFileTest {
- private static final String FILE = "text.txt";
- public static void main(String args[]) {
- LockFileTest instance = new LockFileTest();
- instance.run();
- }
- private void run() {
- ReaderThread reader1 = new ReaderThread();
- ReaderThread reader2 = new ReaderThread();
- WriterThread writer1 = new WriterThread();
- WriterThread writer2 = new WriterThread();
- reader1.start();
- reader2.start();
- writer1.start();
- writer2.start();
- }
- class ReaderThread extends Thread {
- public void run() {
- while (true) {
- String pre = null;
- LockFile file = new LockFile(FILE);
- if (!file.exists()) continue;
- ReentrantReadWriteLock lock = file.getLock();
- lock.readLock().lock();
- try {
- BufferedReader reader = new BufferedReader(new FileReader(file));
- String line = reader.readLine();
- while (line != null) {
- if (pre == null) {
- pre = line;
- }
- else {
- if (!pre.equals(line)) {
- System.out.println("read wrong");
- }
- }
- line = reader.readLine();
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- lock.readLock().unlock();
- }
- }
- }
- class WriterThread extends Thread {
- Random random = new Random();
- public void run() {
- String s = String.valueOf(Math.abs(random.nextInt()));
- LockFile file = new LockFile(FILE);
- ReentrantReadWriteLock lock = file.getLock();
- lock.writeLock().lock();
- try {
- BufferedWriter writer = new BufferedWriter(new FileWriter(file));
- for (int i=0 ; i<1000 ; i++) {
- for (int j=0 ; j<1000 ; j++) {
- writer.write(s + System.getProperty("line.separator"));
- }
- }
- writer.flush();
- writer.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- lock.writeLock().unlock();
- }
- }
- }
转载地址http://blog.csdn.net/sunnybuer/article/details/8263056