AbstractQueuedSynchronizer(三)——acquireInterruptibly/doAcquireInterruptibly方法
来源:互联网 发布:普华永道部门 知乎 编辑:程序博客网 时间:2024/04/30 09:48
/** * Acquires in exclusive mode, aborting if interrupted. * Implemented by first checking interrupt status, then invoking * at least once {@link #tryAcquire}, returning on * success. Otherwise the thread is queued, possibly repeatedly * blocking and unblocking, invoking {@link #tryAcquire} * until success or the thread is interrupted. This method can be * used to implement method {@link Lock#lockInterruptibly}. * * @param arg the acquire argument. This value is conveyed to * {@link #tryAcquire} but is otherwise uninterpreted and * can represent anything you like. * @throws InterruptedException if the current thread is interrupted */ public final void acquireInterruptibly(int arg) throws InterruptedException { if (Thread.interrupted()) throw new InterruptedException(); if (!tryAcquire(arg)) doAcquireInterruptibly(arg); }
1.原理对比
和acquire的唯一不同,就是在于它能够在外界对当前线程进行中断的时候提前结束获取状态的操作,换句话说,
就是在类似synchronized获取锁时,外界能够对当前线程进行中断,并且获取锁的这个操作能够响应中断并提前返回。一个线程处于synchronized块中或者进行同步I/O操作时,对该线程进行中断操作,这时该线程的中断标识位被设置为true,
但是线程依旧继续运行。
如果在获取一个通过网络交互实现的锁时,这个锁资源突然进行了销毁,那么使用acquireInterruptibly的获取方式
就能够让该时刻尝试获取锁的线程提前返回。而同步器的这个特性被实现Lock接口中的lockInterruptibly方法。
根据Lock的语义,在被中断时,lockInterruptibly将会抛出InterruptedException来告知使用者。
2.代码对比
{@link java.util.concurrent.locks.AbstractQueuedSynchronizer#acquireQueued}和{@link java.util.concurrent.locks.AbstractQueuedSynchronizer#doAcquireInterruptibly(int)},
在判断有interrupt的时候,前者正常执行代码,并返回一个boolean变量,但是后者直接抛出
{@link java.lang.InterruptedException}异常。
if (shouldParkAfterFailedAcquire(p, node) && parkAndCheckInterrupt()) interrupted = true;
if (shouldParkAfterFailedAcquire(p, node) && parkAndCheckInterrupt()) throw new InterruptedException();
0 0
- AbstractQueuedSynchronizer(三)——acquireInterruptibly/doAcquireInterruptibly方法
- AbstractQueuedSynchronizer(五)——方法比较
- jdk-AbstractQueuedSynchronizer(三)
- AbstractQueuedSynchronizer(二)——acquire/acquireQueued方法
- AbstractQueuedSynchronizer(四)——tryAcquireNanos/doAcquireNanos方法
- 深入理解AbstractQueuedSynchronizer(三)
- AbstractQueuedSynchronizer(一)——概述
- AbstractQueuedSynchronizer(六)——release
- AbstractQueuedSynchronizer实现源码解析(三)
- AbstractQueuedSynchronizer(七)——Share模式doAcquireShared
- AbstractQueuedSynchronizer(八)——头节点和尾节点
- AbstractQueuedSynchronizer(十)——应用之ReentrantLock
- AbstractQueuedSynchronizer(十一)——应用之CountDownLatch
- AbstractQueuedSynchronizer(十二)——应用之AbstractFuture
- AbstractQueuedSynchronizer(十三)——应用之Futures
- AbstractQueuedSynchronizer(十四)——应用之FutureTask
- AbstractQueuedSynchronizer(十五)——应用之ThreadPoolExecutor
- AbstractQueuedSynchronizer详解(一)——分析ReentrantLock源码
- 下列关于线程调度的叙述中,错误的是()。----阿里巴巴2015校招研发在线
- java中泛型的上下限
- 搭建linux驱动开发环境(配合LDD阅读)
- VMware EXSI 6.0 体验
- Scala 访问修饰符
- AbstractQueuedSynchronizer(三)——acquireInterruptibly/doAcquireInterruptibly方法
- 国内外从事CV相关的企业
- Android最简单QQ头像圆形的ImageView
- 切图快捷方式
- LZW数据压缩算法的原理分析
- 内核无HOOK文件防删除
- Git远程库操作遇到的一些问题
- Object Hook原始地址查找
- 详解js跨域问题