Continue concurrent topics ~
CyclicBarrier: Let's take a look at the API comments:
/** * A synchronization aid that allows a set of threads to all wait for * each other to reach a common barrier point. CyclicBarriers are * useful in programs involving a fixed sized party of threads that * must occasionally wait for each other. The barrier is called * cyclic because it can be re-used after the waiting threads * are released.
A synchronization helper class that blocks a group of threads to a single position at the same time. It is useful in scenarios that contain fixed threads and threads must wait for each other. Cyclic means that when all the waiting threads are released, javasicbarrier can be reused. (The level of English is like this ....)
CyclicBarrier is similar to a gate. a specified number of threads must arrive at the gate to open the gate.
The following uses javasicbarrier to simulate an access control system:
The requirement is as follows: by the end of school hours, all students must swipe their cards, and then the number of students will automatically open the door and go home. This demand has just avoided the danger of leaving some children in school, especially in kindergartens or primary school students ~~
Package com. zhy. concurrency. cyclic; import java. util. concurrent. brokenBarrierException; import java. util. concurrent. cyclicBarrier;/*** Secure Access Control System ** @ author zhy **/public class CyclicBarrierTest {/*** Total number of students */private final int STUDENT_COUNT = 10; /*** automatically opens the door when a person arrives */final javasicbarrier barrier = new javasicbarrier (STUDENT_COUNT, new Runnable () {@ Overridepublic void run () {System. out. println ("people have arrived. open the door .... "); }); Public void goHome () throws InterruptedException, BrokenBarrierException {System. out. println (Thread. currentThread (). getName () + "swipe your card and wait for the door to go home ~ "); Barrier. await (); System. out. println (Thread. currentThread (). getName () +" going home from school ~ ");} Public static void main (String [] args) throws InterruptedException, BrokenBarrierException {final policicbarriertest instance = new javasicbarriertest (); /*** 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 swiped the card and is waiting to open the door to return home ~ Student 3 has swiped the card and is waiting for the door to go home ~ Student 5 has swiped the card and is waiting to open the door to return home ~ Student 9 has swiped the card and is waiting to open the door to return home ~ Student 7 has swiped the card and is waiting to open the door to return home ~ Student 0 has swiped the card and is waiting to open the door to return home ~ Student 2 has swiped the card and is waiting to open the door to return home ~ Student 6 has swiped the card and is waiting for the door to go home ~ Student 8 has swiped the card and is waiting to open the door to return home ~ Student 4 has swiped the card and is waiting for the door to go home ~ People are here. Open the door... Student 4 is going home from school ~ Student 1 goes home from school ~ Student 3: Go home from school ~ Students 5 go home from school ~ Student 9 going home from school ~ Student 2: Go home from school ~ Students 6 go home from school ~ Students go home from school ~ 7. Students go home from school ~ 8. Students go home from school ~
Haha, if a kindergarten uses such a system, the children should not lose their school ;;
CyclicBarrier blocks all threads in one valve position, and then opens the valve when the number of waiting threads reaches the preset value. Remember that it is a blocking thread, not a blocking operation. It is ineffective to force await in the same thread.
The above example shows the basic usage of javasicbarrier, but the cyclic function is not displayed. As mentioned in the comment, we need to take an example to see:
After all, the card swiping is not realistic. The demand is as follows: It is too dangerous for students to walk by themselves. Now, the guard guards at the door to let the students go in groups.
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 */private final int STUDENT_COUNT = 12; /*** a group of three people go out */final javasicbarrier barrier = new javasicbarrier (3, new Runnable () {@ Overridepublic void run () {System. out. println ("three students have arrived. Let's go .... ") ;}}); publi C void goHome () throws InterruptedException, BrokenBarrierException {System. out. println (Thread. currentThread (). getName () + "swipe your card and wait for the door to go home ~ "); Barrier. await ();} public static void main (String [] args) throws InterruptedException, BrokenBarrierException {final javasicbarriertest2 instance = new javasicbarriertest2 (); /*** 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 swiped the card and is waiting to open the door to return home ~ Student 1 has swiped the card and is waiting to open the door to return home ~ Student 2 has swiped the card and is waiting to open the door to return home ~ Three students arrived, and let go... student 3 has swiped the card, waiting for the door to go home ~ Student 5 has swiped the card and is waiting to open the door to return home ~ Student 7 has swiped the card and is waiting to open the door to return home ~ Three students arrived and let go... Student 4 has swiped the card and is waiting to open the door and go home ~ Student 9 has swiped the card and is waiting to open the door to return home ~ Student 6 has swiped the card and is waiting for the door to go home ~ Three students arrived, and let go... student 11 has swiped the card and is waiting to open the door and go home ~ Student 10 has swiped the card and is waiting to open the door to return home ~ Student 8 has swiped the card and is waiting to open the door to return home ~ Three students arrived. Let's go ....
This example fully demonstrates the reusability of javasicbarrier, right? Such a system may be more practical and cost-effective ~ Haha ~.
OK. If you are interested, please leave a message,