Concurrent tools CountDownLatch, CyclicBarrier (synchronization barrier), Semaphore (control the number of concurrent threads), Exchanger (thread exchange data)

CountDownLatch

Introduction Principle

  CountDownLatch is implemented by the queue synchronizer.

  When constructing a new CountDownLatch object, you need to pass an integer int parameter greater than 0. This parameter is used as the state synchronization state of the queue synchronizer.

    public CountDownLatch(int count) {
        if (count < 0) throw new IllegalArgumentException("count < 0");
        this.sync = new Sync(count);
    }
        Sync(int count) {
            setState(count);
        }

  When a thread in the implementation of the CountDownLatch  . The await ()  after the method will block spin determine whether the state is 0. The state is reduced by CountDownLatch's  .countDown ()  method.

    public void countDown() {
        sync.releaseShared(1);
    }

Usage scenario

  For example, if multiple threads are required to calculate multiple sheets, and finally the results need to be summarized, you can execute await () on the main thread, and execute countDown () after the calculation of each sheet.

CyclicBarrier

Introduction Principle

  There are two construction methods for the CyclicBarrier synchronization barrier.

  In the first kind, the parameters only pass integer numbers.

    public CyclicBarrier(int parties) {
        this(parties, null);
    }

  The second kind, the parameter also needs to pass a thread to achieve

    public CyclicBarrier(int parties, Runnable barrierAction) {
        if (parties <= 0) throw new IllegalArgumentException();
        this.parties = parties;
        this.count = parties;
        this.barrierCommand = barrierAction;
    }

  CyclicBarrier by calling it  . The await ()  , the current thread locking method, its internal member variables reentrant lock.

  The first:

  1. Suppose the parameter is passed 2, the thread executes the await () method, thread 1 first grabs the lock, and then decrements the party parameter by 1, and then determines whether it is 0. If it is 0, the logic behind the thread is run.

  2. If it is not 0, then call Condition's trip () method, trip () method will release the lock and wait. At this time, thread 2 can grab the lock and then execute the logic in step 1. At this time, minus 1 for the parties is 0. At this time , the signalAll () method of Condition will be called to notify the thread of the trip () method to continue execution

  The second kind:

  The parameter not only passed 2 but also passed in another thread (alias fetched C). In fact, the general logic is similar to the first one, except that after the party 1 is judged to be 0 in step 1, it will first execute the logic of thread C, and then wake up the other two threads.

Usage scenario

Semaphore

Introduction Principle

  When constructing Semaphore, the shaping parameter is required, indicating how many threads it allows to run in parallel.

    public Semaphore(int permits) {
        sync = new NonfairSync(permits);
    }

  Need to execute semaphore.acquire () in the thread to   get the license, then the synchronization status will respond to the change, and then execute  sync.releaseShared ( 1 ) to  release the license, other threads can obtain the license

Usage scenario

  Various scenarios that require limiting the number of threads. For example, in connection pool scenarios, the number of threads needs to be limited.

Exchanger

Usage scenario

  Exchange data between threads

Guess you like

Origin www.cnblogs.com/lcmlyj/p/12751008.html