Continue concurrent topics~
This time, Cyclic Barrier: Take a look at the API annotations:
-
/**
-
* 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
-
* <em>cyclic</em> because it can be re-used after the waiting threads
-
* 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.~~
-
package com.zhy.concurrency.cyclic;
-
-
import java.util.concurrent.BrokenBarrierException;
-
import java.util.concurrent.CyclicBarrier;
-
-
-
-
-
-
-
-
public class CyclicBarrierTest
-
{
-
-
-
-
-
private final int STUDENT_COUNT = 10;
-
-
-
-
-
final CyclicBarrier barrier = new CyclicBarrier(STUDENT_COUNT,
-
new Runnable()
-
{
-
@Override
-
public void run()
-
{
-
System.out.println("Everybody's here. Open the door.....");
-
}
-
});
-
-
public void goHome() throws InterruptedException, BrokenBarrierException
-
{
-
System.out.println(Thread.currentThread().getName() + "I've swiped my card and waited for the door to open.~");
-
barrier.await();
-
System.out.println(Thread.currentThread().getName() + "Come home from school~");
-
}
-
-
public static void main(String[] args) throws InterruptedException,
-
BrokenBarrierException
-
{
-
-
final CyclicBarrierTest instance = new CyclicBarrierTest();
-
-
-
-
-
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 results:
-
Student 1 has swiped the card and waits for the door to open and go home.~
-
Student 3 has swiped the card and waits for the door to open and go home.~
-
Student 5 has swiped the card and waits for the door to open and go home.~
-
Student 9 has swiped the card and waits for the door to open and go home.~
-
Student 7 has swiped the card and waits for the door to open and go home.~
-
Student 0 has swiped the card and waits for the door to open and go home.~
-
Student 2 has swiped the card and waits for the door to open and go home.~
-
Student 6 has swiped the card and waits for the door to open and go home.~
-
Student 8 has swiped the card and waits for the door to open and go home.~
-
Student 4 has swiped the card and waits for the door to open and go home.~
-
Everyone's here. Open the door.
-
Student 4. Come home from school~
-
Student 1. Come home from school~
-
Student 3. Come home from school~
-
Student 5. Come home from school~
-
Student 9. Come home from school~
-
Student 2. Come home from school~
-
Student 6. Come home from school~
-
Student 0. Come home from school~
-
Student 7. Come home from school~
-
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.
-
package com.zhy.concurrency.cyclic;
-
-
import java.util.concurrent.BrokenBarrierException;
-
import java.util.concurrent.CyclicBarrier;
-
-
-
-
-
-
-
-
public class CyclicBarrierTest2
-
{
-
-
-
-
-
private final int STUDENT_COUNT = 12;
-
-
-
-
-
final CyclicBarrier barrier = new CyclicBarrier(3,
-
new Runnable()
-
{
-
@Override
-
public void run()
-
{
-
System.out.println("Three students arrived and were released.....");
-
}
-
});
-
-
public void goHome() throws InterruptedException, BrokenBarrierException
-
{
-
System.out.println(Thread.currentThread().getName() + "I've swiped my card and waited for the door to open.~");
-
barrier.await();
-
}
-
-
public static void main(String[] args) throws InterruptedException,
-
BrokenBarrierException
-
{
-
-
final CyclicBarrierTest2 instance = new CyclicBarrierTest2();
-
-
-
-
-
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 results:
-
Student 0 has swiped the card and waits for the door to open and go home.~
-
Student 1 has swiped the card and waits for the door to open and go home.~
-
Student 2 has swiped the card and waits for the door to open and go home.~
-
Three students arrived and were released.
-
Student 3 has swiped the card and waits for the door to open and go home.~
-
Student 5 has swiped the card and waits for the door to open and go home.~
-
Student 7 has swiped the card and waits for the door to open and go home.~
-
Three students arrived and were released.
-
Student 4 has swiped the card and waits for the door to open and go home.~
-
Student 9 has swiped the card and waits for the door to open and go home.~
-
Student 6 has swiped the card and waits for the door to open and go home.~
-
Three students arrived and were released.
-
Student 11 has swiped the card and waits for the door to open and go home.~
-
Student 10 has swiped the card and waits for the door to open and go home.~
-
Student 8 has swiped the card and waits for the door to open and go home.~
-
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.