博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ReentrantLock使用对比Synchronized
阅读量:4150 次
发布时间:2019-05-25

本文共 3233 字,大约阅读时间需要 10 分钟。

简述:这篇文章主要是来了解如何使用ReentrantLock,以及ReentrantLock和Synchronized的对比,基于ReentrantLock的AQS源码的分析在中进行了分析介绍, 中对synchronized进行了详细的介绍

    ReentrantLock是可重入锁,也是独享锁,可重入锁是指:如果当前线程获取到了锁,这个线程可再次进入同步块,独享锁是指,当前锁只能被一个线程锁所拥有

已经有Synchronized了,为什么还要使用Lock?

1、synchronized底层是基于jvm实现的,其加锁和释放锁的操作都是隐式的,其他线程如果没有获取到锁,将进入对象的锁池中,进行等待,这个过程中,没有办法手动将等待的线程释放掉,而ReentrantLock可以手动放弃等待

2、在实际开发场景当中,多个线程对文件进行写操作时,会发生线程,但是如果多个线程之间进行读操作,并不会发生冲突,如果我们都是使用Synchronized来加锁的话,显然会造成一种资源和性能上的浪费,如果这时有一个写锁,只对写操作进行加锁,多线程之间进行读操作时,不会发生冲突。

3、lock操作可以知道当前线程是否获取到了锁,相比较于Synchronized, 显然只是对同步代码进行了加锁操作并不知道我所关心的线程是否获取到了锁。lock更加细粒度的进行了配置

4、当然lock需要手动进行锁的释放,synchronized并不需要进行释放锁

ReentrantLock通用使用方式

Lock lock = new ReentrantLock();lock.lock();try{  执行代码逻辑}catch(Exception e){}finally{ lock.unlock}
public class ReenterLockTest {    List
arrayList = new ArrayList<>(); Lock lock = new ReentrantLock(); public void insert(Thread thread) { lock.lock(); try{ System.out.println(thread.getName()+" 得到了锁"); for(int i = 0; i < 5; i++) { arrayList.add(i); } }catch (Exception e){ }finally { System.out.println(thread.getName() +" 释放了锁"); lock.unlock(); } } public static void main(String[] args) { ReenterLockTest test = new ReenterLockTest(); new Thread(()->{ test.insert(Thread.currentThread()); }).start(); new Thread(() ->{ test.insert(Thread.currentThread()); }).start(); }}

以上代码一定是一个线程执行结束之后,另外一个线程才可以执行

关于lockInterruptibly , 可以中断掉正在阻塞的线程。如果当前线程正在执行,是不能被中断的,如果两个线程都通过lockInterruptibly方式去获取锁,一个获取成功,另外一个失败,对于失败的线程执行interrupt方法,可以中断阻塞。

public class ReentrantLockTest3 {    private Lock lock = new ReentrantLock();    private List
list = new ArrayList<>(); public void insert(Thread thread) throws InterruptedException{ lock.lockInterruptibly(); try{ System.out.println(thread.getName()+"获得到了锁!"); long startTime = System.currentTimeMillis(); // for(int i = 0; i < 5;i++){ for(; ; ){ if(System.currentTimeMillis() - startTime >= 10000) { break; } } }finally { System.out.println(thread.getName()+"执行finally"); lock.unlock(); System.out.println("释放掉了锁!"); } } public static void main(String[] args) { ReentrantLockTest3 test = new ReentrantLockTest3(); InterruptedThread thread1 = new InterruptedThread(test); InterruptedThread thread2 = new InterruptedThread(test); thread1.start(); thread2.start(); try{ Thread.sleep(2000); }catch (InterruptedException e) { e.printStackTrace(); } thread2.interrupt(); }}class InterruptedThread extends Thread{ private ReentrantLockTest3 test = null; public InterruptedThread(ReentrantLockTest3 test) { this.test = test; } public void run(){ try{ test.insert(Thread.currentThread()); }catch (InterruptedException e){ System.out.println(Thread.currentThread().getName()+"被中断!"); } }}

执行结果

Thread-0获得到了锁!Thread-1被中断!Thread-0执行finally释放掉了锁!
未完~

转载地址:http://movti.baihongyu.com/

你可能感兴趣的文章
Xcode 11 报错,提示libstdc++.6 缺失,解决方案
查看>>
idea的安装以及简单使用
查看>>
Windows mysql 安装
查看>>
python循环语句与C语言的区别
查看>>
vue 项目中图片选择路径位置static 或 assets区别
查看>>
vue项目打包后无法运行报错空白页面
查看>>
Vue 解决部署到服务器后或者build之后Element UI图标不显示问题(404错误)
查看>>
element-ui全局自定义主题
查看>>
facebook库runtime.js
查看>>
vue2.* 中 使用socket.io
查看>>
openlayers安装引用
查看>>
js报错显示subString/subStr is not a function
查看>>
高德地图js API实现鼠标悬浮于点标记时弹出信息窗体显示详情,点击点标记放大地图操作
查看>>
初始化VUE项目报错
查看>>
vue项目使用安装sass
查看>>
HTTP和HttpServletRequest 要点
查看>>
在osg场景中使用GLSL语言——一个例子
查看>>
laravel 修改api返回默认的异常处理
查看>>
laravel事务
查看>>
【JavaScript 教程】浏览器—History 对象
查看>>