Java并发编程 JUC中的锁

Posted 玉树临枫

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java并发编程 JUC中的锁相关的知识,希望对你有一定的参考价值。

 一 前言

  前面已经说到JUC中的锁主要是基于AQS实现,而AQS(AQS的内部结构 、AQS的设计与实现)在前面已经简单介绍过了。今天记录下JUC包下的锁是怎么基于AQS上实现的

二 同步锁

  同步锁不是JUC中的锁但也顺便提下,它是由synchronized 关键字进行同步,实现对竞争资源互斥访问的锁。

  同步锁的原理:对于每一个对象,有且仅有一个同步锁;不同的线程能共同访问该同步锁。在同一个时间点该同步锁能且只能被一个线程获取到,其他线程都得等待。

  另外:synchronized是Java中的关键字且是内置的语言实现;它是在JVM层面上实现的,不但可以通过一些监控工具监控synchronized的锁定,而且在代码执行时出现异常,JVM会自动释放锁定;synchronized等待的线程会一直等待下去,不能响应中断。

三 JUC中的锁结构

  相比同步锁,JUC包中的锁的功能更加强大,它为锁提供了一个框架,该框架允许更灵活地使用锁(它由自己实现、需要手动释放锁、能响应中断、可以多线程跑提高效率等)。下图是根据源码中查出画的类图,便知它提供的锁有好几种,下面一一分析。

四 可重入锁-ReentrantLock

  重入锁ReentrantLock,顾名思义:就是支持重进入的锁,它表示该锁能够支持一个线程对资源的重复加锁。另外该锁孩纸获取锁时的公平和非公平性选择,所以它包含公平锁与非公平锁(它们两也可以叫可重入锁)。首先提出两个疑问:它怎么实现重进入呢?释放逻辑还跟AQS中一样吗?

非公平锁

    final boolean nonfairTryAcquire(int acquires) {
        final Thread current = Thread.currentThread();
        int c = getState();
        if (c == 0) {
            if (compareAndSetState(0, acquires)) {
                setExclusiveOwnerThread(current);
                return true;
            }
        }
        // 同步状态已经被其他线程占用,则判断当前线程是否与被占用的线程是同一个线程,如果是同一个线程则允许获取,并state+1
        else if (current == getExclusiveOwnerThread()) {
            int nextc = c + acquires;
            if (nextc < 0) // overflow
                throw new Error("Maximum lock count exceeded");
            setState(nextc);
            return true;
        }
        return false;
    }

  该方法增加了再次获取同步状态的处理逻辑:通过判断当前线程是否为获取锁的线程来决定获取操作是否成功。如果是获取锁的线程再次请求,则将同步状态值进行增加并返回true,表示获取同步状态成功。

protected final boolean tryRelease(int releases) {
    int c = getState() - releases;
    if (Thread.currentThread() != getExclusiveOwnerThread())
        throw new IllegalMonitorStateException();
    boolean free = false;
    if (c == 0) {
        free = true;
        setExclusiveOwnerThread(null);
    }
    setState(c);
    return free;
}

  上面代码是释放锁的代码。如果该锁被获取了n次,那么前(n-1)次都是返回false,直至state=0,将占有线程设置为null,并返回true,表示释放成功。

公平锁

  公平锁与非公平锁有啥区别呢? 还是从源码中分析吧。

protected final boolean tryAcquire(int acquires) {
    final Thread current = Thread.currentThread();
    int c = getState();
    if (c == 0) {
        // 区别:增加判断同步队列中当前节点是否有前驱节点的判断
        if (!hasQueuedPredecessors() &&
                compareAndSetState(0, acquires)) {
            setExclusiveOwnerThread(current);
            return true;
        }
    }
    // 一样支持重入
    else if (current == getExclusiveOwnerThread()) {
        int nextc = c + acquires;
        if (nextc < 0)
            throw new Error("Maximum lock count exceeded");
        setState(nextc);
        return true;
    }
    return false;
}

  与非公平锁的唯一不同就是增加了一个判断条件:判断同步队列中当前节点是否有前驱节点的判断,如果方法返回true,则表示有线程比当前线程更早地请求获取锁,因此需要等待前驱线程获取并释放锁之后才能继续获取锁。

