AQS 處理流程和使用

概述

本文主要是總結 AQS 的原理,以及 AQS 的使用場景。ide

什麼是 AQS?

AQS 的全稱是 AbstractQueuedSynchronizer 的縮寫, 他的本質是一個雙向同步鏈表結構。code

  • 使用場景 ReentrantLockCountDownLatchSemaphore

ReentrantLock 使用 Demo

public class ReentrantLockTest {     ReentrantLock lock = new ReentrantLock();     public static void main(String[] args) {         ReentrantLockTest test = new ReentrantLockTest();         ExecutorService executorService = Executors.newFixedThreadPool(10);         for (int i = 0; i < 3; i++) {             executorService.submit(test::serviceMethod);         }         executorService.shutdown();         while (true) {             if (executorService.isTerminated()) {                 break;             }         }     }     private void serviceMethod() {         lock.lock();         doSomething();         lock.unlock();     }     private static void doSomething() {         try {             TimeUnit.SECONDS.sleep(20);         } catch (InterruptedException e) {             e.printStackTrace();         }     } } 複製代碼

AQS 的處理流程

相關文章
相關標籤/搜索