An overview of the

This paper mainly summarizes the principle of AQS and the application scenarios of AQS.

What is AQS?

Is the full name of AQS AbstractQueuedSynchronizer abbreviations, he is the essence of a two-way synchronous chain table structure.

  • Usage scenariosReentrantLockCountDownLatch,Semaphore

Already use the 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(a) {
        lock.lock();
        doSomething();
        lock.unlock();
    }

    private static void doSomething(a) {
        try {
            TimeUnit.SECONDS.sleep(20);
        } catch(InterruptedException e) { e.printStackTrace(); }}}Copy the code

AQS processing process