Examples of locking in concurrent Java programming

  • 2020-04-01 03:49:21
  • OfStack

The lock is equivalent to a door, which remains closed until the lock reaches the end state and no threads can pass through. When the end state is reached, the door opens and allows all threads to pass through. It can cause one or more threads to wait for a set of events to occur. The latched state includes a counter, initialized to a formal number, with a positive number indicating the number of events to wait for. The countDown method decrements the counter to indicate that an event has occurred, and the await method waits for the counter to reach 0 to indicate that the waiting event has occurred. CountDownLatch emphasizes that one thread (or threads) must wait for n other threads to finish something before it can continue.

Scenario application:
Ten runners are ready to run. They wait for the judge to tell them to run at the same time. When the last runner crosses the finish line, the race is over. 10 motions is the same as 10 threads. The key here is to control 10 threads to run at the same time, and how to judge the last thread to reach the end. Two locks can be used, the first lock is used to control 10 threads waiting for the referee's order, the second block to control the end of the game.


import java.util.concurrent.CountDownLatch;
 
class Aworker implements Runnable {
 private int num;
 private CountDownLatch begin;
 private CountDownLatch end;
 
 public Aworker(int num, final CountDownLatch begin, final CountDownLatch end) {
 this.num = num;
 this.begin = begin;
 this.end = end;
 }
 
 @Override
 public void run() {
 // TODO Auto-generated method stub
 try {
  System.out.println(num + "th people is ready");
  begin.await();  //ready
 } catch (InterruptedException e) {
  e.printStackTrace();
 } finally {
  end.countDown();  //When the counter is reduced by one, the terminal is reached
  System.out.println(num + "th people arrive");
 }
 }
}
 
public class Race {
 public static void main(String[] args) {
 int num = 10;
 CountDownLatch begin = new CountDownLatch(1);
 CountDownLatch end = new CountDownLatch(num);
 
 for (int i = 1; i <= num; i++) {
  new Thread(new Aworker(i, begin, end)).start();
 }
 
 try {
  Thread.sleep((long) (Math.random() * 5000));
 } catch (InterruptedException e1) {
  // TODO Auto-generated catch block
  e1.printStackTrace();
 } 
 System.out.println("judge say : run !");
 begin.countDown(); //The referee gave the order to start running
 long startTime = System.nanoTime();
 try {
  end.await(); //Waiting for the end
 } catch (InterruptedException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 } finally {
  long endTime = System.nanoTime();
  System.out.println("judge say : all arrived !");
  System.out.println("spend time: " + (endTime - startTime));
 }
 }
}

The output


1th people is ready
2th people is ready
4th people is ready
6th people is ready
3th people is ready
10th people is ready
8th people is ready
5th people is ready
7th people is ready
9th people is ready
judge say : run !
1th people arrive
4th people arrive
10th people arrive
5th people arrive
2th people arrive
judge say : all arrived !
9th people arrive
7th people arrive
8th people arrive
3th people arrive
6th people arrive
spend time: 970933

Related articles: