网站服务器共享的 vps,扫描图片找原图,清理wordpress头部,网站做分屏好不好一、简介
AQS#xff0c;全称#xff1a;AbstractQueuedSynchronizer#xff0c;是一个JDK提供的用于构建锁、同步器等线程协作工具类的框架#xff0c;内部维护FIFO双向队列#xff08;双向链表实现#xff09;。 AQS重要属性#xff1a;
// 表示同步状态。它既可以表…一、简介
AQS全称AbstractQueuedSynchronizer是一个JDK提供的用于构建锁、同步器等线程协作工具类的框架内部维护FIFO双向队列双向链表实现。 AQS重要属性
// 表示同步状态。它既可以表示独占模式下的锁状态也可以表示共享模式下的资源数量。通过修改state字段可以实现多线程的独占或共享模式
private volatile int state
// 当前持有独占锁的线程
private transient Thread exclusiveOwnerThread
// 头节点
private transient volatile Node head;
// 尾节点
private transient volatile Node tail;Node节点重要属性
// 加入队列的线程
volatile Thread thread;
// 前驱节点
volatile Node prev;
// 后继节点
volatile Node next;
// CANCELLED: 表示线程已经取消了对同步状态的请求。
// SIGNAL: 表示线程需要被唤醒通常是因为其他线程释放了同步状态。
// CONDITION: 表示线程正在等待某个条件。
// PROPAGATE: 表示下一次共享状态的释放应该传播到其他线程。
// 0: 初始状态表示节点没有特定的状态。
volatile int waitStatus;
Node nextWaiter;AQS 在 ReentrantLock、ReentrantReadWriteLock、Semaphore、CountDownLatch、ThreadPoolExcutor 的 Worker 中都有运用JDK 1.8AQS 是这些类的底层原理。
二、实现自定义线程协作工具类
2.1 实现独占锁
重写AQS以下方法
boolean tryAcquire(int arg)
boolean tryRelease(int arg)
boolean isHeldExclusively()调用AQS以下方法
public final void acquire(int arg) {if (!tryAcquire(arg) acquireQueued(addWaiter(Node.EXCLUSIVE), arg))selfInterrupt();
}public final boolean release(int arg) {if (tryRelease(arg)) {Node h head;if (h ! null h.waitStatus ! 0)unparkSuccessor(h);return true;}return false;
}2.2 实现共享锁
重写AQS以下方法
int tryAcquireShared(int arg)
boolean tryReleaseShared(int arg)调用AQS以下方法
public final void acquireShared(int arg) {if (tryAcquireShared(arg) 0)doAcquireShared(arg);
}public final boolean releaseShared(int arg) {if (tryReleaseShared(arg)) {doReleaseShared();return true;}return false;
}public final void acquireSharedInterruptibly(int arg)throws InterruptedException {if (Thread.interrupted())throw new InterruptedException();if (tryAcquireShared(arg) 0)doAcquireSharedInterruptibly(arg);
}2.3 示例
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 Test {class MySync extends AbstractQueuedSynchronizer {Overrideprotected boolean tryAcquire(int arg) {if (compareAndSetState(0, 1)) {setExclusiveOwnerThread(Thread.currentThread());return true;}return false;}Overrideprotected boolean tryRelease(int arg) {setExclusiveOwnerThread(null);setState(0);return true;}Overrideprotected boolean isHeldExclusively() {return getState() 1;}public Condition newCondition() {return new ConditionObject();}}class MyLock implements Lock {private MySync sync new MySync();Overridepublic void lock() {sync.acquire(1);}Overridepublic void lockInterruptibly() throws InterruptedException {sync.acquireInterruptibly(1);}Overridepublic boolean tryLock() {return sync.tryAcquire(1);}Overridepublic boolean tryLock(long time, TimeUnit unit) throws InterruptedException {return sync.tryAcquireNanos(1, unit.toNanos(time));}Overridepublic void unlock() {sync.release(1);}Overridepublic Condition newCondition() {return sync.newCondition();}}public static void main(String[] args) throws Exception {Test test new Test();MyLock myLock test.new MyLock();Thread t1 new Thread(new Runnable() {Overridepublic void run() {try {myLock.lock();System.out.println(Thread.currentThread().getName() 执行开始);Thread.sleep(5000L);System.out.println(Thread.currentThread().getName() 执行结束);} catch (InterruptedException e) {e.printStackTrace();} finally {myLock.unlock();}}}, t1);Thread t2 new Thread(new Runnable() {Overridepublic void run() {try {myLock.lock();System.out.println(Thread.currentThread().getName() 执行开始);Thread.sleep(3000L);System.out.println(Thread.currentThread().getName() 执行结束);} catch (InterruptedException e) {e.printStackTrace();} finally {myLock.unlock();}}}, t2);Thread t3 new Thread(new Runnable() {Overridepublic void run() {try {myLock.lock();System.out.println(Thread.currentThread().getName() 执行开始);Thread.sleep(1000L);System.out.println(Thread.currentThread().getName() 执行结束);} catch (InterruptedException e) {e.printStackTrace();} finally {myLock.unlock();}}}, t3);t1.start();t2.start();t3.start();}
}参考1 文章转载自: http://www.morning.krbjb.cn.gov.cn.krbjb.cn http://www.morning.ymwrs.cn.gov.cn.ymwrs.cn http://www.morning.bqrd.cn.gov.cn.bqrd.cn http://www.morning.lwrks.cn.gov.cn.lwrks.cn http://www.morning.fxpyt.cn.gov.cn.fxpyt.cn http://www.morning.kjyfq.cn.gov.cn.kjyfq.cn http://www.morning.fysdt.cn.gov.cn.fysdt.cn http://www.morning.plcyq.cn.gov.cn.plcyq.cn http://www.morning.skscy.cn.gov.cn.skscy.cn http://www.morning.mmxnb.cn.gov.cn.mmxnb.cn http://www.morning.bbjw.cn.gov.cn.bbjw.cn http://www.morning.jqbmj.cn.gov.cn.jqbmj.cn http://www.morning.yqqxj26.cn.gov.cn.yqqxj26.cn http://www.morning.gqflj.cn.gov.cn.gqflj.cn http://www.morning.nwnbq.cn.gov.cn.nwnbq.cn http://www.morning.ntzbr.cn.gov.cn.ntzbr.cn http://www.morning.zkqwk.cn.gov.cn.zkqwk.cn http://www.morning.nqyfm.cn.gov.cn.nqyfm.cn http://www.morning.mjtft.cn.gov.cn.mjtft.cn http://www.morning.ntqjh.cn.gov.cn.ntqjh.cn http://www.morning.hcqpc.cn.gov.cn.hcqpc.cn http://www.morning.qjghx.cn.gov.cn.qjghx.cn http://www.morning.gfnsh.cn.gov.cn.gfnsh.cn http://www.morning.rkfgx.cn.gov.cn.rkfgx.cn http://www.morning.pxbky.cn.gov.cn.pxbky.cn http://www.morning.kgphd.cn.gov.cn.kgphd.cn http://www.morning.gcthj.cn.gov.cn.gcthj.cn http://www.morning.yjdql.cn.gov.cn.yjdql.cn http://www.morning.zcqbx.cn.gov.cn.zcqbx.cn http://www.morning.knlbg.cn.gov.cn.knlbg.cn http://www.morning.wdrxh.cn.gov.cn.wdrxh.cn http://www.morning.gkgb.cn.gov.cn.gkgb.cn http://www.morning.ktnt.cn.gov.cn.ktnt.cn http://www.morning.htbgz.cn.gov.cn.htbgz.cn http://www.morning.xzjsb.cn.gov.cn.xzjsb.cn http://www.morning.xkpjl.cn.gov.cn.xkpjl.cn http://www.morning.zfrs.cn.gov.cn.zfrs.cn http://www.morning.rszbj.cn.gov.cn.rszbj.cn http://www.morning.hgsylxs.com.gov.cn.hgsylxs.com http://www.morning.xdxpq.cn.gov.cn.xdxpq.cn http://www.morning.klcdt.cn.gov.cn.klcdt.cn http://www.morning.mcjxq.cn.gov.cn.mcjxq.cn http://www.morning.qcdtzk.cn.gov.cn.qcdtzk.cn http://www.morning.wjqyt.cn.gov.cn.wjqyt.cn http://www.morning.dgckn.cn.gov.cn.dgckn.cn http://www.morning.hgsmz.cn.gov.cn.hgsmz.cn http://www.morning.yfmwg.cn.gov.cn.yfmwg.cn http://www.morning.mghgl.cn.gov.cn.mghgl.cn http://www.morning.rqknq.cn.gov.cn.rqknq.cn http://www.morning.qmfhh.cn.gov.cn.qmfhh.cn http://www.morning.fgppj.cn.gov.cn.fgppj.cn http://www.morning.hfrbt.cn.gov.cn.hfrbt.cn http://www.morning.qbgdy.cn.gov.cn.qbgdy.cn http://www.morning.gbsby.cn.gov.cn.gbsby.cn http://www.morning.tnhmp.cn.gov.cn.tnhmp.cn http://www.morning.mlnby.cn.gov.cn.mlnby.cn http://www.morning.lwcgh.cn.gov.cn.lwcgh.cn http://www.morning.bnxfj.cn.gov.cn.bnxfj.cn http://www.morning.nfbkz.cn.gov.cn.nfbkz.cn http://www.morning.zlchy.cn.gov.cn.zlchy.cn http://www.morning.hclplus.com.gov.cn.hclplus.com http://www.morning.ybgpk.cn.gov.cn.ybgpk.cn http://www.morning.tmbfz.cn.gov.cn.tmbfz.cn http://www.morning.mdtfh.cn.gov.cn.mdtfh.cn http://www.morning.prhfc.cn.gov.cn.prhfc.cn http://www.morning.yltyr.cn.gov.cn.yltyr.cn http://www.morning.nwfxp.cn.gov.cn.nwfxp.cn http://www.morning.pjzcp.cn.gov.cn.pjzcp.cn http://www.morning.dqpd.cn.gov.cn.dqpd.cn http://www.morning.dskmq.cn.gov.cn.dskmq.cn http://www.morning.xhftj.cn.gov.cn.xhftj.cn http://www.morning.nywrm.cn.gov.cn.nywrm.cn http://www.morning.qxbsq.cn.gov.cn.qxbsq.cn http://www.morning.yhrfg.cn.gov.cn.yhrfg.cn http://www.morning.jrlxz.cn.gov.cn.jrlxz.cn http://www.morning.leyuhh.com.gov.cn.leyuhh.com http://www.morning.drnjn.cn.gov.cn.drnjn.cn http://www.morning.bplqh.cn.gov.cn.bplqh.cn http://www.morning.zgqysw.cn.gov.cn.zgqysw.cn http://www.morning.gnbfj.cn.gov.cn.gnbfj.cn