Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of await () of CountDownLatch source code

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces what is the use of await () of CountDownLatch source code. It is very detailed and has certain reference value. Friends who are interested must finish it.

The details are as follows

We already know that await can keep the current thread blocked until the latch count is zero (or thread interrupts).

The following is its source code.

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

Sync is the inner class of CountDownLatch. Here is the definition of it.

Private static final class Sync extends AbstractQueuedSynchronizer {...}

It inherits AbstractQueuedSynchronizer. The AbstractQueuedSynchronizer class is a very important class in java threads.

It provides a framework for implementing blocking locks and related synchronizers (such as signals, events, etc.) that rely on FIFO waiting queues.

Go on and jump into the AbstractQueuedSynchronizer class.

Sync.acquireSharedInterruptibly (1); ↓ public final void acquireSharedInterruptibly (int arg) / / AbstractQueuedSynchronizer throws InterruptedException {if (Thread.interrupted ()) throw new InterruptedException (); if (tryAcquireShared (arg))

< 0) doAcquireSharedInterruptibly(arg);} 这里有两个判断,首先判断线程是否中断,然后再进行下一个判断,这里我们主要看看第二个判断。 protected int tryAcquireShared(int acquires) { return (getState() == 0) ? 1 : -1;} 需要注意的是 tryAcquireShared 这个方法是在Sync 中实现的。 AbstractQueuedSynchronizer 中虽然也有对它的实现,但是默认的实现是抛一个异常。 tryAcquireShared 这个方法是用来查询当前对象的状态是否能够被允许获取锁。 我们可以看到Sync 中是通过判断state 是否为0 来返回对应的 int 值的。 那么 state 又代表什么? /** * The synchronization state. */ private volatile int state; 上面代码很清楚的表明 state 是表示同步的状态 。 需要注意的是 state 使用 volatile 关键字修饰。 volatile 关键字能够保证 state 的修改立即被更新到主存,当有其他线程需要读取时,会去内存中读取新值。 也就是保证了state的可见性。是最新的数据。 走到这里 state 是多少呢? 这里我们就需要看一看CountDownLatch 的 构造函数了。 CountDownLatch end = new CountDownLatch(2); ↓public CountDownLatch(int count) { if (count < 0) throw new IllegalArgumentException("count < 0"); this.sync = new Sync(count);} ↓Sync(int count) { setState(count);} 原来构造函数中的数字就是这个作用啊,用来set state 。 所以我们这里state == 2 了。tryAcquireShared 就返回 -1。进入到下面 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;}} if (shouldParkAfterFailedAcquire (p, node) & & parkAndCheckInterrupt () throw new InterruptedException ();}} finally {if (failed) cancelAcquire (node);}}

OK, this code is a little long, and several functions are called in it. Let's look at it one by one.

A new class Node appears on the first line.

Node is an inner class in the AQS (AbstractQueuedSynchronizer) class that defines a chained structure. As shown below.

+-+ prev +-+-+ head | |

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report