yuyee

ReentrantReadWriteLock读写锁

public class ReentrantReadWriteLock extends Object implements Object implements ReadWriteLock, Serializable
1.可重入,可重入的意思当前线程已获该锁,还可以再获取,但是读写锁里,WriteLock可以获取ReadLock,但是ReadLock不能获取WriteLock
2.WriteLock可以降级为ReadLock,意思是:先获取WriteLock,在获取ReadLock,释放WriteLock,这时候线程就将keep ReadLock,但是如果ReadLock想要升级为WriteLock,则不可能,因为根据读写锁的可重入特性,ReadLock排斥所有WriteLock,也就是(1)特性
3.ReadLock可以被多个线程持有并在作用时排斥任何WriteLock,而WriteLock隔离性比ReadLock高,它是完全的排斥,根据这一特性,读写锁适合高频率读,但不适合高频率写
4.不管是ReadLock,WriteLock都支持Interrupt
5.WriteLock支持Condition,但是ReadLock不支持Condition,将抛出UnsupportedOperationException
下面是个小例子:


package com.google.study;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;

public class ReentrantLockStudy {
private Map<Integer, Result> map = new ConcurrentHashMap<Integer, Result>();
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
private final WriteLock writeLock = lock.writeLock();
private final ReadLock readLock = lock.readLock();

public Result get(Integer key) {
Result value = null;
readLock.lock();// 获取读锁
try {
value = map.get(key);
if (value == null) {// 如果没有该结果
readLock.unlock();// 必须释放读锁
writeLock.lock();// 获取写锁
value = map.get(key);
if (value == null) {
value = getResult();
map.put(key, value);
}
readLock.lock();// 重新降级为读锁
writeLock.unlock();
}
} catch (Exception e) {
writeLock.unlock();
e.printStackTrace();
} finally {
readLock.unlock();
}
return value;
}

public void put(Integer key, Result value) {
writeLock.lock();
try {
map.put(key, value);
} finally {
writeLock.unlock();
}

}

private Result getResult() {
return new Result();
}

private static class Result {

}
}

posted on 2010-10-31 23:31 羔羊 阅读(971) 评论(0)  编辑  收藏 所属分类: concurrent