公平锁与非公平锁的区别

  从上面源码中得知,公平性锁保证了锁的获取按照FIFO原则,但是代价就是进行大量的线程切换。而非公平性锁,可能会造成线程“饥饿”(不会保证先进来的就会先获取),但是极少线程的切换,保证了更大的吞吐量。下面我们看下案例:

import org.junit.Test;

import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class FairAndUnfairTest {
    private static Lock fairLock = new ReentrantLock2(true);
    private static Lock unFairLock = new ReentrantLock2(false);

    @Test
    public void fair() throws Exception{
        testLock(fairLock);
    }

    @Test
    public void unFairLock() throws Exception{
        testLock(unFairLock);
    }

    private static void testLock(Lock lock) throws InterruptedException, ExecutionException {
        ExecutorService threadPool = Executors.newFixedThreadPool(5);
        List<Future<Long>> list = new ArrayList<>();
        for (int i = 0 ; i < 5; i++) {
            Future<Long> future = threadPool.submit(new Job(lock));
            list.add(future);
        }
        long cost = 0;
        for (Future<Long> future : list) {
            cost += future.get();
        }
        // 查看五个线程所需耗时的时间
        System.out.println("cost:" + cost + " ms");
    }

    private static class Job implements Callable<Long> {
        private Lock lock;
        public Job(Lock lock) {
            this.lock = lock;
        }
        @Override
        public Long call() throws Exception {
            long st = System.currentTimeMillis();
            // 同一线程获取100锁
            for (int i =0; i < 100; i ++) {
                lock.lock();
                try {
                    System.out.println("Lock by[" + Thread.currentThread().getId() + "]," +
                            "Waiting by[" + printThread(((ReentrantLock2)lock).getQueuedThreads()) + "]");
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
            // 返回100次所需的时间
            return System.currentTimeMillis() - st;
        }

        private String printThread(Collection<Thread> list) {
            StringBuilder ids = new StringBuilder();
            for (Thread t : list) {
                ids.append(t.getId()).append(",");
            }
            return ids.toString();
        }
    }

    private static class ReentrantLock2 extends ReentrantLock {
        public ReentrantLock2(boolean fair) {
            super(fair);
        }

        public Collection<Thread> getQueuedThreads() {
            List<Thread> arrayList = new ArrayList<>(super.getQueuedThreads());
            Collections.reverse(arrayList);
            return arrayList;
        }
    }
}

  非公平性锁的测试结果,cost:117 ms

Lock by[9],Waiting by[]
Lock by[9],Waiting by[]
Lock by[9],Waiting by[]
Lock by[9],Waiting by[]
Lock by[9],Waiting by[]
Lock by[9],Waiting by[]
Lock by[9],Waiting by[]
Lock by[9],Waiting by[]
Lock by[9],Waiting by[]
Lock by[9],Waiting by[]
Lock by[9],Waiting by[]
Lock by[9],Waiting by[]
Lock by[9],Waiting by[]
Lock by[9],Waiting by[]
Lock by[9],Waiting by[]
Lock by[9],Waiting by[]
Lock by[9],Waiting by[]
Lock by[9],Waiting by[]
Lock by[9],Waiting by[]
Lock by[9],Waiting by[]
Lock by[9],Waiting by[]
Lock by[9],Waiting by[]
Lock by[9],Waiting by[]
Lock by[9],Waiting by[]
Lock by[9],Waiting by[]
Lock by[9],Waiting by[]
Lock by[9],Waiting by[]
Lock by[9],Waiting by[]
Lock by[9],Waiting by[]
Lock by[10],Waiting by[]
Lock by[10],Waiting by[9,]
Lock by[10],Waiting by[9,]
Lock by[10],Waiting by[9,11,]
Lock by[10],Waiting by[9,11,]
Lock by[10],Waiting by[9,11,12,]
Lock by[10],Waiting by[9,11,12,]
Lock by[10],Waiting by[9,11,12,]
Lock by[10],Waiting by[9,11,12,]
Lock by[10],Waiting by[9,11,12,]
Lock by[10],Waiting by[9,11,12,]
Lock by[10],Waiting by[9,11,12,]
Lock by[10],Waiting by[9,11,12,]
Lock by[10],Waiting by[9,11,12,]
Lock by[10],Waiting by[9,11,12,]
Lock by[10],Waiting by[9,11,12,]
Lock by[10],Waiting by[9,11,12,]
Lock by[10],Waiting by[9,11,12,]
Lock by[10],Waiting by[9,11,12,]
Lock by[10],Waiting by[9,11,12,]
Lock by[10],Waiting by[9,11,12,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[10],Waiting by[9,11,12,13,]
Lock by[9],Waiting by[11,12,13,]
Lock by[9],Waiting by[11,12,13,]
Lock by[9],Waiting by[11,12,13,]
Lock by[9],Waiting by[11,12,13,]
Lock by[9],Waiting by[11,12,13,]
Lock by[9],Waiting by[11,12,13,]
Lock by[9],Waiting by[11,12,13,]
Lock by[9],Waiting by[11,12,13,]
Lock by[9],Waiting by[11,12,13,]
Lock by[9],Waiting by[11,12,13,]
Lock by[9],Waiting by[11,12,13,]
Lock by[9],Waiting by[11,12,13,]
Lock by[9],Waiting by[11,12,13,]
Lock by[9],Waiting by[11,12,13,]
Lock by[11],Waiting by[12,13,]
Lock by[11],Waiting by[12,13,]
Lock by[11],Waiting by[12,13,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[11],Waiting by[12,13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[12],Waiting by[13,9,]
Lock by[13],Waiting by[9,]
Lock by[13],Waiting by[9,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[13],Waiting by[9,12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[9],Waiting by[12,]
Lock by[12],Waiting by[]
Lock by[12],Waiting by[]
Lock by[12],Waiting by[]
Lock by[12],Waiting by[]
Lock by[12],Waiting by[]
Lock by[12],Waiting by[]
Lock by[12],Waiting by[]
Lock by[12],Waiting by[]
Lock by[12],Waiting by[]
Lock by[12],Waiting by[]
Lock by[12],Waiting by[]
Lock by[12],Waiting by[]
Lock by[12],Waiting by[]
Lock by[12],Waiting by[]
Lock by[12],Waiting by[]
Lock by[12],Waiting by[]
Lock by[12],Waiting by[]
Lock by[12],Waiting by[]
Lock by[12],Waiting by[]
Lock by[12],Waiting by[]
cost:117 ms
View Code

  公平性锁的测试结果,cost:193 ms

Lock by[14],Waiting by[]
Lock by[14],Waiting by[]
Lock by[14],Waiting by[]
Lock by[14],Waiting by[]
Lock by[14],Waiting by[]
Lock by[14],Waiting by[]
Lock by[14],Waiting by[]
Lock by[14],Waiting by[]
Lock by[14],Waiting by[]
Lock by[14],Waiting by[]
Lock by[14],Waiting by[]
Lock by[14],Waiting by[]
Lock by[14],Waiting by[]
Lock by[14],Waiting by[]
Lock by[14],Waiting by[]
Lock by[14],Waiting by[]
Lock by[14],Waiting by[]
Lock by[14],Waiting by[]
Lock by[14],Waiting by[]
Lock by[14],Waiting by[]
Lock by[14],Waiting by[]
Lock by[14],Waiting by[]
Lock by[14],Waiting by[]
Lock by[14],Waiting by[]
Lock by[14],Waiting by[]
Lock by[14],Waiting by[]
Lock by[14],Waiting by[]
Lock by[14],Waiting by[]
Lock by[14],Waiting by[]
Lock by[14],Waiting by[]
Lock by[14],Waiting by[]
Lock by[14],Waiting by[]
Lock by[14],Waiting by[]
Lock by[14],Waiting by[]
Lock by[14],Waiting by[]
Lock by[14],Waiting by[]
Lock by[14],Waiting by[]
Lock by[14],Waiting by[]
Lock by[14],Waiting by[]
Lock by[14],Waiting by[]
Lock by[14],Waiting by[]
Lock by[14],Waiting by[]
Lock by[14],Waiting by[]
Lock by[14],Waiting by[]
Lock by[14],Waiting by[]
Lock by[14],Waiting by[]
Lock by[15],Waiting by[]
Lock by[14],Waiting by[15,]
Lock by[15],Waiting by[14,]
Lock by[14],Waiting by[15,]
Lock by[15],Waiting by[14,]
Lock by[14],Waiting by[15,16,]
Lock by[15],Waiting by[16,14,]
Lock by[16],Waiting by[14,15,]
Lock by[14],Waiting by[15,16,]
Lock by[15],Waiting by[16,14,]
Lock by[16],Waiting by[14,15,]
Lock by[14],Waiting by[15,16,]
Lock by[15],Waiting by[16,14,]
Lock by[16],Waiting by[14,15,]
Lock by[14],Waiting by[15,16,]
Lock by[15],Waiting by[16,14,]
Lock by[16],Waiting by[14,15,]
Lock by[14],Waiting by[15,16,]
Lock by[15],Waiting by[16,14,]
Lock by[16],Waiting by[14,15,]
Lock by[14],Waiting by[15,16,]
Lock by[15],Waiting by[16,14,]
Lock by[16],Waiting by[14,15,]
Lock by[14],Waiting by[15,16,]
Lock by[15],Waiting by[16,14,]
Lock by[16],Waiting by[14,15,]
Lock by[14],Waiting by[15,16,]
Lock by[15],Waiting by[16,14,]
Lock by[16],Waiting by[14,]
Lock by[14],Waiting by[15,16,]
Lock by[15],Waiting by[16,14,]
Lock by[16],Waiting by[14,15,]
Lock by[14],Waiting by[15,16,]
Lock by[15],Waiting by[16,14,]
Lock by[16],Waiting by[14,15,]
Lock by[14],Waiting by[15,16,]
Lock by[15],Waiting by[16,14,]
Lock by[16],Waiting by[14,15,]
Lock by[14],Waiting by[15,16,]
Lock by[15],Waiting by[16,14,]
Lock by[16],Waiting by[14,15,]
Lock by[14],Waiting by[15,16,]
Lock by[15],Waiting by[16,17,14,]
Lock by[16],Waiting by[17,14,15,]
Lock by[17],Waiting by[14,15,16,]
Lock by[14],Waiting by[15,16,17,]
Lock by[15],Waiting by[16,17,14,]
Lock by[16],Waiting by[17,14,15,]
Lock by[17],Waiting by[14,15,16,]
Lock by[14],Waiting by[15,16,17,]
Lock by[15],Waiting by[16,17,14,]
Lock by[16],Waiting by[17,14,15,]
Lock by[17],Waiting by[14,15,16,]
Lock by[14],Waiting by[15,16,17,]
Lock by[15],Waiting by[16,17,14,]
Lock by[16],Waiting by[17,14,15,]
Lock by[17],Waiting by[14,15,16,]
Lock by[14],Waiting by[15,16,17,]
Lock by[15],Waiting by[16,17,14,]
Lock by[16],Waiting by[17,14,15,]
Lock by[17],Waiting by[14,15,16,]
Lock by[14],Waiting by[15,16,17,]
Lock by[15],Waiting by[16,17,14,]
Lock by[16],Waiting by[17,14,15,]
Lock by[17],Waiting by[14,15,16,]
Lock by[14],Waiting by[15,16,17,]
Lock by[15],Waiting by[16,17,14,]
Lock by[16],Waiting by[17,14,15,]
Lock by[17],Waiting by[14,15,16,]
Lock by[14],Waiting by[15,16,]
Lock by[15],Waiting by[16,17,14,]
Lock by[16],Waiting

以上是关于Java并发编程 JUC中的锁的主要内容,如果未能解决你的问题,请参考以下文章

Java并发编程系列之三JUC概述

Java并发编程系列之三JUC概述

Java - “JUC”锁

Java并发编程系列-locks锁机制类

JUC并发编程 共享模式之工具 JUC CountdownLatch(倒计时锁) -- CountdownLatch应用(等待多个线程准备完毕( 可以覆盖上次的打印内)等待多个远程调用结束)(代码片段

高效并发JUC锁-永恒砖石