Catalog
Concurrent Code Thread Security Promotion
CountDownLatch fence
The concept of CountDownLatch:
CountDownLatch is a synchronization tool class that coordinates synchronization between multiple threads, or acts as communication between threads (rather than as mutually exclusive).
CountDownLatch enables one thread to wait for other threads to finish their work before continuing.Use a counter for implementation.The initial value of the counter is the number of threads.When each thread finishes its own task, the counter is subtracted by one.A counter value of 0 indicates that all threads have completed the task, and threads waiting on CountDownLatch can resume execution.The detailed use of CountDownLatch is explained in depth later.
Semaphore semaphore
Semaphore concept
Semaphore is a count semaphore.Semaphore manages a range of licenses.Each acquire method is blocked until one license is available and one license is removed; each release method adds a license, which may release a blocked acquire method.However, there is no actual license for this object, and Semaphore maintains only a number of licenses available.
Concurrent Code Demo
The code below uses both a fence and a semaphore, which is enough for many simple tests.
@Slf4j @NotThreadSafe public class ConcurrencyTest { // Total number of requests public static int clientTotal = 5000; // Number of threads executed concurrently public static int threadTotal = 200; public static int count = 0; public static void main(String[] args) throws Exception { ExecutorService executorService = Executors.newCachedThreadPool(); final Semaphore semaphore = new Semaphore(threadTotal); final CountDownLatch countDownLatch = new CountDownLatch(clientTotal); for (int i = 0; i < clientTotal ; i++) { executorService.execute(() -> { try { semaphore.acquire(); add(); semaphore.release(); } catch (Exception e) { log.error("exception", e); } countDownLatch.countDown(); }); } countDownLatch.await(); executorService.shutdown(); log.info("count:{}", count); } private static void add() { count++; } }
Concurrent Code Thread Security Promotion
From thread insecurity to thread safety, use fence only:
@Slf4j public class ConcurencyTest { // Total number of requests public static int clientTotal = 5000; // Atomic Lock public static AtomicInteger count = new AtomicInteger(0); public static void main(String[] args) throws Exception { ExecutorService executorService = Executors.newCachedThreadPool(); final CountDownLatch countDownLatch = new CountDownLatch(clientTotal); for (int i = 0; i < clientTotal ; i++) { executorService.execute(() -> { countDownLatch.countDown(); try { countDownLatch.await(); add(); } catch (Exception e) { e.printStackTrace(); } }); } try{ //Until method execution is complete Thread.sleep(10000); executorService.shutdown(); System.out.println(count); }catch(InterruptedException e){ e.printStackTrace(); } } private static void add() { count.incrementAndGet(); } }
Or write this:
public class ConcurencyTest { // Total number of requests public static int clientTotal = 5000; // Atomic Lock public static AtomicInteger count = new AtomicInteger(0); public static void main(String[] args) throws Exception { CountDownLatch countDownLatch = new CountDownLatch(clientTotal); for (int i = 0; i < clientTotal ; i++) { new Thread(()->{ countDownLatch.countDown(); try { countDownLatch.await(); add(); } catch (Exception e) { e.printStackTrace(); } }).start(); } try { Thread.sleep(10000); System.out.println(count); }catch (InterruptedException e){ e.printStackTrace(); } } private static void add() { count.incrementAndGet(); } }