Java multithreading-basic usage of the javasicbarrier class

Source: Internet
Author: User

Java multithreading-basic usage of the javasicbarrier class

Java. util. concurrent. CylicBarrier is a synchronization helper class that allows a group of threads to wait for each other until a common barrier point is reached ). In a program involving a set of fixed threads, these threads must wait for each other from time to time. In this case, javasicbarrier is useful. This barrier can be reused after the waiting thread is released, so it is called a circular barrier. Javasicbarrier supports an optional Runnable command. After the last thread in a group of threads arrives (but before all threads are released), the command runs only once at each barrier point. This barrier operation is useful if the sharing status is updated before all participating threads continue.

Main Methods:
// Set the size, that is, the number of threads.
1. CyclicBarrier (int)

2. When the number of await reaches the set number, first execute the Runnable object: CyclicBarrier (int, Runnable)

3. Notify barrier that the thread has been completed: await ()

Package cn. baokx; import java. util. random; import java. util. concurrent. brokenBarrierException; import java. util. concurrent. cyclicBarrier; import java. util. concurrent. timeUnit; class WorkThread implements Runnable {private static int work_num = 4; private javasicbarrier cb; private String name; public WorkThread (CyclicBarrier cb, String name) {this. cb = cb; this. name = name ;}@ Overridepublic void run () {try {T ImeUnit. SECONDS. sleep (new Random (). nextInt (10); System. out. println (this. name + "completed first-stage work"); if (cb. getNumberWaiting () = (work_num-1) {System. out. println ("Everyone has completed the first stage of work, ready for the second stage of work... ");} Try {cb. await ();} catch (BrokenBarrierException e) {e. printStackTrace ();} TimeUnit. SECONDS. sleep (new Random (). nextInt (10); System. out. println (this. name + "the second stage of work has been completed"); if (cb. getNumberWaiting () = (work_num-1) {System. out. println ("Everyone has completed the second stage of work, preparing for the third stage of work... ");} Try {cb. await ();} catch (BrokenBarrierException e) {e. printStackTrace ();} TimeUnit. SECONDS. sleep (new Random (). nextInt (10); System. out. println (this. name + "finished stage 3"); if (cb. getNumberWaiting () = (work_num-1) {System. out. println ("Everyone finished the third stage... "); System. out. println (" the company's leadership was quite satisfied. All of them had a week off... ");} Try {cb. await ();} catch (BrokenBarrierException e) {e. printStackTrace () ;}} catch (InterruptedException e) {e. printStackTrace () ;}} public class javasicbarriertest {public static void main (String [] args) {javasicbarrier cb = new javasicbarrier (4); new Thread (new WorkThread (cb, "zhangsan ")). start (); new Thread (new WorkThread (cb, "lisi ")). start (); new Thread (new WorkThread (cb, "wangwu ")). start (); new Thread (new WorkThread (cb, "zhaoliu ")). start ();}}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.