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 use CyclicBarrier in Java

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

Share

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

This article introduces the relevant knowledge of "how to use CyclicBarrier in Java". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

CyclicBarrier, like CountDownLatch, also needs to pass a parameter of type int in the constructor, but the meaning of this parameter is different from that of CountDownLatch. This parameter represents the number of intercepted threads. When a thread calls the await () method in CyclicBarrier, it tells CyclicBarrier that I have intercepted the current thread, that is, the thread calling the await () method will be blocked. The code following the await () method in the thread will not execute, and the current thread will wait until the number of threads calling the await () method is the same as the number of parameters in the CyclicBarrier constructor. Let's take a look at a simple example to demonstrate the use of CyclicBarrier.

Let's analyze the output information in detail. The argument we passed in the CyclicBarrier constructor is 2, which means it will intercept the await () method of two threads before executing the logic behind the await () method. So the thread does not block until the await () method is called. So we will give the output start, timing and other information, we will find that when the thread Thread-1 has finished executing the loop task, it does not immediately output the code behind the await () method, but the thread is blocked because Thread-0 has not yet been implemented and it has not yet executed the await () method, that is to say, the number of intercepted threads has not reached the number of passes in the CyclicBarrier constructor. So thread Thread-1 blocks until Thread-0 finishes executing the loop and executes the await () method, when the number of intercepted threads is equal to the parameters in the constructor, so thread Thread-1 resumes execution. This is the use of CyclicBarrier.

The difference between CyclicBarrier and CountDownLatch:

CountDownLatch is blocking the main thread

CountDownLatch counters can only be used once, that is, they can only be decremented.

CyclicBarrier is blocking child threads.

Counters for CyclicBarrier can be reset using the reset () method

CyclicBarrier also provides another constructor that can pass a Runnable type in addition to parameters of type int. Its purpose is to give priority to the tasks in the constructor when the number of intercepts of the threads is equal to the parameters of the constructor, and then execute the code following the await () method in each thread.

This is the end of the content of "how to use CyclicBarrier in Java". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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