Preface:

JUC is also short for java.util.concurrent, and basically all the Java classes that involve multithreading are in this package. Most of the components under JUC package are based on the AQS base class. Today, we will briefly talk about the three categories under JUC: CountDownLatch, CyclicBarrier and Semaphore

CountDownLatch

The CountDownLatch constructor specifies the size of the timer, as shown in the following code.

CountDownLatch countDownLatch=new CountDownLatch(3); for (int i=0; i<3; i++){ int finalI = i; New Thread(()->{system.out.println (" + finalI +"); countDownLatch.countDown(); }).start(); } try { countDownLatch.await(); System.out.println(" three students are ready for class "); } catch (InterruptedException e) { e.printStackTrace(); }Copy the code

Running results:

Each call to the countDown method decrement the counter by one, and the code behind await is executed when the counter is zero.

CyclicBarrier

A CyclicBarrier, which is a positive counter, can also specify the size of the counter through its constructor. A CyclicBarrier can also pass in a thread that executes when the counter reaches a specified threshold, and a CyclicBarrier is reusable.

CyclicBarrier CyclicBarrier = new CyclicBarrier(7, new Thread(()-> system.out.println))); CyclicBarrier = new CyclicBarrier(7, new Thread(()-> system.out.println)); for (int i=1; i<8; i++){ int finalI=i; New Thread(()->{system.out.println (" Thread "+finalI+" Thread "); try { cyclicBarrier.await(); System.out.println("^^"); } catch (InterruptedException e) { e.printStackTrace(); } catch (BrokenBarrierException e) { e.printStackTrace(); } }).start(); }Copy the code

Running results:

As you can see from the run results, when the counter reaches the threshold, the thread in the constructor executes first, and then the code after multiple threads execute the await() method together.

Difference between CountDownLatch and CyclicBarrier

  • CountDownLatch is a reverse count and CyclicBarrier is a positive count
  • CountDownLatch is not reusable, CyclicBarrier is reusable
  • The CyclicBarrier constructor can also pass in a thread
  • A CountDownLatch is a thread that executes downward when multiple threads reach a critical point, and a CyclicBarrier is a thread that executes downward when multiple threads reach a critical point

Semaphore

Semaphore is similar to a signal. Traditionally, only one thread is allowed to access our code at a time by adding a lock. Semaphore can pass in a value that represents the maximum number of threads allowed to access our code. This means that using Semaphore to control blocks of code allows multiple threads to access them.

The code is as follows:

Semaphore semaphore=new Semaphore(3); System.out.println(" 3 Spaces "); system.out.println (" 3 Spaces "); for (int i=0; i<6; i++){ int finalI=i; new Thread(()->{ try { semaphore.acquire(); System.out.println(finalI+" grab a parking space "); TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }finally {system.out.println (finalI+" "); semaphore.release(); // Release resources}}).start(); }}Copy the code

Running results:

Semaphore can also be used for limiting traffic.

conclusion

I hope each of us can ride the wind and waves, more and more excellent bar, with you.