新的Lock接口优于Java中的synchronized块有什么优势? [英] What is the advantage of new Lock interface over synchronized block in Java?

查看:278
本文介绍了新的Lock接口优于Java中的synchronized块有什么优势?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新的Lock接口优于Java中的synchronized块有什么优势?您需要实现一个高性能缓存,它允许多个读者但单个编写者保持完整性如何实现它?

What is the advantage of new Lock interface over synchronized block in Java? You need to implement a high performance cache which allows multiple reader but single writer to keep the integrity how will you implement it?

推荐答案

锁定的优点是


  • 可以使它们公平

  • 它可以制作一个在等待Lock对象时响应中断的线程。

  • 可以尝试获取锁定,但如果无法获取锁定,则立即返回或超时后

  • 可以在不同的范围内以不同的顺序获取和释放锁

  • it's possible to make them fair
  • it's possible to make a thread responsive to interruption while waiting on a Lock object.
  • it's possible to try to acquire the lock, but return immediately or after a timeout if the lock can't be acquired
  • it's possible to acquire and release locks in different scopes, and in different orders

请注意,这是解释在 javadoc of Lock 中及其子类。

Note that this is explained in the javadoc of Lock and its subclasses.

可以使用ConcurrentMap实现高性能缓存。

A high performant cache could be implemented using a ConcurrentMap.

这篇关于新的Lock接口优于Java中的synchronized块有什么优势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