ReentrantReadWriteLock vs synchronized [英] ReentrantReadWriteLock vs synchronized

查看:119
本文介绍了ReentrantReadWriteLock vs synchronized的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与Java中多线程环境中的 synchronized 关键字相比,我们何时应该使用 ReentrantReadWriteLock

When should we use ReentrantReadWriteLock as compared to synchronized keyword in multithreaded environment in Java?

在Java中使用 ReentrantReadWriteLock 而不是 synchronized 有什么好处?

What are the benefits of using ReentrantReadWriteLock over synchronized in Java?

任何人都可以给出一个例子(用Java)吗?

Can any one give an example as well (in Java)?

谢谢!

推荐答案

同步允许一次在一个线程中。

Synchronized allows in one thread at a time.

读/写锁允许多个读取器同时允许,但仅限如果没有编写器已经进入。因此在一些使用场景下我们可以获得更好的并发性,因为读者群可以一起进行。

Read/Write locks allow in multiple readers a the same time, but only if no writers are already in. Hence under some usage scenarios we can get better concurrency, because the reader populations can proceed together.

Java API 文档给出了预期会有的集合类示例读者多于作家。

Java API documentation gives the example of collection classes which are expected to have more readers than writers.

这篇关于ReentrantReadWriteLock vs synchronized的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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