使用synchronized关键字和锁之间有什么区别 [英] what the difference between using synchronized keyword and lock

查看:147
本文介绍了使用synchronized关键字和锁之间有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java 5介绍了lock方法。使用lock的优缺点与synchronized关键字相比?

Java 5 introduce the lock method. Any pros and cons using lock compared to synchronized keyword?

推荐答案

'synchronize'将锁定方法中访问的所有资源。 'lock'允许您更精细和控制(例如,仅锁定某些资源,或仅在满足特定条件时锁定)。

'synchronize' will lock any resources accessed within the method. 'lock' allows you more granularity, and control (e.g. only locking some resources, or locking only if a certain condition is met, for example).

这是一个非常好的此链接顶部附近的代码示例:
http://download.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/locks/Lock.html

There's a pretty good code sample near the top of this link: http://download.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/locks/Lock.html

这篇关于使用synchronized关键字和锁之间有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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