java- concurrent Tools

CountDownLatch

  • CountDownLatch java.util.concurrent package is one kind of concurrent class tool, which allows one or more threads wait until a complete set of operations performed in other threads.
  • You can control thread execution order

Example: the school playground on the runway of eight players first 1000 meter race, finish referee empty runway hurdles.

result:

to sum up

  • CountDownLatch end = new CountDownLatch (N); // need to pass the object is constructed when the parameter N
  • end.await () can block the thread until the call N times end.countDown () method before the release of the thread
  • end.countDown () calls the number of calculations can be called from multiple threads is the sum of the number of calls all threads

 CyclicBarrier- fence

  • Fence similar to the lockout, it blocked a set of threads until the occurrence of an event. The key difference between the fence and the lockout is that all threads must arrive at the same location of the fence, in order to continue. Lockout wait for the event, and the fence waiting for the other thread.
  • CyclicBarrier can make a certain number of threads repeatedly brought together at the fence position. When invoked await a thread reaches the barrier position, this method will block until all threads have reached the barrier position. If all the threads have reached the position of the fence, the fence will then open, then all the threads will be released, and the fence will be reset for the next use.

CyclicBarrier the difference with countDownLatch

  • After CountDownLatch generally used for a thread to wait several other threads executing the task, it was executed; can not be reused
  • CyclicBarrier generally used for a group of threads wait for each other to a certain state, and then execute that group of threads simultaneously; reusable

Example: the school playground on the runway of eight players for the 1000 meter race, only if eight players to the starting line to compete

 result:

   Another Constructor:

 Reaches a certain state, the execution run Runnable ().

Semaphore-- semaphore

  • Control the number of concurrent
  • Commonly used to limit the number of threads that can access certain resources, such as by limiting Semaphore
  • Semaphore is synchronized in the enhanced version, it is to control the number of concurrent threads. In this regard, a simple synchronized keyword is not implemented

Constructor

   to achieve fair representation fair and unfair achieve

 result:

 Since the amount of the control signal setting number 5, so that when the code is executed to begin after hibernation Thread-3 for 5 seconds, go perform other threads.

 Exchanger

  • For exchanging data
  • It provides a synchronization point, in this synchronization point two threads may exchange data with each other. These two threads to exchange data exchange method, if the first thread to execute exchange method, it would have been to wait for a second thread also performs exchange, when two threads have reached the synchronization point, these two threads can exchange data , this thread will be produced by the data is passed to the other side. So the focus using Exchanger is paired threads exchange () method, when a pair of thread synchronization point reached, it will exchange data. Therefore thread object class is [the tool] in pairs.

 result:

Source address: https: //github.com/woxbwo/is-concurrent

 

Guess you like

Origin www.cnblogs.com/woxbwo/p/11462732.html