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

Example Analysis of Circular fence CyclicBarrier

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "CyclicBarrier example analysis", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "CyclicBarrier example analysis"!

CyclicBarrier, from the English word meaning can be understood as it can be recycled, and the fence is used to prevent threads from waiting outside the fence, it is also to prevent threads from waiting, it and CountDownLatch bottom what is the difference?

Give you a problem, 5 threads are executed at the same time, when each thread has executed a certain code and then continue to execute, if 5 threads have not arrived, otherwise the other threads have been waiting.

From the above topic, let's look at the following example to understand what a circular fence is.

First, the CyclicBarrier constructor accepts 5 threads and callback threads when the threads are clustered. What does that mean? That is, when all five threads call the await() method, these five threads continue to execute, and the callback thread runs to the synchronization time before execution.

From the example output results can be seen cb finish run first, indicating that to run a callback thread, and then output the time spent on each thread, we control 10 threads to block waiting, and loop fence only 5, indicating that it can be recycled, every 5 threads blocked to continue execution.

Note that the await method will throw exceptions, one is the interrupt exception InterruptedException, that is, other threads interrupt this thread, and the other is BrokenBarrierException, if one thread is interrupted InterruptedException, other threads waiting outside the loop fence will raise BrokenBarrierException. Because a thread was interrupted, breaking the integrity of the loop fence, causing the thread to fail to get together and the other threads to never continue executing, this exception also prevents the thread from waiting forever.

At this point, I believe that we have a deeper understanding of "CyclicBarrier example analysis," may wish to actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

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