栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

CountDownLatch实现原理

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

CountDownLatch实现原理

文章目录

前置知识构造器怎么阻塞当前线程的怎么唤醒被阻塞线程


 public static void main(String[] args) throws InterruptedException {

       CountDownLatch countDownLatch = new CountDownLatch(3);
       for (int i = 0; i < 3; i++) {
           new Thread(()->{
               countDownLatch.countDown();
           }).start();
       }

       System.out.println("阻塞main线程");
       countDownLatch.await();
       System.out.println("main线程放行");

   }

前置知识

AQSCAS 构造器

1.初始化内部sync属性。
2.设置AQS state属性

public CountDownLatch(int count) {
     if (count < 0) throw new IllegalArgumentException("count < 0");
     this.sync = new Sync(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 zero
        for (;;) {
            int c = getState();
            if (c == 0)
                return false;
            int nextc = c-1;
            if (compareAndSetState(c, nextc))
                return nextc == 0;
        }
    }
}
怎么阻塞当前线程的
countDownLatch.await();

public final void acquireSharedInterruptibly(int arg)
        throws InterruptedException {
    if (Thread.interrupted())
        throw new InterruptedException();
   	//判断当前state  (getState() == 0) ? 1 : -1;
   	//!=0 表示 count数量的线程内有走完
    if (tryAcquireShared(arg) < 0)
        doAcquireSharedInterruptibly(arg);
}

//
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);
                if (r >= 0) {
                    setHeadAndPropagate(node, r);
                    p.next = null; // help GC
                    failed = false;
                    return;
                }
            }
            //修改node等待状态, LockSupport.park(this);阻塞当前线程
            if (shouldParkAfterFailedAcquire(p, node) &&
                parkAndCheckInterrupt())
                throw new InterruptedException();
        }
    } finally {
        if (failed)
            cancelAcquire(node);
    }
}
怎么唤醒被阻塞线程
countDownLatch.countDown();

public final boolean releaseShared(int arg) {
	//state-1,  return state == 0;
    if (tryReleaseShared(arg)) {
    	//state == 0 唤醒队列中阻塞线程
        doReleaseShared();
        return true;
    }
    return false;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/709091.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号