博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ReentrantReadWriteLock读写锁
阅读量:4229 次
发布时间:2019-05-26

本文共 2708 字,大约阅读时间需要 9 分钟。

ReentrantReadWriteLock是一把可重入读写锁,提高了读的性能。读写锁时如何实现了呢。

其实读写锁还是通过一个compareAndSet实现的,只是里面的state的含义不一样了,原先是表示线程的重入次数,现在对该变量做了拆分。高16位表示读线程的重入次数,第16位表示写线程的重入次数。

下面我们看几个变量和方法

static final int SHARED_SHIFT   = 16;        //1 0000 0000 0000 0000 用于读线程重入一次        static final int SHARED_UNIT    = (1 << SHARED_SHIFT);        //读线程最大重入次数        static final int MAX_COUNT      = (1 << SHARED_SHIFT) - 1;        //FFFF FFFF FFFF FFFF 用于计算写线程重入次数        static final int EXCLUSIVE_MASK = (1 << SHARED_SHIFT) - 1;        //获取读线程重入次数                static int sharedCount(int c)    { return c >>> SHARED_SHIFT; }        //获取写线程重入次数        static int exclusiveCount(int c) { return c & EXCLUSIVE_MASK; }

获取写锁

final boolean tryWriteLock() {            Thread current = Thread.currentThread();            int c = getState();            //如果c等于0,说明空闲,如果非0,说明已经被占用了            if (c != 0) {                //获取写线程重入次数                int w = exclusiveCount(c);                                //如果当前没有写线程,或者写线程不是当前线程,直接返回失败                if (w == 0 || current != getExclusiveOwnerThread())                    return false;                                    //如果写线程已经超过最大重入次数了,则报错                if (w == MAX_COUNT)                    throw new Error("Maximum lock count exceeded");            }                        //占用锁            if (!compareAndSetState(c, c + 1))                return false;            setExclusiveOwnerThread(current);            return true;        }

获取读锁

final boolean tryReadLock() {            Thread current = Thread.currentThread();            for (;;) {                int c = getState();                //如果当前存在写锁,并且不是当前线程,则直接获取锁失败                if (exclusiveCount(c) != 0 &&                    getExclusiveOwnerThread() != current)                    return false;                //获取读锁重入次数                int r = sharedCount(c);                if (r == MAX_COUNT)                    throw new Error("Maximum lock count exceeded");                //读锁占用                if (compareAndSetState(c, c + SHARED_UNIT)) {                    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 true;                }            }        }

转载地址:http://esjqi.baihongyu.com/

你可能感兴趣的文章
ASP.NET 2.0 Demystified
查看>>
Pattern-Oriented Software Architecture, Volume 2, Patterns for Concurrent and Networked Objects
查看>>
Pattern-Oriented Software Architecture, Volume 1: A System of Patterns
查看>>
Database Programming with Visual Basic® .NET and ADO.NET: Tips, Tutorials, and Code
查看>>
Visual Basic 2005 Express: Now Playing
查看>>
Jakarta Struts Cookbook
查看>>
AspectJ Cookbook
查看>>
IntelliJ IDEA in Action
查看>>
HTML Professional Projects
查看>>
Python Cookbook, Second Edition
查看>>
Java Extreme Programming Cookbook
查看>>
XSLT Cookbook
查看>>
Java Programming with Oracle JDBC
查看>>
Hack Proofing Your Network (Second Edition)
查看>>
XML Programming (Core Reference)
查看>>
Visual Studio .NET: The .NET Framework Black Book
查看>>
Best Kept Secrets in .NET
查看>>
SQL: The Complete Reference
查看>>
Wireless Network Hacks & Mods For Dummies
查看>>
Programming INDIGO
查看>>