如何把图片放到网站后台做视频点播网站如何赚钱
- 作者: 五速梦信息网
- 时间: 2026年03月21日 09:52
当前位置: 首页 > news >正文
如何把图片放到网站后台,做视频点播网站如何赚钱,杨凌企业网站开发,怎么制作公众号链接专栏系列文章地址#xff1a;https://blog.csdn.net/qq_26437925/article/details/145290162 本文目标#xff1a; 理解CountDownLatch和CyclicBarrier的使用#xff0c;主要是复习Aqs 另外工作中用到CountDownLatch的地方还很多#xff0c;一般是完成某些事情才能继续某…专栏系列文章地址https://blog.csdn.net/qq_26437925/article/details/145290162 本文目标 理解CountDownLatch和CyclicBarrier的使用主要是复习Aqs 另外工作中用到CountDownLatch的地方还很多一般是完成某些事情才能继续某项操作 单词1: countdown 常见释义: 英[ˈkaʊntdaʊn] 美[ˈkaʊntdaʊn] n. 倒数读秒倒计时(如发射宇宙飞船时); 大事临近的时期; [例句]The countdown to Christmas starts here. 现在开始圣诞节倒计时。 [其他] 复数countdowns单词2: latch 常见释义 英[lætʃ] 美[lætʃ] n. 插销; 门闩; 弹簧锁; 碰锁; vt. 用插销插上; 用碰锁锁上; [例句]He lifted the latch and opened the door. 他拉起门闩开了门。 [其他] 第三人称单数latches 复数latches 现在分词latching 过去式latched 过去分词latched目录 CountDownLatch (做减法例子六国逐一灭秦统一)CountDownLatch源码countDownLatch.countDown()countDownLatch.await() CyclicBarrier做加法例子召唤神龙要收集到七龙珠 CountDownLatch (做减法例子六国逐一灭秦统一) A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes. public class Main {public static void main(String[] args) throws Exception {CountDownLatch countDownLatch new CountDownLatch(6);for (int i 1; i 6; i) {new Thread(() - {System.out.println(Thread.currentThread().getName() 国被灭);countDownLatch.countDown(); // count 减一}, i).start();}// 一直等待直到数字达到0放行。countDownLatch.await();System.out.println(Thread.currentThread().getName() \t *************秦帝国统一华夏);closeDoor();}private static void closeDoor() throws InterruptedException {CountDownLatch countDownLatch new CountDownLatch(6);for (int i 1; i 6; i) {new Thread(()-{System.out.println(Thread.currentThread().getName()\t上完自习离开教室);countDownLatch.countDown();}, threadString.valueOf(i)).start();}// 等待减少数字达到0放行。countDownLatch.await();System.out.println(Thread.currentThread().getName()\t ************班长最后关门走人);} }CountDownLatch源码 自定义Sync类继承AbstractQueuedSynchronizer利用AQS自身的状态变量state代表数量(初始化的时候指定),countdown操作让状态只减1(具体是CAS操作compareAndSetState方法让state减少1) public class CountDownLatch {/ Synchronization control For CountDownLatch.* Uses AQS state to represent count./private static final class Sync extends AbstractQueuedSynchronizer {private static final long serialVersionUID 4982264981922014374L;Sync(int count) {setState(count);}int getCount() {return getState();}protected int tryAcquireShared(int acquires) {return (getState() 0) ? 1 : -1;}protected boolean tryReleaseShared(int releases) {// Decrement count; signal when transition to zerofor (;;) {int c getState();if (c 0)return false;int nextc c-1;if (compareAndSetState(c, nextc))return nextc 0;}}}private final Sync sync;public void countDown() {sync.releaseShared(1);}unsafe.compareAndSwapInt方法 /** Atomically sets synchronization state to the given updated* value if the current state value equals the expected value.* This operation has memory semantics of a {code volatile} read* and write.** param expect the expected value* param update the new value* return {code true} if successful. False return indicates that the actual* value was not equal to the expected value./ protected final boolean compareAndSetState(int expect, int update) {// See below for intrinsics setup to support thisreturn unsafe.compareAndSwapInt(this, stateOffset, expect, update); }countDownLatch.countDown() public void countDown() {sync.releaseShared(1); }使用了Aqs的releaseShared方法 /** Releases in shared mode. Implemented by unblocking one or more* threads if {link #tryReleaseShared} returns true.** param arg the release argument. This value is conveyed to* {link #tryReleaseShared} but is otherwise uninterpreted* and can represent anything you like.* return the value returned from {link #tryReleaseShared}*/ public final boolean releaseShared(int arg) {// 如果 tryReleaseShared 返回 false则 releaseShared 返回false// 如果 tryReleaseShared 返回 true, 执行 doReleaseSharedif (tryReleaseShared(arg)) {doReleaseShared();return true;}return false; }countDownLatch 的tryReleaseShared方法不断的while循环CAS操作让state减少1如果成功就返回true;如果state已经等于0了就直接返回false protected boolean tryReleaseShared(int releases) {// Decrement count; signal when transition to zerofor (;;) {int c getState();if (c 0)return false;int nextc c-1;if (compareAndSetState(c, nextc))return nextc 0;} }aqs的doReleaseShared方法 doReleaseShared会尝试唤醒head后继的代表线程如果线程已经唤醒则仅仅设置PROPAGATE状态步骤一的“尝试唤醒head后继的代表线程”和“设置PROPAGATE状态”都是CAS操作如果CAS失败则会循环再次尝试 /**
- Release action for shared mode – signals successor and ensures
- propagation. (Note: For exclusive mode, release just amounts
- to calling unparkSuccessor of head if it needs signal.) / private void doReleaseShared() {/** Ensure that a release propagates, even if there are other in-progress acquires/releases. This proceeds in the usual* way of trying to unparkSuccessor of head if it needs* signal. But if it does not, status is set to PROPAGATE to* ensure that upon release, propagation continues.* Additionally, we must loop in case a new node is added* while we are doing this. Also, unlike other uses of* unparkSuccessor, we need to know if CAS to reset status* fails, if so rechecking./for (;;) {Node h head;if (h ! null h ! tail) {int ws h.waitStatus;if (ws Node.SIGNAL) {if (!compareAndSetWaitStatus(h, Node.SIGNAL, 0))continue; // loop to recheck casesunparkSuccessor(h);}else if (ws 0 !compareAndSetWaitStatus(h, 0, Node.PROPAGATE))continue; // loop on failed CAS}if (h head) // loop if head changedbreak;} }countDownLatch.await() /**当前线程一直等待直到countDown到0或者线程有interrupted异常抛出如果count本来就是0那么该方法直接返回 Causes the current thread to wait until the latch has counted down to
- zero, unless the thread is {linkplain Thread#interrupt interrupted}. *
- pIf the current count is zero then this method returns immediately. *
- pIf the current count is greater than zero then the current
- thread becomes disabled for thread scheduling purposes and lies
- dormant until one of two things happen:
- ul
- liThe count reaches zero due to invocations of the
- {link #countDown} method; or
- liSome other thread {linkplain Thread#interrupt interrupts}
- the current thread.
- /ul *
- pIf the current thread:
- ul
- lihas its interrupted status set on entry to this method; or
- liis {linkplain Thread#interrupt interrupted} while waiting,
- /ul
- then {link InterruptedException} is thrown and the current threads
- interrupted status is cleared. *
- throws InterruptedException if the current thread is interrupted
- while waiting */ public void await() throws InterruptedException {sync.acquireSharedInterruptibly(1); }public final void acquireSharedInterruptibly(int arg)throws InterruptedException {if (Thread.interrupted())throw new InterruptedException();if (tryAcquireShared(arg) 0)doAcquireSharedInterruptibly(arg); }CountdownLatch的tryAcquireShared方法如果state0返回1否则返回-1所以只要state不是0if (tryAcquireShared(arg) 0)条件就成立则执行doAcquireSharedInterruptibly方法 protected int tryAcquireShared(int acquires) {return (getState() 0) ? 1 : -1; }Aqs的doAcquireSharedInterruptibly方法 用当前线程构造一个共享模式Node然后CAS操作加入CLH队列尾部接着仍然是while(true)判断tryAcquireShared(arg),一定要是返回1才会return否则就是判断node的前驱节点然后判断tryAcquireShared(arg)判断 /**
- Acquires in shared interruptible mode.
- param arg the acquire argument */ private void doAcquireSharedInterruptibly(int arg)throws InterruptedException {// 添加共享模式节点final Node node addWaiter(Node.SHARED);boolean failed true;try {for (;;) {// 拿到当前节点的前驱节点final Node p node.predecessor();if (p head) {// 尝试获取资源int r tryAcquireShared(arg);// 大于等于0说明有资源获取if (r 0) {// 把当前节点设置成head节点并传播唤醒后面的节点。setHeadAndPropagate(node, r);p.next null; // help GCfailed false;return;}}// 这里和独占模式一样如果没资源申请封装节点并park等待if (shouldParkAfterFailedAcquire(p, node) parkAndCheckInterrupt())throw new InterruptedException();}} finally {if (failed)cancelAcquire(node);} }CyclicBarrier做加法例子召唤神龙要收集到七龙珠 A synchronization aid that allows a set of threads to all wait for each other to reach a common barrier point. CyclicBarriers are useful in programs involving a fixed sized party of threads that must occasionally wait for each other. The barrier is called cyclic because it can be re-used after the waiting threads are released. cyclic: adj. 循环的; 周期的;barrier: n. 屏障; 障碍物; 障碍; 阻力; 关卡; 分界线; 隔阂; public class Main {public static void main(String[] args) {CyclicBarrier cyclicBarrier new CyclicBarrier(7,()-{System.out.println(*召唤神龙*);});for (int i 1; i 7; i) {final int tempInt i;new Thread(()-{System.out.println(Thread.currentThread().getName()\t收集到第七颗龙珠tempInt龙珠);try {cyclicBarrier.await(); // 找到了就继续等其它龙珠收集} catch (InterruptedException | BrokenBarrierException e) {e.printStackTrace();}}, String.valueOf(i)).start();}}}
- 上一篇: 如何安装网站模板文件无线网二维码推广
- 下一篇: 如何把网站能搜到什么是网站的二级目录下
相关文章
-
如何安装网站模板文件无线网二维码推广
如何安装网站模板文件无线网二维码推广
- 技术栈
- 2026年03月21日
-
如何dns解析网站范文写作网站
如何dns解析网站范文写作网站
- 技术栈
- 2026年03月21日
-
如何360收录网站长沙网页制作开发公司
如何360收录网站长沙网页制作开发公司
- 技术栈
- 2026年03月21日
-
如何把网站能搜到什么是网站的二级目录下
如何把网站能搜到什么是网站的二级目录下
- 技术栈
- 2026年03月21日
-
如何把网站上传到网上网站建设学习什么
如何把网站上传到网上网站建设学习什么
- 技术栈
- 2026年03月21日
-
如何把网站上线网页制作工具知乎
如何把网站上线网页制作工具知乎
- 技术栈
- 2026年03月21日






