BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / java / #63466同步于 2020/3/12
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖

reenTrantLock什么时候将节点抛出去

liu487639
2020/3/12镜像同步1 回复
这两天看reenTrantLock类和AQS,有些尴尬,单行代码能看明白,连起来就看不懂了。。 问题是这样子的,aqs持有的线程如果一直获取不到资源,会一直等待下去嘛,不会饿死嘛?带着这个问题,我看了下源码 ``` final boolean acquireQueued(final Node node, int arg) { boolean failed = true; try { boolean interrupted = false; //死循环,除非抛异常才跳出 for (;;) { final Node p = node.predecessor(); if (p == head && tryAcquire(arg)) { setHead(node); p.next = null; // help GC failed = false; return interrupted; } if (shouldParkAfterFailedAcquire(p, node) && parkAndCheckInterrupt()) interrupted = true; } } finally { if (failed) cancelAcquire(node); } } protected final boolean tryAcquire(int acquires) { final Thread current = Thread.currentThread(); int c = getState(); if (c == 0) { if (!hasQueuedPredecessors() && compareAndSetState(0, acquires)) { setExclusiveOwnerThread(current); return true; } } else if (current == getExclusiveOwnerThread()) { int nextc = c + acquires; if (nextc < 0) //这里抛出了个错误 throw new Error("Maximum lock count exceeded"); setState(nextc); return true; } return false; } ``` 我不明白的是,什么时候nextc会小于0呢,对应的场景又是什么
订阅后,新回复会通过你的通知中心匿名送达。
1 条回复
rancho机器人#1 · 2020/3/12
排队人多于Integer.MAX_VALUE, 溢出变成负的了 【 在 liu487639 的大作中提到: 】 : [md] : 这两天看reenTrantLock类和AQS,有些尴尬,单行代码能看明白,连起来就看不懂了。。 : 问题是这样子的,aqs持有的线程如果一直获取不到资源,会一直等待下去嘛,不会饿死嘛?带着这个问题,我看了下源码 : ...................