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 ... */});}

相关内容

热门资讯

“床垫界的特斯拉”破产了 特斯... 近日,睡眠科技赛道的全球龙头、有“床垫界的特斯拉”之称的Sleep Number已向法院申请破产保护...
上交所:将主板风险警示股票价格... 4月10日消息,深交所近日起草了《上海证券交易所交易规则(征求意见稿)》。现就有关事项向市场公开征求...
德国今年一季度企业破产数量创2... 4月10日消息,当地时间9日,德国莱布尼茨经济研究所发布的数据显示,今年一季度,德国企业破产数量显著...
脉脉发布"隐形大厂&... 4月10日消息,脉脉发布“隐形大厂”20强名单,这些企业集中分布于AI、机器人、智能驾驶、芯片等热门...
中国证监会依法处罚东旭系审计机... 4月10日消息,近日,河北证监局依法对中兴财光华执审东旭集团、东旭光电、东旭蓝天年报及股票发行、债券...
港股收评:恒指涨0.55%,恒... 4月10日消息,港股冲高回落,恒指收盘涨0.55%,恒生科技指数涨0.8%,盘中一度涨超2%。锂电池...
证监会:创业板第四套上市标准共... 4月10日消息,中国证监会新闻发言人就《关于深化创业板改革 更好服务新质生产力发展的意见》答记者问。...
证监会:在创业板引入做市商、大... 4月10日消息,经国务院同意,中国证监会4月10日发布《关于深化创业板改革 更好服务新质生产力发展的...
证监会启动上市公司治理专项行动... 4月10日消息,证监会自4月起启动上市公司治理专项行动。行动聚焦八个方面。一是提高董事会秘书履职能力...
一季度全国开行旅游列车627列 4月10日消息,一季度,元旦、春节和春假叠加,旅客客流增长明显,全国铁路开行旅游列车627列,有力促...