1. CountDownLatch

1.1 Application Scenarios

When multiple threads need to execute, CountDownLatch can be used if one or more of the threads need to wait for other threads to complete certain operations.

1.2 routine

public class CountDownLatchTest {

	static CountDownLatch c = new CountDownLatch(2);

	public static void main(String[] args) throws InterruptedException {
		new Thread(new Runnable() {
			@Override
			public void run() {
				System.out.println(1);
				c.countDown();
				System.out.println(2);
				c.countDown();
			}
		}).start();

		c.await();
		System.out.println("3"); }}Copy the code

1.3 summary

  • ●CountDownLatch’s counter can only be used once.
  • The CountDownLatch constructor sets an int value as a counter that is reduced by one each time CountDownLatch’s countDown() method is called, waking up the waiting thread when the counter is zero.
  • ● Calling await() method with calculator 0 does not wait to block the current thread.

CyclicBarrier

2.1 Application Scenarios

The barrier will be blocked when multiple threads reach it, and only when the last thread of these threads reaches it will the barrier open, and all threads blocked by the barrier can continue to execute, and the barrier is reusable.

2.2 routine

public class CyclicBarrierTest {

	static CyclicBarrier c = new CyclicBarrier(2);

	public static void main(String[] args) {
		new Thread(new Runnable() {

			@Override
			public void run() { try { c.await(); } catch (Exception e) { } System.out.println(1); } }).start(); try { c.await(); } catch (Exception e) { } System.out.println(2); }}Copy the code

2.3 summary

  • ●CyclicBarrier constructors can set an int as the number of threads blocked. Each call to await() of CyclicBarrier blocks the current thread with an int value minus. A 0 value means all threads are blocked to reach the barrier, at which point all blocked threads are “released”.
  • ●CyclicBarrier has a more advanced constructor CyclicBarrier(int parties,Runnable barrierAction), which means that the number of parties threads will give priority to the barrierAction thread when it reaches the barrier.
  • ●CyclicBarrier counters can be reset using the reset() method.

3. Semaphore: Semaphore

3.1 Application Scenarios

When there are m threads and n resources (m > n and each thread occupies one resource), Semaphore can be used to control the number of threads accessing resources at the same time to ensure proper use of resources.

3.2 routine

public class SemaphoreTest {

    Semaphore semaphore = new Semaphore(3);
    
    public static void main(String[] args) {
        for ( int i=0; i<10; i++ ) {
            new Thread( new Runnbale(){
                public void run(){ semaphore.acquire(); Semaphore.release (); } } ).start(); }}}Copy the code

3.3 summary

  • ●Semaphore’s constructor accepts an int as the number of permits (that is, the number of threads allowed to execute).
  • ●Semaphore acquire() means acquire a license. Threads that fail to acquire a license cannot execute.
  • ●Semaphore’s release() method represents the release license.