Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is CountDownLatch?

Shulou Source: shulou.com Published: 2022-06-02 04:30:27 09月16日 Update

Today, I would like to talk to you about what CountDownLatch is, many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something from this article.

CountDownLatch is also based on AQS, which is an implementation of AQS sharing function.

one

CountDownLatch construction

Public CountDownLatch (int count) {

If (count

< 0) throw new IllegalArgumentException("count < 0"); this.sync = new Sync(count); } count最终传递给state ,countDown也是对于state状态的改变 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; } } 2 countDown实现 public void countDown() { sync.releaseShared(1); } public final boolean releaseShared(int arg) { if (tryReleaseShared(arg)) { doReleaseShared(); return true; } return false; } 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; } } private void unparkSuccessor(Node node) { /* * If status is negative (i.e., possibly needing signal) try * to clear in anticipation of signalling. It is OK if this * fails or if status is changed by waiting thread. */ int ws = node.waitStatus; if (ws < 0) compareAndSetWaitStatus(node, ws, 0); /* * Thread to unpark is held in successor, which is normally * just the next node. But if cancelled or apparently null, * traverse backwards from tail to find the actual * non-cancelled successor. */ Node s = node.next; if (s == null || s.waitStatus >

0) {

S = null

For (Node t = tail; t! = null & & t! = node; t = t.prev)

If (t.waitStatus 0 | | h = = null | | h.waitStatus 0) {do {node.prev = pred = pred.prev;} while (pred.waitStatus > 0); pred.next = node;} else {/ / the wait state is set to subsequent wake-up compareAndSetWaitStatus (pred, ws, Node.SIGNAL);} return false;} / / current thread blocking to determine whether the thread interrupts private final boolean parkAndCheckInterrupt () {LockSupport.park (this); return Thread.interrupted () } / / cancel the current node acquisition lock private void cancelAcquire (Node node) {/ / Ignore if node doesn't existif (node = = null) return;node.thread = null;// Skip cancelled predecessorsNode pred = node.prev;while (pred.waitStatus > 0) node.prev = pred = pred.prev;Node predNext = pred.next; / / if neither node has been cancelled, then this node and node are the same node / / node's successor node cancel node.waitStatus = Node.CANCELLED / / If we are the tail, remove ourselves.// CountDownLatch logic ends here if (node = = tail & & compareAndSetTail (node, pred)) {compareAndSetNext (pred, predNext, null);} else {int ws;if (pred! = head & & (ws = pred.waitStatus) = Node.SIGNAL | (ws)

Tags: Nodes threads content status tasks successors functions locations right and wrong more knowledge articles industry information information channels logic channels one support related Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Shulou Information Redmi Xiaomi vpn