码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Functional Programming in Java venkat(13) Working with Resources


    文章目录

    • Functional Programming in Java venkat(13): Working with Resources
      • Managing Locks

    Functional Programming in Java venkat(13): Working with Resources

    这里是记录学习这本书 Functional Programming in Java: Harnessing the Power Of Java 8 Lambda Expressions 的读书笔记,如有侵权,请联系删除。

    Managing Locks

    并发编程中锁很重要。

    synchronize的弊端

    首先,对synchronized的调用很难超时,这会增加死锁和活锁的机会。

    其次,很难模拟出synchronized,这使得单元测试很难看到代码是否遵守了适当的线程安全性。

    First, it’s
    hard to time out a call to synchronized, and this can increase the chance of
    deadlocks and livelocks. Second, it’s hard to mock out synchronized, and that
    makes it really hard to unit-test to see if code adheres to proper thread safety

    为了解决这些问题,在Java 5中引入了Lock接口,以及一些实现,如ReentrantLock。Lock接口给了我们更好的锁定、解锁控制权,检查是否有锁,如果在一定的时间范围内没有获得锁,还可以轻松地超时展示出来。因为这是一个接口,很容易模拟它的实现用于单元测试。

    To address these concerns, the Lock interface, along with a few implementations such as ReentrantLock, was introduced in Java 5. The Lock interface gives us better control to lock, unlock, check if a lock is available, and easily time out if a lock is not gained within a certain time span. Because this is an interface, it’s easy to mock up its implementation for the sake of unit testing

    Lock的弊端,需要显式的unlock

    There’s one caveat to the Lock interface—unlike its counterpart synchronized, it
    requires explicit locking and unlocking. This means we have to remember to
    unlock, and to do the same in the finally block. From our discussions so far
    in this chapter, we can see lambda expressions and the execute around method
    pattern helping out quite a bit here.

    看一个显式使用lock的例子,需要在finally块中unlock

    public class Locking {
      Lock lock = new ReentrantLock(); //or mock
      
      protected void setLock(final Lock mock) {
        lock = mock;
      } 
    
      public void doOp1() {
        lock.lock();
        try {
          //...critical code...
        } finally {
          lock.unlock();
        }
      }
      //...
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    使用lambda表达式来简化lock的调用,我们把复杂的部分放在一个方法runLocked中,使得调用的方法接口更简洁。

    package fpij;
    
    import java.util.concurrent.locks.Lock;
    
    public class Locker {
      public static void runLocked(Lock lock, Runnable block) {
        lock.lock();
    	
        try {
          block.run();
        } finally {
          lock.unlock();
        }    
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    lambda表达式调用runLock方法

    
      public void doOp2() {
        runLocked(lock, () -> {/*...critical code ... */});
      }
      
      public void doOp3() {
        runLocked(lock, () -> {/*...critical code ... */});
      }
      
      public void doOp4() {
        runLocked(lock, () -> {/*...critical code ... */});
      }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
  • 相关阅读:
    【云原生之Docker实战】使用Docker部署Wizard文档管理系统
    Python爬虫基础(三):使用Selenium动态加载网页
    大数据之Hive
    logback-spring.xml配置文件标签(超详解)
    node版本升级:与node-sass、sass-loader版本不兼容问题以及npm install时报错问题解决方法
    2023-Chrome插件推荐
    MongoDB CRUD操作:地理位置应用——通过地理空间查询查找餐厅
    猿创征文|我的前端——【HTML5】基础成长学习之路
    每日学到 43 - JavaScript中BOM和DOM
    Facebook Messenger链接分享:如何创建链接并设置自动化内容
  • 原文地址:https://blog.csdn.net/shizheng_Li/article/details/128104832
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号