Functional Programming in Java venkat(13) Working with Resources
admin
2024-02-29 22:48:27
0次

文章目录

  • 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 mockprotected void setLock(final Lock mock) {lock = mock;} public void doOp1() {lock.lock();try {//...critical code...} finally {lock.unlock();}}//...}

使用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();}    }
}

lambda表达式调用runLock方法

public void doOp2() {runLocked(lock, () -> {/*...critical code ... */});}public void doOp3() {runLocked(lock, () -> {/*...critical code ... */});}public void doOp4() {runLocked(lock, () -> {/*...critical code ... */});}

相关内容

热门资讯

分辨真假辅助!喜临门辅助多功能... 分辨真假辅助!喜临门辅助多功能教程,喜临门本来有辅助教程1、很好的工具软件,可以解锁游戏的喜临门辅助...
最新技巧辅助!慕雪娱乐辅助多功... 最新技巧辅助!慕雪娱乐辅助多功能工具,慕雪娱乐一直有辅助工具运慕雪娱乐辅助工具,进入游戏界面。进入慕...
玩家必看科普透视挂!中至余干麻... 您好,中至余干麻将510k这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很...
盘点一款透视挂!绿色棋游透视挂... 盘点一款透视挂!绿色棋游透视挂多功能工具,绿色棋游一贯有透视挂工具1、上手简单,内置详细流程视频教学...
记者揭秘透视器!常德跑胡子透视... 记者揭秘透视器!常德跑胡子透视器多功能技巧,常德跑胡子切实存在有透视器技巧1、打开软件启动之后找到中...
科技新动态辅助器!竹间茶馆辅助... 科技新动态辅助器!竹间茶馆辅助器多功能软件,竹间茶馆真是有辅助器软件1)竹间茶馆有没有挂:进一步探索...
玩家必备科普透视挂!张同学俱乐... 玩家必备科普透视挂!张同学俱乐部透视挂多功能app,张同学俱乐部一直是真的有透视挂app一、张同学俱...
每日必看透视器!约逗东乡棋牌透... 每日必看透视器!约逗东乡棋牌透视器多功能教程,约逗东乡棋牌竟然真的有透视器教程约逗东乡棋牌脚本下载中...
大家学习交流透视挂!爱上斗牌透... 大家学习交流透视挂!爱上斗牌透视挂多功能软件,爱上斗牌其实是有透视挂软件1、这是跨平台的爱上斗牌轻量...
总算了解透视器!FLX透视器多... 总算了解透视器!FLX透视器多功能方法,FLX都是存在有透视器方法1、进入到FLX是否有挂之后,能看...