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

Analysis of CyclicBarrier Source Code in Java

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

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of CyclicBarrier source code analysis in Java, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value, I believe you will have something to gain after reading this Java CyclicBarrier source code analysis article, let's take a look at it.

Introduction to CyclicBarrier

For CountDownLatch, the other threads are gamers, such as League of Legends, and the main thread is the thread that controls the start of the game. Until all the players are ready, the main thread is waiting, that is, the game cannot start. When all the players are ready, the next action implementer is the main thread, that is, start the game.

For CyclicBarrier, suppose a company wants all its staff to carry out team-building activities, the content of which is to climb over three obstacles, and each person takes different time to climb over the obstacles. But the company requires everyone to climb over the current obstacle before starting to climb the next obstacle, that is, after everyone has climbed the first obstacle, before starting to climb the second obstacle, and so on. Analogically, each employee is a "other thread". The program ends when everyone has climbed over all the obstacles. The main thread may have ended a long time ago, so we don't have to worry about the main thread here.

Inheritance relationship of CyclicBarrier Source Code Analysis Class

CyclicBarrier does not show which parent class to inherit or which parent interface to implement, and all AQS and reentrant locks are not implemented through inheritance, but through composition.

There is an inner class Generation in the inner class CyclicBarrier of public class CyclicBarrier {} ```# # class. The CycBarrier used each time can be regarded as an instance of Generation. Its source code is as follows: ````javaprivate static class Generation {boolean broken = false;}

Description: the Generation class has a property broken that indicates whether the current barrier is damaged.

The property of the class public class CyclicBarrier {/ * * The lock for guarding barrier entry * / / reentrant lock private final ReentrantLock lock = new ReentrantLock (); / * * Condition to wait on until tripped * / / conditional queue private final Condition trip = lock.newCondition (); / * * The number of parties * / / number of participating threads private final int parties;/* The command to run when tripped * / / the operation private final Runnable barrierCommand performed by the last thread entering barrier / * * The current generation * / / current generation private Generation generation = new Generation (); / / number of threads waiting to enter the barrier private int count;}

Note: this property has one ReentrantLock object, one Condition object, and the Condition object is based on AQS, so, in the final analysis, the underlying layer is supported by AQS.

Constructor of class

CyclicBarrier (int, Runnable) type constructor

Public CyclicBarrier (int parties, Runnable barrierAction) {/ / the number of participating threads is less than or equal to 0, an exception if (parties 0L) / / sets the waiting time, and the waiting time is greater than 0 / the specified length of time nanos = trip.awaitNanos (nanos);} catch (InterruptedException ie) {if (g = = generation & &! G.broken) {/ / equals the current generation and the barrier is not damaged / / the current barrier breakBarrier (); / / throws an exception throw ie;} else {/ / does not mean that the current band is the barrier damaged / / We're about to finish waiting even if we had not// been interrupted, the so this interrupt is deemed to// "belong" to subsequent execution.// interrupts the current thread Thread.currentThread (). Interrupt () }} if (g.broken) / / the barrier is damaged and an exception throw new BrokenBarrierException () is thrown; if (g! = generation) / / is not equal to the current generation / / return index return index;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

Development

Wechat

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

12
Report