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

How to realize the CyclicBarrier of JUC

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces "how to realize the CyclicBarrier of JUC". In the daily operation, I believe that many people have doubts about how to realize the CyclicBarrier of JUC. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to achieve CyclicBarrier of JUC". Next, please follow the editor to study!

Insider implementation of CyclicBarrier

The CountDownLatch synchronizer introduced earlier is implemented based on AQS, while the CyclicBarrier introduced in this article does not directly inherit AQS's AbstractQueuedSynchronizer abstract class, but is implemented based on ReentrantLock locks. First, let's take a look at the field definition of CyclicBarrier, as follows:

Public class CyclicBarrier {/ * * CyclicBarrier's reentrant lock * / private final ReentrantLock lock = new ReentrantLock (); / * * conditional queue in which threads that have reached the barrier will wait for other threads * / private final Condition trip = lock.newCondition (); / * * number of threads participating * / private final int parties / * * callback function when all threads reach the barrier * / private final Runnable barrierCommand; / * current age object * / private Generation generation = new Generation (); / * current number of outstanding threads * / private int count; / /. Omit method definition}

The meaning of each of the above fields, such as code comments, let's further explain the generation field, which is of type Generation and is used to represent the age information of the current CyclicBarrier synchronizer. The inner class of Generation is defined as follows:

Private static class Generation {boolean broken = false;}

The CyclicBarrier#generation field is initialized when a new CyclicBarrier object is created. In addition, when all participating threads reach the barrier (also known as tripped), or when the CyclicBarrier is reset (that is, when the CyclicBarrier#reset method is called), a new Generation object is assigned to the CyclicBarrier#generation field, indicating the change of age.

The Generation#broken attribute defined by Generation is used to identify whether the current barrier has been broken. When the CyclicBarrier is reset, or a thread participating in the barrier is interrupted, a wait timeout, or an exception occurs when the callback function is executed, the barrier is broken. A broken barrier, or broken=true, causes the currently waiting thread, and the thread that is already in the waiting state, to throw a BrokenBarrierException exception and exit the current barrier process.

Because of the reusability of CyclicBarrier, multiple chronological information may coexist during the running of the program, but only one chronological object is active at any time, and the CyclicBarrier corresponding to the remaining chronological objects is either tripped or broken.

After introducing the field definition, let's analyze the method implementation of CyclicBarrier. First, let's take a look at the construction method. CyclicBarrier defines two construction methods, which are implemented as follows:

Public CyclicBarrier (int parties) {this (parties, null);} public CyclicBarrier (int parties, Runnable barrierAction) {if (parties 0L) {/ / enter conditional queue timeout waiting for nanos = trip.awaitNanos (nanos) }} catch (InterruptedException ie) {/ / the current thread is interrupted in response to the interrupt if (g = = generation & &! g.broken) {this.breakBarrier (); throw ie } else {/ / We're about to finish waiting even if we had not been interrupted, / / so this interrupt is deemed to "belong" to subsequent execution. Thread.currentThread () .interrupt ();}} / / the barrier has been broken if (g.broken) {throw new BrokenBarrierException ();} / / the current CyclicBarrier has been reset if (g! = generation) {return index } / / wait for timeout if (timed & & nanos)

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

Internet Technology

Wechat

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

12
Report