Concurrent Art Java programming and (11) - the countdown, the barrier synchronization, semaphore

1. countdown: CountDownLatch

1.1 usage scenarios

When multiple threads need to be performed, after which, if one or more threads need to wait for the other thread to complete some operations you can perform, you can use CountDownLatch realize the function.

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");
	}

}
复制代码

1.3 Summary

  • ● CountDownLatch counter can only be used once.
  • ● CountDownLatch constructor int value may be set as a counter, a countDown CountDownLatch each call () method decrements the calculator, the calculator when the thread is waiting for the wake 0:00.
  • ● Calculator is 0, calling await () method does not wait blocks the current thread.

2. Synchronization Barrier: CyclicBarrier

2.1 usage scenarios

When multiple threads to reach the barrier will be blocked only after the last thread of these threads have reached the barrier, the barrier will open the door, all barriers blocked thread can proceed, 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);
	}
}
复制代码

2.3 Summary

  • ● CyclicBarrier constructor may be provided as a number of threads intercept int value, each call await CyclicBarrier () method of the int value Save collectively block the current thread, when the value of 0 indicates that all threads have reached the barrier, at this time need to "release" all intercepted thread.
  • ● CyclicBarrier have a more advanced constructor CyclicBarrier (int parties, Runnable barrierAction), the number of parties represented thread priority thread execution barrierAction arrival barrier.
  • ● CyclicBarrier available counter reset () method to reset.

3. Semaphore: Semaphore

3.1 usage scenarios

When there are m threads, when n resource (m> n and each thread occupies one resource), Semaphore used to control the number of threads simultaneously access a resource, 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();
        }
    }
}
复制代码

3.3 Summary

  • ● Semaphore int constructor takes a value as the number of licenses (i.e., allowing the number of threads of execution).
  • ● Semaphore's acquire () method represents a license, failure to obtain a license threads will not be executed.
  • ● Semaphore's release () method shows the release license.

Guess you like

Origin juejin.im/post/5d1378a0f265da1bc552740a