AbstractQueuedSynchronizer(五)——方法比较
2016-07-12 20:27
399 查看
1.acquire,acquireInterruptibly和tryAcquireNanos
acquire方法会一直阻塞,直到获得锁,不需要返回值。acquireInterruptibly会一直阻塞,直到获取锁或者被打断抛出中断异常,不需要返回值。
tryAcquireNanos方法会阻塞到获取锁返回true表示获得了锁,或者被打断抛出异常,或者到超时,返回false表示没有获得锁。
public final void acquire(int arg) { if (!tryAcquire(arg) && acquireQueued(addWaiter(Node.EXCLUSIVE), arg)) selfInterrupt(); }
public final void acquireInterruptibly(int arg) throws InterruptedException { if (Thread.interrupted()) throw new InterruptedException(); if (!tryAcquire(arg)) doAcquireInterruptibly(arg); }
public final boolean tryAcquireNanos(int arg, long nanosTimeout) throws InterruptedException { if (Thread.interrupted()) throw new InterruptedException(); return tryAcquire(arg) || doAcquireNanos(arg, nanosTimeout); }2.acquireQueued,doAcquireInterruptibly和doAcquireNanos
acquireQueued方法会一直阻塞,知道获得锁,如果被打断,返回false表示被打断,不会抛出中断异常。
doAcquireInterruptibly方法一直阻塞,直到获取锁,或者被打断抛出中断异常,不需要返回值。
doAcquireNanos方法会阻塞到获取锁返回true表示获得了锁,或者被打断抛出异常,或者到超时,返回false表示没有获得锁。
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); } }
private void doAcquireInterruptibly(int arg) throws InterruptedException { final Node node = addWaiter(Node.EXCLUSIVE); boolean failed = true; try { for (;;) { final Node p = node.predecessor(); if (p == head && tryAcquire(arg)) { setHead(node); p.next = null; // help GC failed = false; return; } if (shouldParkAfterFailedAcquire(p, node) && parkAndCheckInterrupt()) throw new InterruptedException(); } } finally { if (failed) cancelAcquire(node); } }
private boolean doAcquireNanos(int arg, long nanosTimeout) throws InterruptedException { long lastTime = System.nanoTime(); final Node node = addWaiter(Node.EXCLUSIVE); boolean failed = true; try { for (;;) { final Node p = node.predecessor(); if (p == head && tryAcquire(arg)) { setHead(node); p.next = null; // help GC failed = false; return true; } if (nanosTimeout <= 0) return false; if (shouldParkAfterFailedAcquire(p, node) && nanosTimeout > spinForTimeoutThreshold) LockSupport.parkNanos(this, nanosTimeout); long now = System.nanoTime(); nanosTimeout -= now - lastTime; lastTime = now; if (Thread.interrupted()) throw new InterruptedException(); } } finally { if (failed) cancelAcquire(node); } }
相关文章推荐
- AbstractQueuedSynchronizer(四)——tryAcquireNanos/doAcquireNanos方法
- 使用OC通过数组来存放图片并加按钮使之能够切换照片
- requirejs使用
- 如何用Ext中的panel动态的加载js文件,我用的了autoLoad,并将其属性scripts设成了true,但还是不行
- AbstractQueuedSynchronizer(三)——acquireInterruptibly/doAcquireInterruptibly方法
- minigui demo helloworld在arm目标板子上的运行
- hdu1503 -Advanced Fruits
- AbstractQueuedSynchronizer(二)——acquire/acquireQueued方法
- 357. Count Numbers with Unique Digits
- 【HDU】5493 Queue(2015 ACM/ICPC Asia Regional Hefei Online)
- MUI上拉加载
- MUI下拉刷新
- MUI Ajax
- MUI utils
- AbstractQueuedSynchronizer(一)——概述
- MUI事件管理
- MUI窗口管理
- IOS Dev Intro - UISwitch
- Fuel 30 分钟快速安装OpenStack
- MUI组价五:开关、底部选项卡、9宫格和分页