Just write something yourself 3

Sometimes you want to test the function of a concurrent capacity, without the aid of others in order to test tools, simply write your own simple demo simulate a concurrent requests on the most convenient. If you are familiar jemter test an interface of concurrency in fact more professional, here is his own toss play.

CountDownLatch and CyclicBarrier jdk concurrent packet is two concurrent useful tools, they provide a means of controlling concurrent processes. In fact, they are the source view maintains a counter inside the control flow

  • CountDownLatch: One or more threads wait for the completion of a number of other threads to execute after something;
  • CyclicBarrier: multiple threads wait for each other to reach the same synchronization point, and then continue with.   

CountDownLatch and the difference CyclicBarrier

  • CountDownLatch counter, a thread to complete a record, the counter is a down counter, can only be used once
  • CyclicBarrier counter is more like a valve, the need to reach all threads, the valve to open, and then continue, the counter is incremented counter provides reset function can be used multiple times

   In addition Semaphore can control the number of threads access through acquire obtain a license (), if not wait, and release () Releases a permit.

 

  Usually we simulate concurrent requests, are generally more open several threads, initiated the request just fine. However, the general there will be the order of the boot, this is not true concurrent! How can you be truly concurrent it? This article is trying to say point, java provides a lockout CountDownLatch, CyclicBarrier used to do such a thing to happen on the most appropriate.

  We were used to simulate the following CountDownLatch and concurrent requests CyclicBarrier

Guess you like

Origin www.cnblogs.com/xxj0316/p/11646482.html