您的位置:首页 > 科技 > IT业 > 深圳网站建设制作企业_商超软件系统哪家好_网站建设及推广优化_百度推广助手app

深圳网站建设制作企业_商超软件系统哪家好_网站建设及推广优化_百度推广助手app

2024/12/27 3:04:19 来源:https://blog.csdn.net/Growing_hacker/article/details/144031528  浏览:    关键词:深圳网站建设制作企业_商超软件系统哪家好_网站建设及推广优化_百度推广助手app
深圳网站建设制作企业_商超软件系统哪家好_网站建设及推广优化_百度推广助手app

aqs采用模板方式设计模式,需要重写方法

package com.company.aqs;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 SelfLock implements Lock {// 静态内部类,自定义同步器private static class Sync extends AbstractQueuedSynchronizer {/*** 获取锁* @param arg* @return*/@Overrideprotected boolean tryAcquire(int arg) {if(compareAndSetState(0,1)){setExclusiveOwnerThread(Thread.currentThread());return true;}else{return false;}}/*** 释放锁* @param arg* @return*/@Overrideprotected boolean tryRelease(int arg) {//判断是否是持有线程释放锁if(getExclusiveOwnerThread() != Thread.currentThread()){throw new RuntimeException();}if(getState() == 0){throw new RuntimeException();}//释放锁setExclusiveOwnerThread(null);setState(0);return true;}@Overrideprotected boolean isHeldExclusively() {return  getState() == 1;}// 返回一个Condition,每个condition都包含了一个condition队列Condition newCondition() {return new ConditionObject();}}// 仅需要将操作代理到Sync上即可private final Sync sync = new Sync();public void lock() {System.out.println(Thread.currentThread().getName()+" ready get lock");sync.acquire(1);System.out.println(Thread.currentThread().getName()+" already got lock");}public boolean tryLock() {return sync.tryAcquire(1);}public void unlock() {System.out.println(Thread.currentThread().getName()+" ready release lock");sync.release(1);System.out.println(Thread.currentThread().getName()+" already released lock");}public Condition newCondition() {return sync.newCondition();}public boolean isLocked() {return sync.isHeldExclusively();}public boolean hasQueuedThreads() {return sync.hasQueuedThreads();}public void lockInterruptibly() throws InterruptedException {sync.acquireInterruptibly(1);}public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException {return sync.tryAcquireNanos(1, unit.toNanos(timeout));}
}

测试自定义显示锁

package com.company.aqs;import com.company.SleepTools;import java.util.concurrent.locks.Lock;public class TestMyLock {public void test() {final Lock lock = new SelfLock();class Worker extends Thread {public void run() {lock.lock();System.out.println(Thread.currentThread().getName());try {SleepTools.second(1);} finally {lock.unlock();}}}// 启动4个子线程for (int i = 0; i < 3; i++) {Worker w = new Worker();//w.setDaemon(true);w.start();}// 主线程每隔1秒换行for (int i = 0; i < 10; i++) {SleepTools.second(1);//System.out.println();}}public static void main(String[] args) {TestMyLock testMyLock = new TestMyLock();testMyLock.test();}
}

aqs源码分析

acquire方法中线程cas自旋尝试加入aqs队列

acquire方法中队列第一个node争抢锁

acquire方法中获取不到锁,就一直阻塞

release方法唤醒线程执行

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com