当前位置:   article > 正文

7、ReentrantReadWriteLock源码分析_reentrantreadwritelock持有锁超时释放

reentrantreadwritelock持有锁超时释放

一、ReentrantReadWriteLock介绍

ReentrantReadWriteLock是解决计算机场景的读写场景,并发场景有读线程和写线程同时对一个资源操作,且读场景远远多于写场景,多个读线程共享一个资源没有问题,但是多个写线程不能同时共享一个资源。

1、进入读写锁前提

ReentrantReadWriteLock进入读锁前提:无其他写锁
ReentrantReadWriteLock进入写锁前提:无其他写锁 且 无其他读锁

2、读写锁特性

功能可重入锁不可重入锁公平锁非公平锁独占锁可中断锁锁降级锁升级
ReentrantReadWriteLock支持不支持支持支持(默认)支持支持支持不支持

3、读写锁特点解释

公平锁:多线程按照申请锁的顺序获取锁,性能差保证顺序。
非公平锁:获取锁顺序和申请锁的顺序无关,大部分的锁都支持非公平锁,性能好。
可重入锁:当一个线程获取资源(锁),别的线程再次获取,不会出现异常(线程等待)。
不可重入锁:当一个线程获取锁后,别的线程无法再次获取锁。(实现需要自定义)
独占锁:一个线程获取资源(锁),其他线程不能获取该资源。(syn,rl都是队列独占锁)
可中断锁:一个线程等待另外一个线程释放资源,超时后不在等待。
锁降级当线程 先获取写锁->在获取读锁->在释放写锁->最后线程持有读锁,线程由写锁降级为读锁。 
锁升级:不支持先获取读锁,在获取写锁,在释放读锁,达到锁升级的目的。错误的:违背前提

内部类解释:
Sync:  Sync继承AQS实现锁信号同步
NonfairSync: 非公平锁实现
FairSync:公平锁实现
ReadLock:读锁实现
WriteLock:写锁实现

读写状态设计:
       TODO 移位运算相关

二、ReentrantReadWriteLock锁降级理解

1、锁降级的实现

锁降级是把持住当前拥有的写锁,再获取到读锁,随后释放先前拥有的写锁。

2、锁降级中读锁的获取是否必要?

答案是必要的。锁降级目的就是保证获取数据正确性主要是为了保证数据的可见性,若果当前线程不获取读锁而是直接释放写锁,假设此刻另一个线程T获取了写锁并修改了数据,那么当前线程无法感知线程T的数据更新(是说线程A使用数据时,并不知道别的线程已经更改了数据,所以使用的是线程T的修改结果。因此通过锁降级来保证数据每次修改后的可见性)。如果当前线程获取读锁,即遵循锁降级的步骤,则线程T将会被阻塞,直到当前线程使用数据并释放读锁后,线程T才能获取写锁进行数据更新。说白了就是,写锁和读锁的代码块中的操作是要保证串行执行,就相当于synchronized关键字修饰的一段代码块。

3、为啥不支持锁升级?

ReentrantReadWriteLock不支持锁升级,因为可能有其他线程同时持有读锁,而读写锁之间是互斥的,因此升级为写锁存在冲突。

