Java Concurrency Theme: Cyclic Barrier Creates a Secure Access Control System

Continue concurrent topics~

This time, Cyclic Barrier: Take a look at the API annotations:

  1. /**  
  2.  * A synchronization aid that allows a set of threads to all wait for  
  3.  * each other to reach a common barrier point.  CyclicBarriers are  
  4.  * useful in programs involving a fixed sized party of threads that  
  5.  * must occasionally wait for each other. The barrier is called  
  6.  * <em>cyclic</em> because it can be re-used after the waiting threads  
  7.  * are released.  

General meaning: A synchronization auxiliary class that blocks a group of threads to a location at the same time. It is very useful in scenarios where there are fixed threads and threads have to wait for each other. cyclic Barrier means cyclic Barrier can be reused when all the waiting threads are released. That's all for English proficiency.

Cyclic Barrier is similar to a gate, where a specified number of threads must reach before the gate opens.

Next, use Cyclic Barrier to simulate an access control system:

The demand is this: by the end of school, all students must swipe their cards, and then the number of people automatically open the door, unified home. This requirement is just enough to avoid the danger of leaving some children in school, especially kindergartens or primary school students.~~

  1. package com.zhy.concurrency.cyclic;  
  2.   
  3. import java.util.concurrent.BrokenBarrierException;  
  4. import java.util.concurrent.CyclicBarrier;  
  5.   
  6. /** 
  7.  * Safe Access Control System 
  8.  *  
  9.  * @author zhy 
  10.  *  
  11.  */  
  12. public class CyclicBarrierTest  
  13. {  
  14.   
  15.     /** 
  16.      * Total number of students 
  17.      */  
  18.     private final int STUDENT_COUNT = 10;  
  19.   
  20.     /** 
  21.      * When people arrive, automatic door opening procedure 
  22.      */  
  23.     final CyclicBarrier barrier = new CyclicBarrier(STUDENT_COUNT,  
  24.             new Runnable()  
  25.             {  
  26.                 @Override  
  27.                 public void run()  
  28.                 {  
  29.                     System.out.println("Everybody's here. Open the door.....");  
  30.                 }  
  31.             });  
  32.   
  33.     public void goHome() throws InterruptedException, BrokenBarrierException  
  34.     {  
  35.         System.out.println(Thread.currentThread().getName() + "I've swiped my card and waited for the door to open.~");  
  36.         barrier.await();  
  37.         System.out.println(Thread.currentThread().getName() + "Come home from school~");  
  38.     }  
  39.   
  40.     public static void main(String[] args) throws InterruptedException,  
  41.             BrokenBarrierException  
  42.     {  
  43.   
  44.         final CyclicBarrierTest instance = new CyclicBarrierTest();  
  45.   
  46.         /** 
  47.          * Each thread represents a student 
  48.          */  
  49.         for (int i = 0; i < instance.STUDENT_COUNT; i++)  
  50.         {  
  51.             new Thread("Student" + i +"  " )  
  52.             {  
  53.                 public void run()  
  54.                 {  
  55.   
  56.                     try  
  57.                     {  
  58.                         instance.goHome();  
  59.                     } catch (InterruptedException e)  
  60.                     {  
  61.                         e.printStackTrace();  
  62.                     } catch (BrokenBarrierException e)  
  63.                     {  
  64.                         e.printStackTrace();  
  65.                     }  
  66.   
  67.                 };  
  68.   
  69.             }.start();  
  70.         }  
  71.   
  72.     }  
  73. }  

Output results:

  1. Student 1 has swiped the card and waits for the door to open and go home.~
  2. Student 3 has swiped the card and waits for the door to open and go home.~
  3. Student 5 has swiped the card and waits for the door to open and go home.~
  4. Student 9 has swiped the card and waits for the door to open and go home.~
  5. Student 7 has swiped the card and waits for the door to open and go home.~
  6. Student 0 has swiped the card and waits for the door to open and go home.~
  7. Student 2 has swiped the card and waits for the door to open and go home.~
  8. Student 6 has swiped the card and waits for the door to open and go home.~
  9. Student 8 has swiped the card and waits for the door to open and go home.~
  10. Student 4 has swiped the card and waits for the door to open and go home.~
  11. Everyone's here. Open the door.
  12. Student 4. Come home from school~
  13. Student 1. Come home from school~
  14. Student 3. Come home from school~
  15. Student 5. Come home from school~
  16. Student 9. Come home from school~
  17. Student 2. Come home from school~
  18. Student 6. Come home from school~
  19. Student 0. Come home from school~
  20. Student 7. Come home from school~
  21. Student 8. Come home from school~

