Java concurrency topic: cyclicbarrier To create a secure access control system

Source: Internet
Author: User

Continue the Concurrency topic ~

This introduction Cyclicbarrier: Take a look at the API gaze:

/** * A synchronization aid that allows a set of threads to all wait for * each other to reach A common barrier point.
   
    cyclicbarriers is * useful in programs involving a fixed sized party of threads it * must occasionally wait for each o Ther. The barrier is called * <em>cyclic</em> because it can being re-used after the waiting threads * is released.
   

A synchronous helper class that allows a group of threads to block to a location at the same time. It is useful in scenarios that include fixed threads and must wait between threads. Cyclic means that cyclicbarrier can be reused when all the waiting threads are released.

(This is the English level.)

。。 )

Cyclicbarrier similar to a gate, the specified number of threads must reach this gate before the gate will open.

The following uses Cyclicbarrier to simulate an access control system:

The demand is this: by the time of school, all the students must swipe their cards. Then the number of their own initiative to open the door, reunification home. This requirement has just been avoided by leaving part of the children at school in a critical situation. Especially kindergarten or elementary school students ~

Package Com.zhy.concurrency.cyclic;import Java.util.concurrent.brokenbarrierexception;import  java.util.concurrent.cyclicbarrier;/** * Secure Access System * * @author Zhy * */public class cyclicbarriertest{/** * Total number of students */private Final int student_count = 10;/** * When the person is aligned. Own initiative Open Door program */final cyclicbarrier barrier = new Cyclicbarrier (student_count,new Runnable () {@Overridepublic void run () { System.out.println ("People are here.") Open the door ....);}); public void GoHome () throws Interruptedexception, Brokenbarrierexception{system.out.println (Thread.CurrentThread (). GetName () + "has been swiped. Waiting to open the door home ~ "); barrier.await (); System.out.println (Thread.CurrentThread (). GetName () + "home from school ~"); public static void Main (string[] args) throws Interruptedexception,brokenbarrierexception{final Cyclicbarriertest Instance = new Cyclicbarriertest ();/** * Each thread represents a student */for (int i = 0; I < instance. Student_count; i++) {New Thread ("student" + i + "") {public void run () {try{instance.gohome ();} catch (Interruptedexception e) {e.printstacktrace ();} catch (Brokenbarrierexception e) {e.printstacktrace ();}};}. Start ();}}}

Output Result:

Student 1  has been credit card, waiting to open the door to go home ~ 3  have been credit card, waiting to open the door home ~ 5  have been credit card, waiting to open the door home ~ Students 9  have been credit card, waiting to open the door home ~ Students 7  have been swipe, waiting to open the door home ~ 0  have Waiting to open the door home ~ Student 2  has been swipe. Waiting to open the door home ~ Student 6  has been swipe. Waiting to open the door home ~ Student 8  has been swipe. Waiting to open the door home ~ 4  have been credit card, waiting to open the door home ~ people to the QI, open the door .... Students 4  School Home ~ 1 School Home ~ Students 3 home from school ~ Students 5 School Home ~ Students 9 School Home ~ Students 2 School Home ~ Students 6 from school home ~ students  come back to class  0 School Home ~ Students 7  School Home ~ Students 8  home from school ~

Haha, assuming which kindergarten uses such a system, the child should not lose school. ,,。 Joking about;

Cyclicbarrier all the threads in a valve position, and wait until the number of threads waiting reaches the preset value. Just open the valve. Remember that it is blocking the thread, not blocking the operation, the same thread hard to drop the await is no effect.


The example above shows the basic use of cyclicbarrier. But cyclic's function does not show, since the gaze, we need to take a sample to see:

We reformed our access control, after all, the card is not realistic, now demand is this: students go too critical, now the guard from school at the door guarding. Let the students walk in groups of 3.

Package Com.zhy.concurrency.cyclic;import Java.util.concurrent.brokenbarrierexception;import java.util.concurrent.cyclicbarrier;/** * Modified access control system * * @author Zhy * */public class cyclicbarriertest2{/** * Total number of students */priva Te final int student_count = 12;/** * 3 people per group out of the */final cyclicbarrier barrier = new Cyclicbarrier (3,new Runnable () {@Overrid epublic void Run () {System.out.println ("3 students have arrived, released ...);}); public void GoHome () throws Interruptedexception, Brokenbarrierexception{system.out.println (Thread.CurrentThread (). GetName () + "has been swiped. Wait for the door to go home ~ "); barrier.await ();} public static void Main (string[] args) throws Interruptedexception,brokenbarrierexception{final CyclicBarrierTest2 Instance = new CyclicBarrierTest2 ();/** * Each thread represents a student */for (int i = 0; I < instance. Student_count; i++) {New Thread ("student" + i + "") {public void run () {try{instance.gohome ();} catch (Interruptedexception e) {e.printstacktrace ();} catch (Brokenbarrierexception e) {e.printstacktrace ();}};}. Start ();}}}
Output Result:

Student 0  has been swiping, waiting to open the door home ~ 1  has been credit card. Waiting to open the door home ~ Student 2  has been swipe. Waiting to open the door home ~ There are 3 students arrived, released .... Student 3  has been swiped. Waiting to open the door home ~ 5  have been credit card, waiting to open the door to go home ~ 7  have been credit card, waiting to open the door home ~ There are 3 students to the QI. Release.... Student 4  has been swipe, waiting to open the door home ~ 9  has been credit card, waiting to open the door home ~ 6  have been credit card. Waiting to open the door home ~ There are 3 students arrived, released .... Students  have been credit card, waiting to open the door to go home ~ students  have credit card, waiting to open the door home ~ 8  have been credit card, waiting to open the door home ~ There are 3 students to the QI, release ....

This example fully embodies the reusability of Cyclicbarrier, yes, this system may be more real, 0 cost ~ haha ~.


Well, interested in the welcome message,

Java concurrency topic: cyclicbarrier To create a secure access control system

Related Article

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.