In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how Java multithreading uses synchronization tool class CyclicBarrier". In the operation of actual cases, many people will encounter such a dilemma, 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!
1 CyclicBarrier method description
The methods provided by CyclicBarrier are:
CyclicBarrier (parties): the constructor that initializes the number of threads waiting for each other.
CyclicBarrier (parties,Runnable barrierAction): initializes the number of threads waiting for each other and the construction method of the barrier thread.
The timing of the barrier thread:
Number of threads waiting = after parties, before CyclicBarrier opens the barrier.
For example: in the grouping calculation, each thread is responsible for part of the calculation. Finally, after the calculation of these threads is finished, it is handed over to the barrier thread for summary calculation.
Int getParties (): gets the number of threads that CyclicBarrier opens the barrier, which also becomes the square.
Int getNumberWaiting (): gets the number of threads waiting on the CyclicBarrier.
Int await (): block wait on CyclicBarrier until one of the following occurs:
If the number of threads waiting on the CyclicBarrier reaches parties, all threads are released and execution continues.
If the current thread is interrupted, an InterruptedException exception is thrown, and the wait is stopped and execution continues.
If other waiting threads are interrupted, the current thread throws a BrokenBarrierException exception, stops waiting and continues execution.
If the other waiting thread times out, the current thread throws a BrokenBarrierException exception, stops waiting and continues execution.
If another thread calls the CyclicBarrier.reset () method, the current thread throws a BrokenBarrierException exception, stops waiting and continues execution.
Int await (timeout,TimeUnit): performs a time-limited blocking wait on the CyclicBarrier until one of the following occurs:
If the number of threads waiting on the CyclicBarrier reaches parties, all threads are released and execution continues.
If the current thread is interrupted, an InterruptedException exception is thrown, and the wait is stopped and execution continues.
If the current thread waits for a timeout, it throws a TimeoutException exception, stops waiting and continues execution.
If other waiting threads are interrupted, the current thread throws a BrokenBarrierException exception, stops waiting and continues execution.
If the other waiting thread times out, the current thread throws a BrokenBarrierException exception, stops waiting and continues execution.
If another thread calls the CyclicBarrier.reset () method, the current thread throws a BrokenBarrierException exception, stops waiting and continues execution.
Boolean isBroken (): gets the value of whether the flag bit broken is broken. This value can be found in the following cases:
When CyclicBarrier is initialized, broken=false indicates that the barrier is not broken.
If the waiting thread is interrupted, broken=true indicates that the barrier is broken.
If the waiting thread times out, broken=true indicates that the barrier is broken.
If a thread calls the CyclicBarrier.reset () method, broken=false indicates that the barrier is back in an unbroken state.
Void reset (): brings CyclicBarrier back to its original state, and visually it does two things:
If there are waiting threads, a BrokenBarrierException exception is thrown, and those threads stop waiting and continue execution.
Set the breakage flag bit broken to false.
2 CyclicBarrier instance
If there are several threads that want to write data, and only after all threads have finished writing data, can these threads continue to do the next thing, then you can take advantage of CyclicBarrier:
Public static void main (String [] args) {int N = 4; CyclicBarrier barrier = new CyclicBarrier (N); for (int itemositani)
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.