Ha-ha, if any kindergarten uses such a system, children should not lose their school,,, joking.

Cyclic Barrier blocks all threads in one valve position and then opens the valve until the number of threads waiting reaches the preset value. Remember that it's blocking threads, not blocking operations. It's no use trying to await on the same thread.


The above example shows the basic usage of cyclic Barrier, but the function of cyclic is not shown. Now that the commentary says, we need to take an example to see:

After all, it is unrealistic to swipe the card. Now the demand is as follows: students are too dangerous to walk alone. Now the guard is guarding at the door after school, letting the students go in groups of three.

  1. package com.zhy.concurrency.cyclic;  
  2.   
  3. import java.util.concurrent.BrokenBarrierException;  
  4. import java.util.concurrent.CyclicBarrier;  
  5.   
  6. /** 
  7.  * Modified access control system 
  8.  *  
  9.  * @author zhy 
  10.  *  
  11.  */  
  12. public class CyclicBarrierTest2  
  13. {  
  14.   
  15.     /** 
  16.      * Total number of students 
  17.      */  
  18.     private final int STUDENT_COUNT = 12;  
  19.   
  20.     /** 
  21.      * Go out in groups of three 
  22.      */  
  23.     final CyclicBarrier barrier = new CyclicBarrier(3,  
  24.             new Runnable()  
  25.             {  
  26.                 @Override  
  27.                 public void run()  
  28.                 {  
  29.                     System.out.println("Three students arrived and were released.....");  
  30.                 }  
  31.             });  
  32.   
  33.     public void goHome() throws InterruptedException, BrokenBarrierException  
  34.     {  
  35.         System.out.println(Thread.currentThread().getName() + "I've swiped my card and waited for the door to open.~");  
  36.         barrier.await();  
  37.     }  
  38.   
  39.     public static void main(String[] args) throws InterruptedException,  
  40.             BrokenBarrierException  
  41.     {  
  42.   
  43.         final CyclicBarrierTest2 instance = new CyclicBarrierTest2();  
  44.   
  45.         /** 
  46.          * Each thread represents a student 
  47.          */  
  48.         for (int i = 0; i < instance.STUDENT_COUNT; i++)  
  49.         {  
  50.             new Thread("Student" + i +"  " )  
  51.             {  
  52.                 public void run()  
  53.                 {  
  54.   
  55.                     try  
  56.                     {  
  57.                         instance.goHome();  
  58.                     } catch (InterruptedException e)  
  59.                     {  
  60.                         e.printStackTrace();  
  61.                     } catch (BrokenBarrierException e)  
  62.                     {  
  63.                         e.printStackTrace();  
  64.                     }  
  65.   
  66.                 };  
  67.   
  68.             }.start();  
  69.         }  
  70.   
  71.     }  
  72. }  
Output results:

  1. Student 0 has swiped the card and waits for the door to open and go home.~
  2. Student 1 has swiped the card and waits for the door to open and go home.~
  3. Student 2 has swiped the card and waits for the door to open and go home.~
  4. Three students arrived and were released.
  5. Student 3 has swiped the card and waits for the door to open and go home.~
  6. Student 5 has swiped the card and waits for the door to open and go home.~
  7. Student 7 has swiped the card and waits for the door to open and go home.~
  8. Three students arrived and were released.
  9. Student 4 has swiped the card and waits for the door to open and go home.~
  10. Student 9 has swiped the card and waits for the door to open and go home.~
  11. Student 6 has swiped the card and waits for the door to open and go home.~
  12. Three students arrived and were released.
  13. Student 11 has swiped the card and waits for the door to open and go home.~
  14. Student 10 has swiped the card and waits for the door to open and go home.~
  15. Student 8 has swiped the card and waits for the door to open and go home.~
  16. Three students arrived and were released.

This example fully reflects the reusability of Cyclic Barrier, right, such a system may be more realistic, zero cost to haha.

Keywords: Java

Added by knight on Sat, 25 May 2019 22:23:02 +0300