并发编程三:读写锁
并发编程之读写锁
介绍
所谓读写锁指的是,对待读操作是一种逻辑对待写实另一种判断逻辑。读写锁的场景通常是读大于甚至远大于写的请求,此时使用读写锁就非常的合适。
举例:比如数据库存有一个用户信息的表记录,如果多个线程都来读取某个用户的信息,那么我们应该是允许多个线程同时操作的,
但是当有一个用户来进行更新操作时,那么这条记录就会对其它线程不可见即对该记录上锁,直到更新线程结束才释放锁资源。这也很好理解,符合人们的正常思维。
引入读写锁
资源对象
/**
*
* @author xuyi
* @Time 2016年8月13日 上午9:24:04
* @类名 Resource
* @功能描述:存在锁重入的方法
* @春风十里不如你
* @备注:
*/
public class Resource
{
// 资源名字
private String name;
// 读取资源名字
public String readName()
{
return getName();
}
//读取资源名字操作不需要进行同步
// 更新资源名字
public void writeName(String newName)
{
setName(newName);
}
//显然更新资源名字操作需要加锁同步
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}
假如现在是这种场景,大量的读和少量的更新操作,如果我们使用普通的锁实现方式存在的问题是如果对读上普通锁的话,
那么回导致锁竞争比较强并且当有写操作线程可能会存在锁饥饿。
所以读写锁具有这些特征:
1、允许多个线程都操作
2、写线程的优先级要高于读的线程(即当有请求写线程来申请锁资源的时候,读线程的锁资源将不会再继续生成)
对读写访问资源的条件概述:
读取-没有线程正在做写操作,且没有线程在请求写操作
写入-没有线程正在做读写操作
简单的读写锁实现
/**
*
* @author xuyi
* @Time 2016年8月13日 上午10:19:35
* @类名 SimpleReadWriteLock
* @功能描述:简单的读写锁实现
* @春风十里不如你
* @备注:
*/
public class SimpleReadWriteLock
{
// 读线程数
private int readers = 0;
// 写线程数(在0/1之间变化)
private int writers = 0;
// 请求写线程数
private int requestWriters = 0;
// 获取读锁
public void readLock()
{
synchronized (this)
{
while (writers > 0 || requestWriters > 0)
{// 当存在写线程或请求写线程,那么就等待.
try
{
wait();
} catch (InterruptedException e)
{
}
}
readers++;
}
}
// 释放读锁
public void unlockReadLock()
{
synchronized (this)
{
readers--;
notifyAll();
}
}
// 获取写锁
public void writeLock()
{
synchronized (this)
{
requestWriters++;
while (readers > 0 || writers > 0)
{// 如果存在写线程或读线程就等待
try
{
wait();
} catch (InterruptedException e)
{
}
}
requestWriters--;
writers++;
}
}
// 释放写锁
public void unlockWriteLock()
{
synchronized (this)
{
writers--;
notifyAll();
}
}
}
备注:这个简单的读写锁实现也比较简单粗暴,并没有考虑重入锁的场景,我们下面来优化这个读写锁。
简单的重入读写锁实现
分析读锁写锁重入
读锁重入要满足以下一个条件
1、没有写请求和没有写操作(即最初读锁条件)
2、该线程已经持有读锁
写锁重入要满足以条件
1、该线程已经持有写锁
改造后的可重入读写锁Code
/**
*
* @author xuyi
* @Time 2016年8月13日 下午3:05:38
* @类名 SimpleReentrantLockReadWriteLock
* @功能描述:简单的重入读写锁实现
* @春风十里不如你
* @备注:
*/
public class SimpleReentrantLockReadWriteLock
{
// 读线程数
private int readersCount = 0;
// 写线程数(在0/1之间变化)
private int writersCount = 0;
// 请求写线程数
private int requestWriters = 0;
// 持有读锁线程容器
private Map<Thread, Integer> readerThreads = new HashMap<>();
// 持有写锁的线程
private Thread writeThread = null;
// 获取读锁
public void readLock()
{
synchronized (this)
{
Thread currentThread = Thread.currentThread();
while (writersCount > 0 || requestWriters > 0 || !isCurrentThreadHoldReadLock(currentThread))
// 上面的条件应该重构出一个方法出来
{// 如果存在写线程或请求写线程,或者当前线程没有持有读锁,那么就等待.
try
{
wait();
} catch (InterruptedException e)
{
}
}
readerThreads.put(currentThread, (getCurrentThreadCount(currentThread) + 1));
}
}
// 释放读锁
public void unlockReadLock()
{
synchronized (this)
{
Thread currentThread = Thread.currentThread();
int count = getCurrentThreadCount(currentThread);
if (count == 1)
{
readerThreads.remove(currentThread);
} else
{
readerThreads.put(currentThread, (count - 1));
}
notifyAll();
}
}
/**
* 当前线程是否持有读锁
*
* @param currentThread
* @return
*/
public boolean isCurrentThreadHoldReadLock(Thread currentThread)
{
return readerThreads.containsKey(currentThread);
}
/**
* 获得当前线程上锁次数
*
* @param currentThread
* @return
*/
public int getCurrentThreadCount(Thread currentThread)
{
Integer count = readerThreads.get(currentThread);
if (count == null)
return 0;
return count.intValue();
}
// 获取写锁
public void writeLock()
{
synchronized (this)
{
Thread currentThread = Thread.currentThread();
requestWriters++;
while (readersCount > 0 || writersCount > 0 || (writeThread != null && currentThread != writeThread))
// 上面这个条件应该抽成一个方法
{// 如果存在写线程或读线程就等待
try
{
wait();
} catch (InterruptedException e)
{
}
}
requestWriters--;
writersCount++;
writeThread = currentThread;
}
}
// 释放写锁
public void unlockWriteLock()
{
synchronized (this)
{
writersCount--;
if (writersCount == 0)
{
writeThread = null;
}
notifyAll();
}
}
}
总结
虽然jdk并发包已经为我们提供了读写锁的实现,但是我们能够理解其实现原理对我们今后使用和理解有很大帮助。
和普通锁一样,不管是哪种类型锁实现,最终都要释放锁资源,所以将释放锁资源的代码块放在finally块中。
参考
1、https://www.ibm.com/developerworks/cn/linux/l-cn-rwspinlock1/
2、http://ifeve.com/read-write-locks/
还没有评论,来说两句吧...