橡胶抛光轮,林业,宁静致远lacarmen
在前几篇文章中了解了reentrantlock、semaphore与countdownlatch后,j.u.c包中基于aqs实现的并发工具类还剩一个比较重要的:读写锁reentrantreadwritelock。读写锁在java面试过程中是一个经常性考的题目,他涉及到的知识点比较多,导致很多人不能透彻的理解他。举几个面试中常见的问题:
上面的问题大家都能回答出来吗?如果都能很好的回答,那么这篇文章也许对你没有太大帮助。如果不能,别着急,下面就来一一分析解答上面的几个问题。
这个问题大家应该都会回答:reentrantlock是独占锁,reentrantreadwritelock是读写锁。那么这就引申出下一个问题:reentrantreadwritelock中的读锁和写锁分别是独占还是共享的?他们之间的关系又是什么样的?要了解透彻这两个问题,分析源码是再好不过的了。
使用reentrantreadwritelock读写锁的方式,会调用readlock()和writelock()两个方法,看下他们的源码:
public reentrantreadwritelock.writelock writelock() { return writerlock; } public reentrantreadwritelock.readlock readlock() { return readerlock; }
可以看到用到了writelock和readlock两个静态内部类,他们对锁的实现如下:
public static class readlock implements lock, java.io.serializable { public void lock() { sync.acquireshared(1); //共享 } public void unlock() { sync.releaseshared(1); //共享 } } public static class writelock implements lock, java.io.serializable { public void lock() { sync.acquire(1); //独占 } public void unlock() { sync.release(1); //独占 } } abstract static class sync extends abstractqueuedsynchronizer {}
看到这里发现了reentrantreadwritelock和reentrantlock的一个相同点和不同点,相同的是使用了同一个关键实现abstractqueuedsynchronizer,不同的是reentrantreadwritelock使用了两个锁分别实现了aqs,而且writelock和reentrantlock一样,使用了独占锁。而readlock和semaphore一样,使用了共享锁。再往下的内容估计看过前面几篇文章的都很熟悉了,独占锁通过state变量的0和1两个状态来控制是否有线程占有锁,共享锁通过state变量0或者非0来控制多个线程访问。在上面的代码中,readlock和writelock使用了同一个aqs,那么在reentrantreadwritelock中又是怎么控制读锁和写锁关系的呢?
读写锁定义为:一个资源能够被多个读线程访问,或者被一个写线程访问,但是不能同时存在读写线程。
通过这句话很容易联想到通过两个不同的变量来控制读写,当获取到读锁时对读变量+1,当获取懂啊写变量时对写变量+1。但aqs中并没有为readlock和writelock添加额外的变量,还是通过一个state来实现的。那是怎么做到读写分离的呢?来看看下面这段代码:1。但aqs中并没有为readlock和writelock添加额外的变量,还是通过一个state来实现的。那是怎么做到读写分离的呢?来看看下面这段代码:
static final int shared_shift = 16; static final int shared_unit = (1 << shared_shift); static final int max_count = (1 << shared_shift) - 1; static final int exclusive_mask = (1 << shared_shift) - 1; /** returns the number of shared holds represented in count */ static int sharedcount(int c) { return c >>> shared_shift; } /** returns the number of exclusive holds represented in count */ static int exclusivecount(int c) { return c & exclusive_mask; }
这段代码在sync静态内部类中,这里有两个关键方法sharedcount和exclusivecount,通过名字可以看出sharedcount是共享锁的数量,exclusivecount是独占锁的数量。共享锁通过对c像右位移16位获得,独占锁通过和16位的1与运算获得。举个例子,当获取读锁的线程有3个,写锁的线程有1个(当然这是不可能同时有的),state就表示为0000 0000 0000 0011 0000 0000 0000 0001,高16位代表读锁,通过向右位移16位(c >>> shared_shift)得倒10进制的3,通过和0000 0000 0000 0000 1111 1111 1111 1111与运算(c & exclusive_mask),获得10进制的1。弄懂了着几个方法,就明白了为什么通过一个state实现了读写共享。
这当中还有一个问题,由于16位最大全1表示为65535,所以读锁和写锁最多可以获取65535个。
上面说过,writelock也是独占锁,那么他和reentrantlock有什么区别呢?最大的区别就在获取锁时writelock不仅需要考虑是否有其他写锁占用,同时还要考虑是否有其他读锁,而reentrantlock只需要考虑自身是否被占用就行了。来看下writelock获取锁的源代码:
public void lock() { sync.acquire(1); } public final void acquire(int arg) { if (!tryacquire(arg) && //尝试获取独占锁 acquirequeued(addwaiter(node.exclusive), arg)) //获取失败后排队 selfinterrupt(); } protected final boolean tryacquire(int acquires) { thread current = thread.currentthread(); int c = getstate(); //获取共享变量state int w = exclusivecount(c); //获取写锁数量 if (c != 0) { //有读锁或者写锁 // (note: if c != 0 and w == 0 then shared count != 0) if (w == 0 || current != getexclusiveownerthread()) //写锁为0(证明有读锁),或者持有写锁的线程不为当前线程 return false; if (w + exclusivecount(acquires) > max_count) throw new error("maximum lock count exceeded"); // reentrant acquire setstate(c + acquires); //当前线程持有写锁,为重入锁,+acquires即可 return true; } if (writershouldblock() || !compareandsetstate(c, c + acquires)) //cas操作失败,多线程情况下被抢占,获取锁失败。cas成功则获取锁成功 return false; setexclusiveownerthread(current); return true; }
这段代码是不是很熟悉?和reentrantlock中获取锁的代码很相似,差别在于其中调用了exclusivecount方法来获取是否存在写锁,然后通过c != 0和w == 0判断了是否存在读锁。acquirequeued和addwaiter就不详细解说了,需要了解的可以查看前面reentrantlock的文章。
到这里大家应该对reentrantreadwritelock和reentrantlock的区别应该做到心里有数了吧。
writelock是独占模式,我们比较了它和reentrantlock独占锁获取锁的区别,这里我们再看看readlock在获取锁上有什么不同呢?先看下面的源代码:
protected final int tryacquireshared(int unused) { thread current = thread.currentthread(); int c = getstate(); if (exclusivecount(c) != 0 && getexclusiveownerthread() != current) //写锁不等于0的情况下,验证是否是当前写锁尝试获取读锁 return -1; int r = sharedcount(c); //获取读锁数量 if (!readershouldblock() && //读锁不需要阻塞 r < max_count && //读锁小于最大读锁数量 compareandsetstate(c, c + shared_unit)) { //cas操作尝试设置获取读锁 也就是高位加1 if (r == 0) { //当前线程第一个并且第一次获取读锁, firstreader = current; firstreaderholdcount = 1; } else if (firstreader == current) { //当前线程是第一次获取读锁的线程 firstreaderholdcount++; } else { // 当前线程不是第一个获取读锁的线程,放入线程本地变量 holdcounter rh = cachedholdcounter; if (rh == null || rh.tid != getthreadid(current)) cachedholdcounter = rh = readholds.get(); else if (rh.count == 0) readholds.set(rh); rh.count++; } return 1; } return fulltryacquireshared(current); }ß
在上面的代码中尝试获取读锁的过程和获取写锁的过程也很相似,不同在于读锁只要没有写锁占用并且不超过最大获取数量都可以尝试获取读锁,而写锁不仅需要考虑读锁是否占用,也要考虑写锁是否占用。上面的代码中firstreader,firstreaderholdcount以及cachedholdcounter都是为readholds(threadlocalholdcounter)服务的,用来记录每个读锁获取线程的获取次数,方便获取当前线程持有锁的次数信息。在threadlocal基础上添加了一个int变量来统计次数,可以通过他们的实现来理解:
static final class threadlocalholdcounter extends threadlocal<holdcounter> { //threadlocal变量ß public holdcounter initialvalue() { return new holdcounter(); } } static final class holdcounter { int count = 0; //当前线程持有锁的次数 // use id, not reference, to avoid garbage retention final long tid = getthreadid(thread.currentthread()); //当前线程id }
先来分析一个简单缓存需要满足的功能,这里我们为了实现简单,不考虑缓存过期策略等复杂因素。
public static void reentrantreadwritelockcachesystem() { //这里为了实现简单,将缓存大小设置为4。 map<string, string> cachemap = new hashmap<>(4); reentrantreadwritelock readwritelock = new reentrantreadwritelock(); for (int i = 0; i < 20; i++) { //同时开启20个线程访问缓存 final string key = string.valueof(i % 4); thread thread = new thread(new runnable() { @override public void run() { try { //①读取缓存时获取读锁 readwritelock.readlock().lock(); //获取读锁后通过key获取缓存中的值 string valuestr = cachemap.get(key); //缓存值不存在 if (valuestr == null) { //③释放读锁后再尝试获取写锁 readwritelock.readlock().unlock(); try { //④获取写锁来写入不存在的key值, readwritelock.writelock().lock(); valuestr = cachemap.get(key); if (valuestr == null) { valuestr = key + " --- value"; cachemap.put(key, valuestr); //写入值 system.out.println(thread.currentthread().getname() + " --------- put " + valuestr); } // ⑥锁降级,避免被其他写线程抢占后再次更新值,保证这一次操作的原子性 readwritelock.readlock().lock(); system.out.println(thread.currentthread().getname() + " --------- get new " + valuestr); } finally { readwritelock.writelock().unlock(); //⑤释放写锁 } } else { system.out.println(thread.currentthread().getname() + " ------ get cache value"); } } finally { readwritelock.readlock().unlock(); //②释放读锁 } } }, string.valueof(i)); thread.start(); } }
首先线程会尝试去获取数据,需要获取读锁①,如果存在值,则直接读取并释放读锁②。如果不存在值,则首先释放已经获取的读锁③,然后尝试获取写锁④。获取到写锁之后,再次检查值,因为此时可能存在其他写锁已经更新值,这时只需要读取,然后释放写锁⑤。如果还是没有值,则通过其他途径获取值并更新然后获取读锁⑥,这一步锁降级操作是为了直接抢占读锁,避免释放写锁之后再次获取读锁时被其他写线程抢占,这样保证了这一次读取数据的原子性。之后再执行⑤释放写锁和②释放读锁。
执行后输出结果如下,每次执行可能输出不同:
//1 --------- put 1 --- value //1 --------- get new 1 --- value //0 --------- put 0 --- value //0 --------- get new 0 --- value //9 ------ get cache value //4 ------ get cache value //2 --------- put 2 --- value //2 --------- get new 2 --- value //11 --------- put 3 --- value //11 --------- get new 3 --- value //5 ------ get cache value //13 ------ get cache value //6 ------ get cache value //8 ------ get cache value //7 ------ get cache value //3 --------- get new 3 --- value //10 ------ get cache value //12 ------ get cache value //14 ------ get cache value //15 ------ get cache value //16 ------ get cache value //17 ------ get cache value //18 ------ get cache value //19 ------ get cache value
经过上面对读写锁原理的初步分析和使用,现在你能自己实现一个简单的读写锁了吗?这里列出了一步步实现一个简单的读写锁的过程,你可以按这个步骤自己先实现一遍。
public class myreadwritelock { private int state = 0; //1. 定义一个读写锁共享变量state //2. state高16位为读锁数量 private int getreadcount() { return state >>> 16; } //2. 低16位为写锁数量 private int getwritecount() { return state & ((1 << 16) - 1); } //3. 获取读锁时先判断是否有写锁,有则等待,没有则将读锁数量加1 public synchronized void lockread() throws interruptedexception{ while (getwritecount() > 0) { wait(); } system.out.println("lockread ---" + thread.currentthread().getname()); state = state + (1 << 16); } //4. 释放读锁数量减1,通知所有等待线程 public synchronized void unlockread() { state = state - ((1 << 16)); notifyall(); } //5. 获取写锁时需要判断读锁和写锁是否都存在,有则等待,没有则将写锁数量加1 public synchronized void lockwriters() throws interruptedexception{ while (getreadcount() > 0 || getwritecount() > 0) { wait(); } system.out.println("lockwriters ---" + thread.currentthread().getname()); state++; } //6. 释放写锁数量减1,通知所有等待线程 public synchronized void unlockwriters(){ state--; notifyall(); } }
在读写的过程中,写操作一般是优先的,不能因为读操作过多,写操作一直等待的状况发生,这样会导致数据一直得不到更新,发生写饥饿。现在大家思考一下上面我们实现的简单读写锁,是否能做到这点呢?答案很明显,在读写线程都wait的情况下,通过notifyall并不能保证写优先执行。那在这个例子中怎么改进这一点呢?
这里我通过添加一个中间变量来达到这个目的,这个中间变量在获取写锁之前先记录一个写请求,这样一旦notifyall,优先检查是否有写请求,如果有,则让写操作优先执行,具体代码如下:
public class myreadwritelock { private int state = 0; //1. 定义一个读写锁共享变量state private int writerequest = 0; //记录写请求数量 //2. state高16位为读锁数量 private int getreadcount() { return state >>> 16; } //2. 低16位为写锁数量 private int getwritecount() { return state & ((1 << 16) - 1); } //3. 获取读锁时先判断是否有写锁,有则等待,没有则将读锁数量加1 public synchronized void lockread() throws interruptedexception{ //写锁数量大于0或者写请求数量大于0的情况下都优先执行写 while (getwritecount() > 0 || writerequest > 0) { wait(); } system.out.println("lockread ---" + thread.currentthread().getname()); state = state + (1 << 16); } //4. 释放读锁数量减1,通知所有等待线程 public synchronized void unlockread() { state = state - ((1 << 16)); notifyall(); } //5. 获取写锁时需要判断读锁和写锁是否都存在,有则等待,没有则将写锁数量加1 public synchronized void lockwriters() throws interruptedexception{ writerequest++; //写请求+1 while (getreadcount() > 0 || getwritecount() > 0) { wait(); } writerequest--; //获取写锁后写请求-1 system.out.println("lockwriters ---" + thread.currentthread().getname()); state++; } //6. 释放写锁数量减1,通知所有等待线程 public synchronized void unlockwriters(){ state--; notifyall(); } }
大家可以测试下上面的代码看看是不是写请求都优先执行了呢?现在我们把这个问题放到reentrantreadwritelock中来考虑,显而易见,reentrantreadwritelock也会发生写请求饥饿的情况,因为写请求一样会排队,不管是公平锁还是非公平锁,在有读锁的情况下,都不能保证写锁一定能获取到,这样只要读锁一直占用,就会发生写饥饿的情况。那么jdk就没有提供什么好办法来解决这个问题吗?当然是有的,那就是jdk8中新增的改进读写锁---stampedlock,在下一篇文章中将会对stampedlock进行详细讲解。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
apollo与springboot集成实现动态刷新配置的教程详解
网友评论