ReentrantLock和Synchronized简单比较

简介: ReentrantLock和Synchronized简单比较

ReentrantLock
Synchronized
锁实现机制
依赖AQS
监视器模式
灵活性
支持响应中断、超时、尝试获取锁
不灵活
释放形式 必须显式调用unlock()方法释放锁
自动释放监听器
锁类型
公平锁&非公平锁
非公平锁
条件队列
可关联多个条件队列
关联一个条件队列
可重入性
可重入 可重入

下面进行伪代码进行比较

//**************synchronized的实现方式******************
// 1、用于代码块
synchronized(this){}
// 2、用于实例对象
synchronized(object){}
// 3、用于方法
public synchronized void test(){}
// 4、可重入
for(int i=0;i<10;i++){
   synchronized(this){}   
}
// *********************ReentrantLock使用方式***********************
public void test() throws Exception{
    //1、初始化选择公平锁,非公平锁
    ReentrantLock lock = new ReentrantLock(true);    
    //2、用于代码块
    lock.lock();
    try{
        try{
            //3、支持多种加锁方式,比较灵活,具有可重入特性
            if(lock.tryLock(10,TimeUtil.MILLISECONDS)){}
         }finally{
           //4、手动释放锁 
           lock.unlock();
        }
    } finally{
       lock.unlock();
    }       
}

自定义个实现AbstractQueuedSynchronizer 类的同步块

package com.kit.mok;
import java.util.concurrent.locks.AbstractQueuedSynchronizer;
/**
 * @author 七彩星星
 * @version 1.0
 * @date 2021/7/26 16:59
 */
public class Aqslock {
    public static class Sync extends AbstractQueuedSynchronizer {
        @Override
        protected boolean tryAcquire(int args) {
            return compareAndSetState(0, 1);
        }
        @Override
        protected boolean tryRelease(int args) {
            setState(0);
            return true;
        }
        @Override
        protected boolean isHeldExclusively() {
            return getState() == 1;
        }
    }
    private Sync sync = new Sync();
    public void lock() {
        sync.acquire(1);
    }
    public void unlock() {
        sync.release(1);
    }
}
package com.kit.mok;
/**
 * @author 七彩星星
 * @version 1.0
 * @date 2021/7/26 17:05
 */
public class AqsMain {
    static int count = 0;
    static Aqslock reentrantLock = new Aqslock();
    // static ReentrantLock reentrantLock = new ReentrantLock(false);
    public static void main(String[] args) throws InterruptedException {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                try {
                    aqslock.lock();
                    for (int i = 0; i < 10000; i++) {
                        count ++;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    aqslock.unlock();
                }
            }
        };
        Thread thread1=new Thread(runnable);
        Thread thread2=new Thread(runnable);
        thread1.start();
        thread2.start();
        thread1.join();
        thread2.join();
        System.out.println(count);
    }
}

输出结果:

目录
相关文章
|
1月前
|
安全 Java API
ReentrantLock
ReentrantLock
37 0
|
4月前
synchronized与ReentrantLock区别与联系
synchronized与ReentrantLock区别与联系
22 0
|
9月前
|
程序员
ReentrantLock与synchronized的区别
ReentrantLock与synchronized的区别
|
10月前
|
Java
16.ReentrantLock全解读
大家好,我是王有志。今天和大家一起聊聊ReentrantLock,它是我们最常见的基于AQS实现的互斥锁。
73 0
|
存储 缓存 安全
【Synchronized】
【Synchronized】
104 0
【Synchronized】
ReentrantLock介绍
ReentrantLock介绍
126 0
synchronized和ReentrantLock的区别
synchronized和ReentrantLock的区别
101 0
synchronized与ReentrantLock有什么区别
synchronized与ReentrantLock区别之处
85 0
|
安全 Java
synchronized 和 ReentrantLock 有什么区别?
synchronized 和 ReentrantLock 有什么区别?
149 0
synchronized 和 ReentrantLock 有什么区别?
|
存储 设计模式 Java
深入理解ReentrantLock
同步锁synchronized和重入锁ReentrantLock都是用于并发程序设计必不可少的手段,在JDK 5.0早期版本中,同步锁性能远远低于重入锁,但是在6.0版本之后,jdk对同步锁做了大量的优化,使得同步锁跟重入锁性能差距并不大,并且jdk团队表示,同步锁还有进一步升级优化的空间
深入理解ReentrantLock