synchronized和reentrantlock到底锁了什么?

做~自己de王妃 提交于 2019-11-28 22:56:04

第一个类,在add clear等方法里使用可重入锁 

public class LockList extends ArrayList<Object>{

    private Lock lock = new ReentrantLock(false);

    @Override
    public boolean add(Object o) {
        lock.lock();
        try{
            System.out.println(Thread.currentThread().getName()+"add lock");
            return super.add(o);
        }finally {
            System.out.println(Thread.currentThread().getName()+"add unlock");
            lock.unlock();
        }
    }

    @Override
    public void clear() {
        lock.lock();
        try{
            System.out.println(Thread.currentThread().getName()+"clear lock");
            super.clear();
        }finally {
            System.out.println(Thread.currentThread().getName()+"clear unlock");
            lock.unlock();
        }
    }

    @Override
    public int size() {
        lock.lock();
        try{
            System.out.println(Thread.currentThread().getName()+"clear lock");
            return super.size();
        }finally {
            System.out.println(Thread.currentThread().getName()+"clear unlock");
            lock.unlock();
        }
    }


    public static void main(String[] args) {

        final LockList list = new LockList();

        for(int i=0;i<110;i++){
            final int j = i;
            new Thread(){
                @Override
                public void run() {
                    setName("线程"+j);
                    list.add(j);
                    list.clear();
                }
            }.start();
        }

    }
}

第二个类,在add remove方法里使用synchronized关键字

public class SyncList extends ArrayList<Object> {

    @Override
    public boolean add(Object o) {
        synchronized (this){
            System.out.println(Thread.currentThread().getName() +"add synchronized lock");
            boolean r=  super.add(o);
            System.out.println(Thread.currentThread().getName() +"add synchronized unlock");
            return r;
        }
    }

    @Override
    public void clear() {
        synchronized (this){
            System.out.println(Thread.currentThread().getName() +"clear synchronized lock");
            super.clear();
            System.out.println(Thread.currentThread().getName() +"clear synchronized unlock");
        }
    }

    @Override
    public int size() {
        synchronized (this){
            System.out.println(Thread.currentThread().getName() +"size synchronized lock");
            int r= super.size();
            System.out.println(Thread.currentThread().getName() +"size synchronized unlock");
            return r;
        }
    }

    @Override
    public boolean addAll(Collection<?> c) {
        synchronized (this) {
            System.out.println(Thread.currentThread().getName() +"addAll synchronized lock");
            boolean r= super.addAll(c);
            System.out.println(Thread.currentThread().getName() +"addAll synchronized unlock");
            return r;
        }
    }

    @Override
    public boolean remove(Object o) {
        synchronized (this){
            System.out.println(Thread.currentThread().getName() +"remove synchronized lock");
            boolean r= super.remove(o);
            System.out.println(Thread.currentThread().getName() +"remove synchronized unlock");
            return r;
        }
    }

    public static void main(String[] args) {
        final SyncList list = new SyncList();

        for(int i=0;i<110;i++){
            final int j = i;
            new Thread(){
                @Override
                public void run() {
                    setName("线程"+j);
//                    list.add(j);
                    list.remove(new Integer(j));
                }
            }.start();
        }
    }
}

 

第一个结果,可以看到lock和unlock都是成在一个同一个线程里对出现的,说明了调用该方法时另外的线程是进不来的,或者说进入等待队列,这个要看AbstractQueueSynchronized的源码,他对锁进行了优化。

线程2add lock
线程2add unlock
线程2clear lock
线程2clear unlock
线程3add lock
线程3add unlock
线程3clear lock
线程3clear unlock
线程5add lock
线程5add unlock
线程5clear lock
线程5clear unlock
线程8add lock
线程8add unlock
线程8clear lock
线程8clear unlock
线程15add lock
线程15add unlock
线程23add lock
线程23add unlock
线程23clear lock
线程23clear unlock
线程15clear lock
线程15clear unlock
线程26add lock
线程26add unlock
线程26clear lock
线程26clear unlock
线程20add lock
线程20add unlock
线程20clear lock
线程20clear unlock
线程32add lock
线程32add unlock
线程32clear lock
线程32clear unlock
线程9add lock
线程9add unlock
线程9clear lock
线程9clear unlock
线程35add lock
线程35add unlock
线程35clear lock
线程35clear unlock
线程14add lock
线程14add unlock
线程14clear lock
线程14clear unlock
线程45add lock
线程45add unlock
线程45clear lock
线程45clear unlock
线程21add lock
线程21add unlock
线程21clear lock
线程21clear unlock
线程27add lock
线程27add unlock
线程27clear lock
线程27clear unlock
线程62add lock
线程62add unlock
线程68add lock
线程68add unlock
线程68clear lock
线程68clear unlock
线程1add lock
线程1add unlock
线程1clear lock
线程1clear unlock
线程33add lock
线程33add unlock
线程33clear lock
线程33clear unlock
线程6add lock
线程6add unlock
线程39add lock
线程39add unlock
线程39clear lock
线程39clear unlock
线程18add lock
线程18add unlock
线程18clear lock
线程18clear unlock
线程44add lock
线程44add unlock
线程44clear lock
线程44clear unlock
线程24add lock
线程24add unlock
线程30add lock
线程30add unlock
线程30clear lock
线程30clear unlock
线程63add lock
线程63add unlock
线程63clear lock
线程63clear unlock
线程41add lock
线程41add unlock
线程41clear lock
线程41clear unlock
线程17add lock
线程17add unlock
线程17clear lock
线程17clear unlock
线程105add lock
线程105add unlock
线程105clear lock
线程105clear unlock
线程99add lock
线程99add unlock
线程99clear lock
线程99clear unlock
线程92add lock
线程92add unlock
线程87add lock
线程87add unlock
线程87clear lock
线程87clear unlock
线程109add lock
线程109add unlock
线程109clear lock
线程109clear unlock
线程96add lock
线程96add unlock
线程96clear lock
线程96clear unlock
线程79add lock
线程79add unlock
线程79clear lock
线程79clear unlock
线程72add lock
线程72add unlock
线程72clear lock
线程72clear unlock
线程47add lock
线程47add unlock
线程47clear lock
线程47clear unlock
线程50add lock
线程50add unlock
线程50clear lock
线程50clear unlock
线程53add lock
线程53add unlock
线程53clear lock
线程53clear unlock
线程51add lock
线程51add unlock
线程51clear lock
线程51clear unlock
线程56add lock
线程56add unlock
线程56clear lock
线程56clear unlock
线程59add lock
线程59add unlock
线程59clear lock
线程59clear unlock
线程65add lock
线程65add unlock
线程65clear lock
线程65clear unlock
线程62clear lock
线程62clear unlock
线程69add lock
线程69add unlock
线程69clear lock
线程69clear unlock
线程0add lock
线程0add unlock
线程0clear lock
线程0clear unlock
线程4add lock
线程4add unlock
线程4clear lock
线程4clear unlock
线程7add lock
线程7add unlock
线程7clear lock
线程7clear unlock
线程10add lock
线程10add unlock
线程10clear lock
线程10clear unlock
线程12add lock
线程12add unlock
线程12clear lock
线程12clear unlock
线程6clear lock
线程6clear unlock
线程16add lock
线程16add unlock
线程16clear lock
线程16clear unlock
线程19add lock
线程19add unlock
线程19clear lock
线程19clear unlock
线程22add lock
线程22add unlock
线程22clear lock
线程22clear unlock
线程28add lock
线程28add unlock
线程28clear lock
线程28clear unlock
线程25add lock
线程25add unlock
线程25clear lock
线程25clear unlock
线程13add lock
线程13add unlock
线程13clear lock
线程13clear unlock
线程34add lock
线程34add unlock
线程34clear lock
线程34clear unlock
线程71add lock
线程71add unlock
线程71clear lock
线程71clear unlock
线程57add lock
线程57add unlock
线程57clear lock
线程57clear unlock
线程38add lock
线程38add unlock
线程38clear lock
线程38clear unlock
线程29add lock
线程29add unlock
线程29clear lock
线程29clear unlock
线程104add lock
线程104add unlock
线程104clear lock
线程104clear unlock
线程11add lock
线程11add unlock
线程11clear lock
线程11clear unlock
线程107add lock
线程107add unlock
线程107clear lock
线程107clear unlock
线程101add lock
线程101add unlock
线程101clear lock
线程101clear unlock
线程98add lock
线程98add unlock
线程98clear lock
线程98clear unlock
线程95add lock
线程95add unlock
线程95clear lock
线程95clear unlock
线程93add lock
线程93add unlock
线程93clear lock
线程93clear unlock
线程80add lock
线程80add unlock
线程80clear lock
线程80clear unlock
线程77add lock
线程77add unlock
线程77clear lock
线程77clear unlock
线程89add lock
线程89add unlock
线程89clear lock
线程89clear unlock
线程86add lock
线程86add unlock
线程86clear lock
线程86clear unlock
线程92clear lock
线程92clear unlock
线程83add lock
线程83add unlock
线程83clear lock
线程83clear unlock
线程81add lock
线程81add unlock
线程81clear lock
线程81clear unlock
线程75add lock
线程75add unlock
线程75clear lock
线程75clear unlock
线程106add lock
线程106add unlock
线程106clear lock
线程106clear unlock
线程108add lock
线程108add unlock
线程108clear lock
线程108clear unlock
线程103add lock
线程103add unlock
线程103clear lock
线程103clear unlock
线程102add lock
线程102add unlock
线程102clear lock
线程102clear unlock
线程97add lock
线程97add unlock
线程97clear lock
线程97clear unlock
线程94add lock
线程94add unlock
线程94clear lock
线程94clear unlock
线程100add lock
线程100add unlock
线程100clear lock
线程100clear unlock
线程91add lock
线程91add unlock
线程91clear lock
线程91clear unlock
线程85add lock
线程85add unlock
线程85clear lock
线程85clear unlock
线程88add lock
线程88add unlock
线程88clear lock
线程88clear unlock
线程90add lock
线程90add unlock
线程90clear lock
线程90clear unlock
线程84add lock
线程84add unlock
线程84clear lock
线程84clear unlock
线程82add lock
线程82add unlock
线程82clear lock
线程82clear unlock
线程78add lock
线程78add unlock
线程78clear lock
线程78clear unlock
线程76add lock
线程76add unlock
线程76clear lock
线程76clear unlock
线程73add lock
线程73add unlock
线程73clear lock
线程73clear unlock
线程70add lock
线程70add unlock
线程70clear lock
线程70clear unlock
线程67add lock
线程67add unlock
线程67clear lock
线程67clear unlock
线程60add lock
线程60add unlock
线程60clear lock
线程60clear unlock
线程58add lock
线程58add unlock
线程58clear lock
线程58clear unlock
线程49add lock
线程49add unlock
线程49clear lock
线程49clear unlock
线程54add lock
线程54add unlock
线程54clear lock
线程54clear unlock
线程52add lock
线程52add unlock
线程52clear lock
线程52clear unlock
线程48add lock
线程48add unlock
线程48clear lock
线程48clear unlock
线程42add lock
线程42add unlock
线程42clear lock
线程42clear unlock
线程64add lock
线程64add unlock
线程64clear lock
线程64clear unlock
线程40add lock
线程40add unlock
线程40clear lock
线程40clear unlock
线程24clear lock
线程24clear unlock
线程31add lock
线程31add unlock
线程31clear lock
线程31clear unlock
线程46add lock
线程46add unlock
线程46clear lock
线程46clear unlock
线程66add lock
线程66add unlock
线程66clear lock
线程66clear unlock
线程36add lock
线程36add unlock
线程36clear lock
线程36clear unlock
线程61add lock
线程61add unlock
线程61clear lock
线程61clear unlock
线程43add lock
线程43add unlock
线程43clear lock
线程43clear unlock
线程74add lock
线程74add unlock
线程74clear lock
线程74clear unlock
线程37add lock
线程37add unlock
线程37clear lock
线程37clear unlock
线程55add lock
线程55add unlock
线程55clear lock
线程55clear unlock

Process finished with exit code 0

第二个是大家都熟悉的synchorized关键字,打印结果只贴一部分,可以看到结果和上面一样,在加锁的操作里只有一个线程能进方法,也就是持有该对象,当然对于没有加锁的方法依然可以被多个线程同时访问(这个是老师教但是没有自己证实过)。

线程59clear lock
线程59clear unlock
线程60add lock
线程60add unlock
线程60clear lock
线程60clear unlock
线程55add lock
线程55add unlock
线程55clear lock
线程55clear unlock
线程56add lock
线程56add unlock
线程56clear lock
线程56clear unlock
线程51add lock
线程51add unlock
线程51clear lock
线程51clear unlock
线程43add lock
线程43add unlock
线程43clear lock
线程43clear unlock
线程48add lock
线程48add unlock
线程48clear lock
线程48clear unlock
线程72add lock
线程72add unlock
线程72clear lock
线程72clear unlock
线程63add lock
线程63add unlock
线程63clear lock
线程63clear unlock
线程77add lock
线程77add unlock
线程77clear lock
线程77clear unlock
线程14add lock
线程14add unlock
线程14clear lock
线程14clear unlock
线程76add lock
线程76add unlock
线程76clear lock
线程76clear unlock
线程64add lock
线程64add unlock
线程64clear lock
线程64clear unlock
线程49add lock
线程49add unlock
线程49clear lock
线程49clear unlock
线程61add lock
线程61add unlock
线程61clear lock
线程61clear unlock
线程57add lock
线程57add unlock
线程57clear lock
线程57clear unlock
线程18add lock
线程18add unlock
线程18clear lock
线程18clear unlock
线程54add lock
线程54add unlock
线程54clear lock
线程54clear unlock

所以结论是可重入锁锁的是当前持有锁的对象对象,和synchronized关键字锁的后面括号里的对象,可以看出sync关键字的可读性更好,因为可以直接看到后面括号里的类或对象,而可重入锁没有。但是可重入锁的性能更好(具体好多少后面再出个博文),sync是系统级别函数调用开销大(这句话是并发编程的艺术这本书里写的,背下来)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!