ReentrantLock学习
2022-05-06 19:58
232 查看
ReentrantLock
ReentrantLock功能
ReentrantLock和synchronized一样是可重入的
可重入即当线程拥有了锁时,当该线程再次请求锁资源的时候,线程是可以再次成功获得的。
-
static ReentrantLock lock = new ReentrantLock(); public static void main(String[] args) { new Thread(() ->{ lock.lock(); try{ log.debug("获得了锁,再次请求锁"); lock.lock(); try{ log.debug("再次获得了锁"); }finally { log.debug("释放了锁"); lock.unlock(); } }finally { log.debug("释放了锁"); lock.unlock(); } },"t1").start(); }
ReentrantLock是可打断的
即请求锁资源的时候,当请求不到锁资源的时候,可以被interrupt方法打断。
static ReentrantLock lock = new ReentrantLock(); public static void main(String[] args) { Thread t1 = new Thread(() -> { try { // 如果当lock请求锁资源的同时被其他其他线程调用了interrupt方法 // 就会进入异常状态 lock.lockInterruptibly(); } catch (InterruptedException e) { e.printStackTrace(); log.debug("请求锁被打断"); return; // 当请求锁被打断后没有获得锁,不应该在进入下面的语句 } try { log.debug("获得到了锁资源"); } finally { lock.unlock(); } }, "t1"); lock.lock(); t1.start(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } t1.interrupt(); }
ReentrantLock可以锁超时
synchronized请求锁会一直地等待锁资源,而ReentrantLock请求锁不会无限制的进行下去,使用tryLock()方法,可以在一次请求锁资源或请求一段时间的锁资源后结束请求锁。
static ReentrantLock lock = new ReentrantLock(); public static void main(String[] args) throws InterruptedException { Thread t1 = new Thread(() -> { log.debug("开始运行了"); // 该情况下只会请求一次锁资源,请求到锁资源返回true,否则返回false // 加上时间限制的trylock是一样的。 try { if (lock.tryLock(2, TimeUnit.SECONDS)) { try { log.debug("请求到了锁资源"); } finally { lock.unlock(); } } else { log.debug("没请求到锁资源"); } } catch (InterruptedException e) { e.printStackTrace(); } }, "t1"); lock.lock(); log.debug("上锁了"); t1.start(); Thread.sleep(1000); log.debug("解锁了"); lock.unlock(); }
当一些线程一直无法获得锁资源时,使用公平锁就可以时获得锁变成先进先获得。
static ReentrantLock lock = new ReentrantLock(true);
条件变量,即ReentrantLock支持多条件的进入不同的WaitSet进行等待,synchronized就只有一个WaitSet队列。
当出现需要不同条件进入等待就可以使用该条件变量。
static ReentrantLock lock = new ReentrantLock(); static Condition waitCondition1Set = lock.newCondition(); // 然后调用condition的await方法即可进入等待状态。
设计模式---顺序执行
保证线程执行的一致性,因为这里是同一个锁对象所以不能用join
wait-notify
@Slf4j public class Test5 { static boolean already = false; public static void main(String[] args) { final Object o = new Object(); Thread t1 = new Thread(() -> { synchronized (o){ while (!already){ try { o.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } log.debug("1"); } }, "t1"); Thread t2 = new Thread(() -> { synchronized (o){ log.debug("2"); already = true; o.notify(); } }, "t2"); t1.start(); t2.start(); } }
park-unpark
@Slf4j public class Test6 { public static void main(String[] args) throws InterruptedException { Thread t1 = new Thread(() -> { LockSupport.park(); log.debug("1"); }, "t1"); Thread t2 = new Thread(() -> { LockSupport.park(); log.debug("2"); LockSupport.unpark(t1); }, "t2"); t1.start(); t2.start(); Thread.sleep(2000); LockSupport.unpark(t2); } }
设计模式---交替输出
三个线程交替输出
wait-notify
public class Test7 { public static void main(String[] args) { PrintObject object = new PrintObject(5,2); Thread t1 = new Thread(() -> { object.print(2,1); }, "t1"); Thread t2 = new Thread(() -> { object.print(3,2); }, "t2"); Thread t3 = new Thread(() -> { object.print(1,3); }, "t3"); t1.start(); t2.start(); t3.start(); } } class PrintObject{ private int loopNum; private int flag; public PrintObject(int loopNum,int flag) { this.loopNum = loopNum; this.flag = flag; } public synchronized void print(int next,int now){ for(int i = 0;i < loopNum;i++){ while (flag != now){ try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(now); flag = next; notifyAll(); } } }
await-signal
class PrintObject{ private ReentrantLock lock = new ReentrantLock(); private Condition condition = lock.newCondition(); private int loopNum; private int flag; public PrintObject(int loopNum,int flag) { this.loopNum = loopNum; this.flag = flag; } public void print(int next,int now){ lock.lock(); try{ for(int i = 0;i < loopNum;i++){ while (flag != now){ try { condition.await(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(flag); flag = next; condition.signalAll(); } }finally { lock.unlock(); } } }
park-unpark
@Slf4j public class Test8 { static Thread t1; static Thread t2; static Thread t3; public static void main(String[] args) throws InterruptedException { t1 = new Thread(() -> { LockSupport.park(); log.debug("1"); LockSupport.unpark(t2); }, "t1"); t2 = new Thread(() -> { LockSupport.park(); log.debug("2"); LockSupport.unpark(t3); }, "t2"); t3 = new Thread(() -> { LockSupport.park(); log.debug("3"); LockSupport.unpark(t1); }, "t3"); t1.start(); t2.start(); t3.start(); Thread.sleep(1000); LockSupport.unpark(t1); } }
相关文章推荐
- 戏说领域驱动设计(廿五)——领域事件
- 朴素贝叶斯算法的实现与推理
- 【Azure Developer】使用 Powershell az account get-access-token 命令获取Access Token (使用用户名+密码)
- MySQL(5) - 数据库的高级查询
- Spring Ioc源码分析系列--Ioc的基础知识准备
- 使用docker搭建jupyter notebook / jupyterlab
- Spring 源码(8)Spring BeanPostProcessor的注册、国际化及事件发布机制
- 给vscode注入骚的一批的灵魂字体
- Masa Blazor自定义组件封装
- Flutter 状态管理框架 Provider 和 Get 分析
- 爬虫介绍、requests模块使用、get地址中携带参数、请求头、cookie、发送post请求模拟登陆、响应对象、编码问题、获取二进制数据、解析json、ssl认证、使用代理
- 【面试普通人VS高手系列】讲一下wait和notify这个为什么要在synchronized代码块中?
- Azure DevOps (十二) 通过Azure Devops部署一个SpringBoot应用
- java高级用法之:JNA中的Function
- Spring按业务模块输出日志到不同的文件
- Apache ShardingSphere 企业行|走进携程
- Water 2.6.3 发布,一站式服务治理平台
- 【Java分享客栈】超简洁SpringBoot使用AOP统一日志管理-纯干货干到便秘
- Solon 项目的单元测试插件
- 最佳案例 | QQ 相册云原生容器化之路