Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

并发工具类CountDownLatch源码分析 #26

Open
diaosichengxuyuan opened this issue Feb 18, 2019 · 0 comments
Open

并发工具类CountDownLatch源码分析 #26

diaosichengxuyuan opened this issue Feb 18, 2019 · 0 comments

Comments

@diaosichengxuyuan
Copy link
Owner

diaosichengxuyuan commented Feb 18, 2019

CountDownLatch是一个多线程同步的工具类,基于AQS实现

调用await方法的线程统称为等待线程,调用countDown方法的线程统称执行线程

public class CountDownLatch {

     private volatile int state;
     
    //初始化类时,同时初始化state值
    public CountDownLatch(int count) {
        if (count < 0) throw new IllegalArgumentException("count < 0");
        this.sync = new Sync(count);
    }
   
    //等待线程使用的方法
    public void await() throws InterruptedException {
        //共享式获取状态1
        sync.acquireSharedInterruptibly(1);
    }
   
    //执行线程使用的方法,执行完业务逻辑调用该方法
    public void countDown() {
        sync.releaseShared(1);
    }
   
    private static final class Sync extends AbstractQueuedSynchronizer {
        private static final long serialVersionUID = 4982264981922014374L;

        Sync(int count) {
            setState(count);
        }

        int getCount() {
            return getState();
        }
        
        //共享式获取状态1,如果当前状态为0,表示所有执行线程都运行结束
        //了,返回1;如果状态不为0,表示仍然有执行线程在运行,返回-1
        protected int tryAcquireShared(int acquires) {
            return (getState() == 0) ? 1 : -1;
        }

        //死循环式的释放状态,当状态不是0时,返回false;当最终状态是0
        //时,返回true,表示没有执行线程在运行了
        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;
            }
        }
    }
public abstract class AbstractQueuedSynchronizer
    extends AbstractOwnableSynchronizer
    implements java.io.Serializable {
    
    //共享式获取状态
    public final void acquireSharedInterruptibly(int arg)
            throws InterruptedException {
        if (Thread.interrupted())
            throw new InterruptedException();
        //如果尝试共享式的获取状态1失败,加入同步队列并自旋,
        //doAcquireSharedInterruptibly方法就不分析了
        if (tryAcquireShared(arg) < 0)
            doAcquireSharedInterruptibly(arg);
    }
    
    //共享式释放状态
    public final boolean releaseShared(int arg) {
        //如果尝试释放状态1成功,则唤醒同步队列中的线程,
        //doReleaseShared就不分析了
        if (tryReleaseShared(arg)) {
            doReleaseShared();
            return true;
        }
        return false;
    }
}    

作者原创,转载请注明出处,违法必究!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant