import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.AbstractQueuedSynchronizer; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; public class MySimpleReadWriteLock { private Syn syn = new Syn(); private Lock readLock = new ReadLock(syn); private Lock writeLock = new WriteLock(syn); public MySimpleReadWriteLock() { } private static class ReadLock implements Lock { final private Syn syn; public ReadLock(Syn syn) { this.syn = syn; } @Override public void lock() { syn.acquireShared(1); } @Override public void lockInterruptibly() throws InterruptedException { syn.acquireInterruptibly(1); } @Override public boolean tryLock() { return syn.justTryAcquire(1); } @Override public boolean tryLock(long time, TimeUnit unit) throws InterruptedException { return syn.tryAcquireNanos(1, unit.toNanos(time)); } @Override public void unlock() { syn.release(1); } @Override public Condition newCondition() { return syn.newCondition(); } } private static class WriteLock implements Lock{ private final Syn syn; public WriteLock(Syn syn) { this.syn = syn; } @Override public void lock() { syn.acquire(1); } @Override public void lockInterruptibly() throws InterruptedException { syn.acquireInterruptibly(1); } @Override public boolean tryLock() { return syn.justTryAcquire(1); } @Override public boolean tryLock(long time, TimeUnit unit) throws InterruptedException { return syn.tryAcquireNanos(1, unit.toNanos(time)); } @Override public void unlock() { syn.release(1); } @Override public Condition newCondition() { return syn.newCondition(); } } private static class Syn extends AbstractQueuedSynchronizer { private static final long serialVersionUID = 1L; private static final int SHARED_SHIFT = 16; private static final int SHARED_UNIT = 1 << SHARED_SHIFT; private static final int EXCLUSIVE_MASK = (1 << SHARED_SHIFT) - 1; private static final HoldCounterThreadLocal LOCAL = new HoldCounterThreadLocal(); private HoldCounter holdCounter = null; private static class HoldCounterThreadLocal extends ThreadLocal<HoldCounter>{ @Override protected MySimpleReadWriteLock.Syn.HoldCounter initialValue() { return new HoldCounter(); } } private static class HoldCounter { int i = 0; long tid = Thread.currentThread().getId(); public int decrement(int arg) { return i-=arg; } public int increment(int acquires) { return i+=acquires; } } @Override protected int tryAcquireShared(int acquires) { int state = super.getState(); Thread current = Thread.currentThread(); int exclusiveCount = exclusiveCount(state); if(exclusiveCount != 0 && current != getExclusiveOwnerThread()) {//current != getExclusiveOwnerThread() 保证写锁可以让读锁进 return -1; } int c = state + (acquires<<SHARED_SHIFT); if(compareAndSetState(state, c)) { HoldCounter hc = holdCounter; if(hc == null || hc.tid != current.getId() ) {//holdCounter.tid != current.getId() 利用的底层的缓存,导致每个线程可能看到的holdCounter是不一样的,很有可能是自己的,也有可能是其他线程的 holdCounter = hc = LOCAL.get(); } hc.increment(acquires); return 1; } return -1; } private int exclusiveCount(int state) { return EXCLUSIVE_MASK & state; } @Override protected boolean tryReleaseShared(int arg) { Thread current = Thread.currentThread(); HoldCounter hc = holdCounter; if(hc == null || hc.tid != current.getId()) { holdCounter = hc = LOCAL.get(); } if(hc.i-arg < 0) throw new IllegalMonitorStateException("release arg is illegal"); else hc.decrement(arg); for(;;) { //独占模式下的release不用for(;;) 因为只要判断了current和exclusiveOwnerThead就可以 但是共享模式下的,由于release可能会并发执行,需要保证原子操作,就for和cas int state = getState(); int nextc = state - (arg<<SHARED_SHIFT); if(compareAndSetState(state, nextc)) { return nextc == 0; } } } @Override protected boolean tryAcquire(int acquires) { return justTryAcquire(acquires); } private boolean justTryAcquire(int acquires) { int state = super.getState(); int w = exclusiveCount(state); Thread current = Thread.currentThread(); if(state==0) { if(super.compareAndSetState(state, state+acquires)) { setExclusiveOwnerThread(current); return true; } } else if(w==0 || current!=getExclusiveOwnerThread()) { return false; } else { if(compareAndSetState(state, state+acquires)) { return true; } } return false; } @Override protected boolean tryRelease(int arg) { Thread current = Thread.currentThread(); if(current!=getExclusiveOwnerThread()) { throw new IllegalMonitorStateException("release error"); } int p = super.getState(); int c = p-arg; if(c==0) { setState(c); setExclusiveOwnerThread(null); return true; } else { setState(c); return false; } } public Condition newCondition() { return new ConditionObject(); } @Override protected boolean isHeldExclusively() { return getExclusiveOwnerThread() == Thread.currentThread(); } } public Lock readLock() { return readLock; } public Lock writeLock() { return writeLock; } public boolean tryLock() { return syn.justTryAcquire(1); } public void lock() { syn.acquire(1); } public void unlock() { syn.release(1); } }
import java.util.concurrent.locks.Lock; public class MySimpleRWLockTest { static Lock readLock = null; static Lock writeLock = null; static { MySimpleReadWriteLock lock = new MySimpleReadWriteLock(); readLock = lock.readLock(); writeLock = lock.writeLock(); } public static void main(String[] args) { writeLockTest(); readLockTest(); } private static void writeLockTest() { TestWorker worker = new TestWorker() { @Override public void exe() { System.out.println("..........start write ...."); writeLock.lock(); try { System.out.println("enter write ...."); Thread.sleep(2000); System.out.println("exit write ...."); } catch (Exception e) { } finally { writeLock.unlock(); } } }; worker.printTakeTimeMutil(2); } private static void readLockTest() { TestWorker worker = new TestWorker() { @Override public void exe() { System.out.println("..........start read ...."); readLock.lock(); try { System.out.println("enter read ...."); Thread.sleep(1000); System.out.println("exit read ...."); } catch (Exception e) { } finally { readLock.unlock(); } } }; worker.printTakeTimeMutil(5); } }
..........start write ....
..........start read ....
enter write ....
..........start read ....
..........start write ....
..........start read ....
..........start read ....
..........start read ....
exit write ....
enter read ....
enter read ....
exit read ....
exit read ....
enter write ....
exit write ....
enter read ....
enter read ....
enter read ....
exit read ....
exit read ....
exit read ....
相关推荐
setting.xml文件,修改Maven仓库指向至阿里仓
基于java的玉安农副产品销售系统的开题报告
dev-c++ 6.3版本
基于java的项目监管系统开题报告
基于springboot多彩吉安红色旅游网站源码数据库文档.zip
毕业设计&课设_基于 AFLFast 改进能量分配策略的毕业设计项目,含 Mix Schedule策略设计及测试结果分析.zip
基于springboot办公用品管理系统源码数据库文档.zip
C++调用qml对象Demo
非常漂亮的类Web界面的Delphi设计54ed7-main.zip
VB SQL车辆管理系统是一款基于Visual Basic(VB)编程语言和SQL数据库开发的综合车辆管理工具。该系统集成了车辆信息管理、驾驶员信息管理、车辆调度、维修记录、数据存储与检索、报告生成以及安全权限管理等多个核心功能模块。 源代码部分提供了详细的开发流程和实现方法,涵盖了从数据库设计、界面设计到事件驱动编程、数据访问技术和错误处理等关键技术点。通过该系统,用户可以方便地录入、查询、修改和删除车辆及驾驶员信息,实现车辆信息的实时更新和跟踪。同时,系统还支持生成各类车辆管理相关的报告,帮助用户更好地掌握车辆运营情况。 系统部分则采用了直观易用的用户界面设计,使得用户能够轻松上手并快速完成车辆管理工作。系统还具备强大的数据处理能力和安全性,通过数据备份和系统升级优化等功能,确保数据的完整性和系统的稳定运行。 总体而言,VB SQL车辆管理系统是一款功能全面、易于操作且安全可靠的车辆管理工具,适用于企业和个人进行日常车辆运营和管理。无论是车辆信息的录入、查询还是报告生成,该系统都能够提供高效、便捷的服务,是车辆管理工作的理想选择。
AutoSAR基础学习资源
基于springboot英语学习平台源码数据库文档.zip
数据集,深度学习,密封数据集,马体态数据集
基于java的数字家庭网站开题报告
podman使用国内源镜像加速器
基于springboot+web的留守儿童网站源码数据库文档.zip
基于springboot的智能宾馆预定系统源码数据库文档.zip
GetQzonehistory-main.zip
环境说明:开发语言:Java 框架:springboot JDK版本:JDK1.8 服务器:tomcat7 数据库:mysql 5.7 数据库工具:Navicat 开发软件:eclipse/myeclipse/idea Maven包:Maven 浏览器:谷歌浏览器。 项目经过测试均可完美运行
内容概要:本文档详细介绍了QST公司生产的QMI8A01型号的6轴惯性测量单元的数据表及性能参数。主要内容包括设备特性、操作模式、接口标准(SPI、I2C与I3C),以及各种运动检测原理和技术规格。文中还提到了设备的工作温度范围宽广,内置的大容量FIFO可用于缓冲传感器数据,减少系统功耗。此外,对于器件的安装焊接指导亦有详细介绍。 适合人群:电子工程技术人员、嵌入式开发人员、硬件设计师等。 使用场景及目标:适用于需要精准测量物体空间位置变化的应用场合,如消费电子产品、智能穿戴设备、工业自动化等领域。帮助工程师快速掌握该款IMU的技术要点和应用场景。 其他说明:文档提供了详细的电气连接图表、封装尺寸图解等资料,方便用户进行电路板的设计制作。同时针对特定应用提出了一些优化建议。