| await() 进入等待的状态 |
| countDown() 计数器减一 |
| import java.util.concurrent.CountDownLatch; |
| |
| public class CountDownLatchDemo { |
| |
| public static void main(String[] args) { |
| CountDownLatch countDownLatch = new CountDownLatch(8); |
| new Thread(()->{ |
| try { |
| countDownLatch.await(); |
| } catch (InterruptedException e) { |
| e.printStackTrace(); |
| } |
| System.out.println("800米比赛结束,准备清空跑道并继续跨栏比赛"); |
| }).start(); |
| |
| for (int i = 0; i < 8; i++) { |
| int finalI = i; |
| new Thread(()->{ |
| try { |
| Thread.sleep(finalI * 1000L); |
| System.out.println(Thread.currentThread().getName()+"到达终点"); |
| } catch (InterruptedException e) { |
| e.printStackTrace(); |
| } finally { |
| countDownLatch.countDown(); |
| } |
| }).start(); |
| } |
| } |
| |
| } |
| |
| # 控制台执行结果: |
| Thread-1到达终点 |
| Thread-2到达终点 |
| Thread-3到达终点 |
| Thread-4到达终点 |
| Thread-5到达终点 |
| Thread-6到达终点 |
| Thread-7到达终点 |
| Thread-8到达终点 |
| 800米比赛结束,准备清空跑道并继续跨栏比赛 |
| # ctrl 查看await方法 |
| public void await() throws InterruptedException { |
| sync.acquireSharedInterruptibly(1); |
| } |
| |
| # sync继承了aqs |
| 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) { |
| |
| for (;;) { |
| int c = getState(); |
| if (c == 0) |
| return false; |
| int nextc = c - 1; |
| if (compareAndSetState(c, nextc)) |
| return nextc == 0; |
| } |
| } |
| } |
| |
| # 当我们new CountDownLatch对象时,实际上是new sync |
| public CountDownLatch(int count) { |
| if (count < 0) throw new IllegalArgumentException("count < 0"); |
| this.sync = new Sync(count); |
| } |
| |
| # 查看sync |
| Sync(int count) { |
| setState(count); |
| } |
| |
| # 返回await方法 |
| public void await() throws InterruptedException { |
| sync.acquireSharedInterruptibly(1); |
| } |
| |
| # 继续查看acquireSharedInterruptibly方法 |
| public final void acquireSharedInterruptibly(int arg) |
| throws InterruptedException { |
| if (Thread.interrupted()) |
| throw new InterruptedException(); |
| if (tryAcquireShared(arg) < 0) |
| doAcquireSharedInterruptibly(arg); |
| } |
| |
| # 查看实现类 |
| protected int tryAcquireShared(int arg) { |
| throw new UnsupportedOperationException(); |
| } |

| protected int tryAcquireShared(int acquires) { |
| return (getState() == 0) ? 1 : -1; |
| } |
| |
| |
| public final void acquireSharedInterruptibly(int arg) |
| throws InterruptedException { |
| if (Thread.interrupted()) |
| throw new InterruptedException(); |
| if (tryAcquireShared(arg) < 0) |
| doAcquireSharedInterruptibly(arg); |
| } |
| |
| |
| private void doAcquireSharedInterruptibly(int arg) |
| throws InterruptedException { |
| final Node node = addWaiter(Node.SHARED); |
| try { |
| for (;;) { |
| final Node p = node.predecessor(); |
| if (p == head) { |
| int r = tryAcquireShared(arg); |
| if (r >= 0) { |
| setHeadAndPropagate(node, r); |
| p.next = null; |
| return; |
| } |
| } |
| if (shouldParkAfterFailedAcquire(p, node) && |
| parkAndCheckInterrupt()) |
| throw new InterruptedException(); |
| } |
| } catch (Throwable t) { |
| cancelAcquire(node); |
| throw t; |
| } |
| } |
| public void countDown() { |
| sync.releaseShared(1); |
| } |
| |
| # 查看releaseShared方法 |
| public final boolean releaseShared(int arg) { |
| if (tryReleaseShared(arg)) { |
| doReleaseShared(); |
| return true; |
| } |
| return false; |
| } |
| |
| # 查看实现类 |
| protected boolean tryReleaseShared(int arg) { |
| throw new UnsupportedOperationException(); |
| } |

| protected boolean tryReleaseShared(int releases) { |
| |
| for (;;) { |
| int c = getState(); |
| if (c == 0) |
| return false; |
| int nextc = c - 1; |
| if (compareAndSetState(c, nextc)) |
| return nextc == 0; |
| } |
| } |
| |
| # 返回releaseShared方法 |
| public final boolean releaseShared(int arg) { |
| if (tryReleaseShared(arg)) { |
| doReleaseShared(); |
| return true; |
| } |
| return false; |
| } |
| |
| # 查看doReleaseShared方法 |
| private void doReleaseShared() { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| for (;;) { |
| Node h = head; |
| if (h != null && h != tail) { |
| int ws = h.waitStatus; |
| if (ws == Node.SIGNAL) { |
| if (!h.compareAndSetWaitStatus(Node.SIGNAL, 0)) |
| continue; |
| unparkSuccessor(h); |
| } |
| else if (ws == 0 && |
| !h.compareAndSetWaitStatus(0, Node.PROPAGATE)) |
| continue; |
| } |
| if (h == head) |
| break; |
| } |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
2021-05-16 真实机中安装CentOS7(一)