三、ReentrantReadWriteLock源码解读

  1. public class ReentrantReadWriteLock
  2. implements ReadWriteLock, java.io.Serializable {
  3. private static final long serialVersionUID = -6992448646407690164L;
  4. /** 读锁 */
  5. private final ReentrantReadWriteLock.ReadLock readerLock;
  6. /** 写锁 */
  7. private final ReentrantReadWriteLock.WriteLock writerLock;
  8. /** Performs all synchronization mechanics */
  9. final Sync sync;
  10. /**
  11. * 默认初始化非公平锁
  12. */
  13. public ReentrantReadWriteLock() {
  14. this(false);
  15. }
  16. /**
  17. * true:公平锁
  18. * false:非公平锁
  19. */
  20. public ReentrantReadWriteLock(boolean fair) {
  21. sync = fair ? new FairSync() : new NonfairSync();
  22. readerLock = new ReadLock(this);
  23. writerLock = new WriteLock(this);
  24. }
  25. public ReentrantReadWriteLock.WriteLock writeLock() { return writerLock; }
  26. public ReentrantReadWriteLock.ReadLock readLock() { return readerLock; }
  27. /**
  28. * Sync构建
  29. */
  30. abstract static class Sync extends AbstractQueuedSynchronizer {
  31. private static final long serialVersionUID = 6317671515068378041L;
  32. //16位为读锁,低16位为写锁
  33. static final int SHARED_SHIFT = 16;
  34. //读锁单位,移位运算216次方,TODO移位运算流程
  35. static final int SHARED_UNIT = (1 << SHARED_SHIFT);
  36. //读锁最大数量
  37. static final int MAX_COUNT = (1 << SHARED_SHIFT) - 1;
  38. //写锁最大数量
  39. static final int EXCLUSIVE_MASK = (1 << SHARED_SHIFT) - 1;
  40. /** 读锁计数器,持有读锁线程数,位运算 */
  41. static int sharedCount(int c) { return c >>> SHARED_SHIFT; }
  42. /** 写锁获取次数,位运算 */
  43. static int exclusiveCount(int c) { return c & EXCLUSIVE_MASK; }
  44. /**
  45. * 共享线程缓存计数器
  46. */
  47. static final class HoldCounter {
  48. //读锁数量
  49. int count = 0;
  50. // 线程id
  51. final long tid = getThreadId(Thread.currentThread());
  52. }
  53. /**
  54. * 本地线程计数器池
  55. */
  56. static final class ThreadLocalHoldCounter
  57. extends ThreadLocal<HoldCounter> {
  58. public HoldCounter initialValue() {
  59. return new HoldCounter();
  60. }
  61. }
  62. /**
  63. * 读线程计数器
  64. */
  65. private transient ThreadLocalHoldCounter readHolds;
  66. /** 最近一个成功获取读锁的线程的计数。 这省却了ThreadLocal查找 缓存*/
  67. private transient HoldCounter cachedHoldCounter;
  68. /** 第一个读线程 */
  69. private transient Thread firstReader = null;
  70. /** 针对只有一个读锁的优化处理 重入计数器 */
  71. private transient int firstReaderHoldCount;
  72. Sync() {
  73. /**初始化重入计数器*/
  74. readHolds = new ThreadLocalHoldCounter();
  75. setState(getState()); // ensures visibility of readHolds
  76. }
  77. /**
  78. * 读公平策略
  79. */
  80. abstract boolean readerShouldBlock();
  81. /**
  82. * 写公平策略
  83. */
  84. abstract boolean writerShouldBlock();
  85. /*
  86. * 尝试释放独占锁
  87. */
  88. protected final boolean tryRelease(int releases) {
  89. /**如果持有锁线程不是当前线程*/
  90. if (!isHeldExclusively())
  91. throw new IllegalMonitorStateException();
  92. /**获取写锁线程状态*/
  93. int nextc = getState() - releases;
  94. //如果写锁线程状态为0,返回true
  95. boolean free = exclusiveCount(nextc) == 0;
  96. if (free)
  97. //释放线程,修改线程状态
  98. setExclusiveOwnerThread(null);
  99. setState(nextc);
  100. return free;
  101. }
  102. //尝试获取独占锁
  103. protected final boolean tryAcquire(int acquires) {
  104. /*
  105. * 获取当前线程
  106. */
  107. Thread current = Thread.currentThread();
  108. //获取线程状态
  109. int c = getState();
  110. //写线程数量
  111. int w = exclusiveCount(c);
  112. if (c != 0) {
  113. // 如果线程状态为非0(占用),【写线程数量为0 或者 当前线程不等于持有线程】,返回尝试获取独占锁失败
  114. if (w == 0 || current != getExclusiveOwnerThread())
  115. return false;
  116. //当前持有线程数+即将持有线程数>最大线容许程数 抛异常
  117. if (w + exclusiveCount(acquires) > MAX_COUNT)
  118. throw new Error("Maximum lock count exceeded");
  119. // 修改线程持有状态
  120. setState(c + acquires);
  121. return true;
  122. }
  123. //返回线程是否持有锁,公平锁判断队列情况,非公平锁直接返回false
  124. if (writerShouldBlock() ||
  125. //修改线程状态
  126. !compareAndSetState(c, c + acquires))
  127. return false;
  128. //持有线程
  129. setExclusiveOwnerThread(current);
  130. return true;
  131. }
  132. /**
  133. * 尝试释放共享锁
  134. */
  135. protected final boolean tryReleaseShared(int unused) {
  136. //获取当前线程
  137. Thread current = Thread.currentThread();
  138. //当第一个读线程等于当前线程
  139. if (firstReader == current) {
  140. // 读线程计数器长度为1
  141. if (firstReaderHoldCount == 1)
  142. //释放此读线程
  143. firstReader = null;
  144. else
  145. //如果线程计数器长度不等于1,计数器自减
  146. firstReaderHoldCount--;
  147. } else {
  148. //获取当前线程计数器
  149. HoldCounter rh = cachedHoldCounter;
  150. //当前线程计数器为null 或者 计数器tid不等于当前线程tid
  151. if (rh == null || rh.tid != getThreadId(current))
  152. //从readHolds本地线程池获取HoldCounter对象
  153. rh = readHolds.get();
  154. //获取资源持有共享锁数量
  155. int count = rh.count;
  156. //持有共享锁数量小于等于1,删除共享锁计数器
  157. if (count <= 1) {
  158. readHolds.remove();
  159. if (count <= 0)
  160. throw unmatchedUnlockException();
  161. }
  162. --rh.count;
  163. }
  164. //死循环修改线程持有锁状态
  165. for (;;) {
  166. //线程状态
  167. int c = getState();
  168. //TODO 这个赋值难以理解
  169. int nextc = c - SHARED_UNIT;
  170. if (compareAndSetState(c, nextc))
  171. return nextc == 0;
  172. }
  173. }
  174. private IllegalMonitorStateException unmatchedUnlockException() {
  175. return new IllegalMonitorStateException(
  176. "attempt to unlock read lock, not locked by current thread");
  177. }
  178. /**
  179. * 尝试获取共享锁
  180. */
  181. protected final int tryAcquireShared(int unused) {
  182. /*
  183. * 获取当前线程
  184. */
  185. Thread current = Thread.currentThread();
  186. //获取线程状态
  187. int c = getState();
  188. if (exclusiveCount(c) != 0 &&
  189. //判断持有锁的线程是否为当前线程
  190. getExclusiveOwnerThread() != current)
  191. return -1;
  192. //获取共享锁的数量
  193. int r = sharedCount(c);
  194. //独占锁:判断队列第一个节点是不是独占模式 共享锁:判断是否是独占锁,持有锁线程是否为当前线程
  195. if (!readerShouldBlock() &&
  196. //共享锁数量要小于锁最大数量
  197. r < MAX_COUNT &&
  198. //修改锁状态
  199. compareAndSetState(c, c + SHARED_UNIT)) {
  200. //共享锁数目为0
  201. if (r == 0) {
  202. //设置当前线程为firstReader,优化部分
  203. firstReader = current;
  204. //共享线程缓存计数器加1
  205. firstReaderHoldCount = 1;
  206. } else if (firstReader == current) {
  207. firstReaderHoldCount++;
  208. } else {
  209. //获取当前线程计数器
  210. HoldCounter rh = cachedHoldCounter;
  211. //如果计数器为null 或 当前线程id不等于线程计数器id
  212. if (rh == null || rh.tid != getThreadId(current))
  213. //赋值线程计数器
  214. cachedHoldCounter = rh = readHolds.get();
  215. //如果线程计数器线程数等于0
  216. else if (rh.count == 0)
  217. //赋值线程计数器
  218. readHolds.set(rh);
  219. rh.count++;
  220. }
  221. return 1;
  222. }
  223. //处理失败后,自旋实现线程重入队列
  224. return fullTryAcquireShared(current);
  225. }
  226. /**
  227. * 自旋方式获取共享锁
  228. */
  229. final int fullTryAcquireShared(Thread current) {
  230. HoldCounter rh = null;
  231. for (;;) {
  232. //获取线程占用状态
  233. int c = getState();
  234. //如果其他线程获取写锁
  235. if (exclusiveCount(c) != 0) {
  236. //持锁线程不等于当前线程
  237. if (getExclusiveOwnerThread() != current)
  238. return -1;
  239. // else we hold the exclusive lock; blocking here
  240. //独占锁:判断队列第一个节点是不是独占模式 共享锁:判断是否是独占锁,持有锁线程是否为当前线程
  241. } else if (readerShouldBlock()) {
  242. // 如果firstReader等于当前线程
  243. if (firstReader == current) {
  244. // assert firstReaderHoldCount > 0;
  245. } else {
  246. //如果线程计数器池等于null
  247. if (rh == null) {
  248. rh = cachedHoldCounter;
  249. if (rh == null || rh.tid != getThreadId(current)) {
  250. rh = readHolds.get();
  251. //如果线程计数器为0,删除此计数器
  252. if (rh.count == 0)
  253. readHolds.remove();
  254. }
  255. }
  256. if (rh.count == 0)
  257. return -1;
  258. }
  259. }
  260. //如果共享线程数等于最大线程数
  261. if (sharedCount(c) == MAX_COUNT)
  262. throw new Error("Maximum lock count exceeded");
  263. //修改共享线程状态
  264. if (compareAndSetState(c, c + SHARED_UNIT)) {
  265. //共享线程数等于0
  266. if (sharedCount(c) == 0) {
  267. firstReader = current;
  268. firstReaderHoldCount = 1;
  269. } else if (firstReader == current) {
  270. firstReaderHoldCount++;
  271. } else {
  272. if (rh == null)
  273. rh = cachedHoldCounter;
  274. if (rh == null || rh.tid != getThreadId(current))
  275. rh = readHolds.get();
  276. else if (rh.count == 0)
  277. readHolds.set(rh);
  278. rh.count++;
  279. cachedHoldCounter = rh; // cache for release
  280. }
  281. return 1;
  282. }
  283. }
  284. }
  285. /**
  286. * 尝试获取写锁(可中断方式,不会傻傻等待)
  287. */
  288. final boolean tryWriteLock() {
  289. //获取当前线程
  290. Thread current = Thread.currentThread();
  291. //获取线程状态 1:持有锁 0:未持有锁
  292. int c = getState();
  293. if (c != 0) {
  294. //获取写锁数量
  295. int w = exclusiveCount(c);
  296. //如果写锁数量为0 或者 当前线程不等于持锁线程 返回false
  297. if (w == 0 || current != getExclusiveOwnerThread())
  298. return false;
  299. if (w == MAX_COUNT)
  300. throw new Error("Maximum lock count exceeded");
  301. }
  302. //CAS更新线程状态
  303. if (!compareAndSetState(c, c + 1))
  304. return false;
  305. //设置当前线程为持锁线程
  306. setExclusiveOwnerThread(current);
  307. return true;
  308. }
  309. /**
  310. * 尝试获取读锁
  311. */
  312. final boolean tryReadLock()
  313. //获取当前线程
  314. Thread current = Thread.currentThread();
  315. for (;;) {
  316. //获取线程状态
  317. int c = getState();
  318. //如果写锁数量不等于0 且 持锁线程不等于当前线程 返回false【说明写锁独占】
  319. if (exclusiveCount(c) != 0 &&
  320. getExclusiveOwnerThread() != current)
  321. return false;
  322. //获取读锁数量
  323. int r = sharedCount(c);
  324. //读锁数量等于最大容许数
  325. if (r == MAX_COUNT)
  326. throw new Error("Maximum lock count exceeded");
  327. //修改线程状态
  328. if (compareAndSetState(c, c + SHARED_UNIT)) {
  329. //共享线程数等于0
  330. if (r == 0) {
  331. //初始化firstReader为当前线程,firstReaderHoldCount=1
  332. firstReader = current;
  333. firstReaderHoldCount = 1;
  334. //如果firstReader当前线程
  335. } else if (firstReader == current) {
  336. //线程池数量自增
  337. firstReaderHoldCount++;
  338. } else {
  339. // 获取线程计数器
  340. HoldCounter rh = cachedHoldCounter;
  341. //如果线程计数器为null 或者 当前线程id不得能与线程计数器id
  342. if (rh == null || rh.tid != getThreadId(current))
  343. //初始化计数器
  344. cachedHoldCounter = rh = readHolds.get();
  345. else if (rh.count == 0)
  346. readHolds.set(rh);
  347. rh.count++;
  348. }
  349. return true;
  350. }
  351. }
  352. }
  353. /**
  354. * 判断当前线程为持锁线程
  355. */
  356. protected final boolean isHeldExclusively() {
  357. return getExclusiveOwnerThread() == Thread.currentThread();
  358. }
  359. // 初始化等待队列 TODO 后续研究Condition在补充
  360. final ConditionObject newCondition() {
  361. return new ConditionObject();
  362. }
  363. /**
  364. * 如果无线程持有写锁返回null,则返回当前持锁线程
  365. */
  366. final Thread getOwner() {
  367. // Must read state before owner to ensure memory consistency
  368. return ((exclusiveCount(getState()) == 0) ?
  369. null :
  370. getExclusiveOwnerThread());
  371. }
  372. //返回读锁数
  373. final int getReadLockCount() {
  374. return sharedCount(getState());
  375. }
  376. //是否是写锁
  377. final boolean isWriteLocked() {
  378. return exclusiveCount(getState()) != 0;
  379. }
  380. //写锁数量
  381. final int getWriteHoldCount() {
  382. return isHeldExclusively() ? exclusiveCount(getState()) : 0;
  383. }
  384. //返回读锁数量
  385. final int getReadHoldCount() {
  386. if (getReadLockCount() == 0)
  387. return 0;
  388. //如当前线程等于firstReader,返回firstReader读线程数
  389. Thread current = Thread.currentThread();
  390. if (firstReader == current)
  391. return firstReaderHoldCount;
  392. //如果当前线程id等于线程计数器id,返回线程计数器读线程数
  393. HoldCounter rh = cachedHoldCounter;
  394. if (rh != null && rh.tid == getThreadId(current))
  395. return rh.count;
  396. //最后返回缓存中线程计数器读线程数
  397. int count = readHolds.get().count;
  398. if (count == 0) readHolds.remove();
  399. return count;
  400. }
  401. /**
  402. * TODO 做什么?
  403. */
  404. private void readObject(java.io.ObjectInputStream s)
  405. throws java.io.IOException, ClassNotFoundException {
  406. s.defaultReadObject();
  407. readHolds = new ThreadLocalHoldCounter();
  408. setState(0); // reset to unlocked state
  409. }
  410. final int getCount() { return getState(); }
  411. }
  412. /**
  413. * 非公平锁实现
  414. */
  415. static final class NonfairSync extends Sync {
  416. private static final long serialVersionUID = -8159625535654395037L;
  417. //非公平方式获取写锁,不加入队列
  418. final boolean writerShouldBlock() {
  419. return false; // writers can always barge
  420. }
  421. //非公平方式获取读锁 查看AQS源码
  422. final boolean readerShouldBlock() {
  423. return apparentlyFirstQueuedIsExclusive();
  424. }
  425. }
  426. /**
  427. * 公平锁实现
  428. */
  429. static final class FairSync extends Sync {
  430. private static final long serialVersionUID = -2274990926593161451L;
  431. //节点是否加入队列,阻塞方式获取写锁
  432. final boolean writerShouldBlock() {
  433. return hasQueuedPredecessors();
  434. }
  435. //节点是否加入队列,阻塞方式获取读锁
  436. final boolean readerShouldBlock() {
  437. return hasQueuedPredecessors();
  438. }
  439. }
  440. /**
  441. * 读锁定义
  442. */
  443. public static class ReadLock implements Lock, java.io.Serializable {
  444. private static final long serialVersionUID = -5992448646407690164L;
  445. private final Sync sync;
  446. /**
  447. * 初始化sync对象
  448. */
  449. protected ReadLock(ReentrantReadWriteLock lock) {
  450. sync = lock.sync;
  451. }
  452. /**
  453. * 获取共享锁 参考AQS源码
  454. */
  455. public void lock() {
  456. sync.acquireShared(1);
  457. }
  458. /**
  459. * 可中断方式获取共享锁
  460. */
  461. public void lockInterruptibly() throws InterruptedException {
  462. sync.acquireSharedInterruptibly(1);
  463. }
  464. /**
  465. * 尝试获取读锁
  466. */
  467. public boolean tryLock() {
  468. return sync.tryReadLock();
  469. }
  470. /**
  471. * 尝试获取锁 true:获取 false:未获取
  472. */
  473. public boolean tryLock(long timeout, TimeUnit unit)
  474. throws InterruptedException {
  475. return sync.tryAcquireSharedNanos(1, unit.toNanos(timeout));
  476. }
  477. /**
  478. * 锁释放
  479. */
  480. public void unlock() {
  481. sync.releaseShared(1);
  482. }
  483. /**
  484. * 不支持conditions抛出异常
  485. */
  486. public Condition newCondition() {
  487. throw new UnsupportedOperationException();
  488. }
  489. /**
  490. * 锁信息打印
  491. */
  492. public String toString() {
  493. int r = sync.getReadLockCount();
  494. return super.toString() +
  495. "[Read locks = " + r + "]";
  496. }
  497. }
  498. /**
  499. * 写锁定义
  500. */
  501. public static class WriteLock implements Lock, java.io.Serializable {
  502. private static final long serialVersionUID = -4992448646407690164L;
  503. private final Sync sync;
  504. /**
  505. * 初始化写锁
  506. */
  507. protected WriteLock(ReentrantReadWriteLock lock) {
  508. sync = lock.sync;
  509. }
  510. /**
  511. * 尝试获取锁
  512. */
  513. public void lock() {
  514. sync.acquire(1);
  515. }
  516. /**
  517. * 可中断方式获取锁 AQS源码分析
  518. */
  519. public void lockInterruptibly() throws InterruptedException {
  520. sync.acquireInterruptibly(1);
  521. }
  522. /**
  523. * 尝试获取锁
  524. */
  525. public boolean tryLock( ) {
  526. return sync.tryWriteLock();
  527. }
  528. /**
  529. * 尝试超时等待方式获取锁
  530. */
  531. public boolean tryLock(long timeout, TimeUnit unit)
  532. throws InterruptedException {
  533. return sync.tryAcquireNanos(1, unit.toNanos(timeout));
  534. }
  535. /**
  536. * 释放锁
  537. */
  538. public void unlock() {
  539. sync.release(1);
  540. }
  541. /**
  542. * 创建Condition队列
  543. */
  544. public Condition newCondition() {
  545. return sync.newCondition();
  546. }
  547. /**
  548. * 打印输出写锁线程信息
  549. */
  550. public String toString() {
  551. Thread o = sync.getOwner();
  552. return super.toString() + ((o == null) ?
  553. "[Unlocked]" :
  554. "[Locked by thread " + o.getName() + "]");
  555. }
  556. /**
  557. * 判断持有锁线程是否是当前线程
  558. */
  559. public boolean isHeldByCurrentThread() {
  560. return sync.isHeldExclusively();
  561. }
  562. /**
  563. * 获取写锁线程数
  564. */
  565. public int getHoldCount() {
  566. return sync.getWriteHoldCount();
  567. }
  568. }
  569. // Instrumentation and status
  570. /**
  571. * 判断锁释放是公平锁
  572. */
  573. public final boolean isFair() {
  574. return sync instanceof FairSync;
  575. }
  576. /**
  577. * 返回持有锁的线程
  578. */
  579. protected Thread getOwner() {
  580. return sync.getOwner();
  581. }
  582. /**
  583. * 获取共享锁数量
  584. */
  585. public int getReadLockCount() {
  586. return sync.getReadLockCount();
  587. }
  588. /**
  589. * 判断是否是独占锁
  590. */
  591. public boolean isWriteLocked() {
  592. return sync.isWriteLocked();
  593. }
  594. /**
  595. * 当前线程是否是持锁线程
  596. */
  597. public boolean isWriteLockedByCurrentThread() {
  598. return sync.isHeldExclusively();
  599. }
  600. /**
  601. * 返回独占锁数量
  602. */
  603. public int getWriteHoldCount() {
  604. return sync.getWriteHoldCount();
  605. }
  606. /**
  607. * 返回读锁数量
  608. */
  609. public int getReadHoldCount() {
  610. return sync.getReadHoldCount();
  611. }
  612. /**
  613. * 返回独占锁线程集合 AQS源码
  614. */
  615. protected Collection<Thread> getQueuedWriterThreads() {
  616. return sync.getExclusiveQueuedThreads();
  617. }
  618. /**
  619. * 返回读锁线程集合 AQS源码
  620. */
  621. protected Collection<Thread> getQueuedReaderThreads() {
  622. return sync.getSharedQueuedThreads();
  623. }
  624. /**
  625. * 是否有线程排队 AQS源码
  626. */
  627. public final boolean hasQueuedThreads() {
  628. return sync.hasQueuedThreads();
  629. }
  630. /**
  631. * 线程是否在队列排队 AQS源码
  632. */
  633. public final boolean hasQueuedThread(Thread thread) {
  634. return sync.isQueued(thread);
  635. }
  636. /**
  637. * 队列长度 AQS源码
  638. */
  639. public final int getQueueLength() {
  640. return sync.getQueueLength();
  641. }
  642. /**
  643. * 队列线程集合 AQS源码
  644. */
  645. protected Collection<Thread> getQueuedThreads() {
  646. return sync.getQueuedThreads();
  647. }
  648. /**
  649. * Condition队列是否有线程排队 AQS源码
  650. */
  651. public boolean hasWaiters(Condition condition) {
  652. if (condition == null)
  653. throw new NullPointerException();
  654. if (!(condition instanceof AbstractQueuedSynchronizer.ConditionObject))
  655. throw new IllegalArgumentException("not owner");
  656. return sync.hasWaiters((AbstractQueuedSynchronizer.ConditionObject)condition);
  657. }
  658. /**
  659. * Condition队列长度 AQS源码
  660. */
  661. public int getWaitQueueLength(Condition condition) {
  662. if (condition == null)
  663. throw new NullPointerException();
  664. if (!(condition instanceof AbstractQueuedSynchronizer.ConditionObject))
  665. throw new IllegalArgumentException("not owner");
  666. return sync.getWaitQueueLength((AbstractQueuedSynchronizer.ConditionObject)condition);
  667. }
  668. /**
  669. * 获取阻塞在Condition队列的线程集合 AQS源码
  670. */
  671. protected Collection<Thread> getWaitingThreads(Condition condition) {
  672. if (condition == null)
  673. throw new NullPointerException();
  674. if (!(condition instanceof AbstractQueuedSynchronizer.ConditionObject))
  675. throw new IllegalArgumentException("not owner");
  676. return sync.getWaitingThreads((AbstractQueuedSynchronizer.ConditionObject)condition);
  677. }
  678. /**
  679. * 打印线程信息
  680. */
  681. public String toString() {
  682. int c = sync.getCount();
  683. int w = Sync.exclusiveCount(c);
  684. int r = Sync.sharedCount(c);
  685. return super.toString() +
  686. "[Write locks = " + w + ", Read locks = " + r + "]";
  687. }
  688. /**
  689. * 获取线程ID
  690. */
  691. static final long getThreadId(Thread thread) {
  692. return UNSAFE.getLongVolatile(thread, TID_OFFSET);
  693. }
  694. // 初始化Unsafe对象
  695. private static final sun.misc.Unsafe UNSAFE;
  696. private static final long TID_OFFSET;
  697. static {
  698. try {
  699. UNSAFE = sun.misc.Unsafe.getUnsafe();
  700. Class<?> tk = Thread.class;
  701. TID_OFFSET = UNSAFE.objectFieldOffset
  702. (tk.getDeclaredField("tid"));
  703. } catch (Exception e) {
  704. throw new Error(e);
  705. }
  706. }
  707. }

参考文档:

  1. ReentrantReadWriteLock读写锁详解
  2. 可重入读写锁ReentrantReadWriteLock的使用详解

   

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/966856
推荐阅读
相关标签
  

闽ICP备14008679号