Java 多线程 AQS 之 CountDownLatch

 

部分锁释放,部分没有释放,await 会一直等待全部锁释放

    static void work(CountDownLatch lock) {
        System.out.println(Thread.currentThread().getName() + " 释放1把锁");
        lock.countDown();
    }

    /**
     * 部分锁释放,部分没有释放,await 会一直等待全部锁释放
     */
    /*
    private final boolean parkAndCheckInterrupt() {
        //Thread.currentThread().getName() 是主线程
        LockSupport.park(this);
        return Thread.interrupted();
    }
     */
    public static void someReleaseTest() {
        //分配3把共享锁
        CountDownLatch lock = new CountDownLatch(3);

        new Thread(() -> work(lock)).start();
        new Thread(() -> work(lock)).start();

        try {
            lock.await();
            System.out.println("全部锁已释放");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


输出:
Thread-0 释放1把锁
Thread-1 释放1把锁

一直等待

 

分配的3把锁全部释放,main 线程继续运行

    /**
     * 分配的3把锁全部释放,main 线程继续运行
     */
    public static void allReleaseTest() {
        //分配3把共享锁
        CountDownLatch lock = new CountDownLatch(3);

        new Thread(() -> work(lock)).start();
        new Thread(() -> work(lock)).start();
        new Thread(() -> work(lock)).start();

        try {
            //直接返回,不需要part
            lock.await();
            System.out.println("全部锁已释放");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


输出:
Thread-0 释放1把锁
Thread-1 释放1把锁
Thread-2 释放1把锁
全部锁已释放

 

源码解析

new CountDownLatch(3)

//构造 CountDownLatch
public CountDownLatch(int count) {
    if (count < 0) throw new IllegalArgumentException("count < 0");
    this.sync = new Sync(count);
}

//CountDownLatch 的内部类 Sync
private static final class Sync extends AbstractQueuedSynchronizer {

    Sync(int count) {
    	//父类 AQS 的方法
        setState(count);
    }
...
}

 

lock.await();

public void await() throws InterruptedException {
    sync.acquireSharedInterruptibly(1);
}

//AQS 
public abstract class AbstractQueuedSynchronizer
    extends AbstractOwnableSynchronizer
    implements java.io.Serializable {
    ...
	public final void acquireSharedInterruptibly(int arg)
	        throws InterruptedException {
	    if (Thread.interrupted())
	        throw new InterruptedException();
	    if (tryAcquireShared(arg) < 0)
	        doAcquireSharedInterruptibly(arg);
	}
}

//CountDownLatch Sync 实现父类方法
public class CountDownLatch {
	...
    private static final class Sync extends AbstractQueuedSynchronizer {
    	...
    	//全部锁释放返回1
        protected int tryAcquireShared(int acquires) {
            return (getState() == 0) ? 1 : -1;
        }
    }	
	...
}


//如果还有锁没有释放,执行 doAcquireSharedInterruptibly
public abstract class AbstractQueuedSynchronizer
    extends AbstractOwnableSynchronizer
    implements java.io.Serializable {
    ...
    private void doAcquireSharedInterruptibly(int arg)
        throws InterruptedException {

		...
        if (shouldParkAfterFailedAcquire(p, node) && parkAndCheckInterrupt())
            throw new InterruptedException();    
    }
    ...
}


//真正part线程的方法
private final boolean parkAndCheckInterrupt() {
    LockSupport.park(this);
    return Thread.interrupted();
}

 

  • 0
    点赞
  • 1
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

打赏
文章很值,打赏犒劳作者一下
相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页

打赏

jamie956

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者