ReentrantReadWriteLock,ReadLock和WriteLock之间有什么区别 [英] ReentrantReadWriteLock,what's the difference between ReadLock and WriteLock

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

问题描述

我所知道的,writelock就像是同步的。

What I know is , writelock is just like synchronized .

Readlock和writelock以某种方式相互影响。

Readlock and writelock affect each other somehow .

ReadLock似乎不能单独工作。

ReadLock seems cann't work alone.

推荐答案

readLock.lock();



这意味着如果任何其他线程(即持有写锁定),则在此处停止,直到没有其他线程正在写入。

readLock.lock();

This means that if any other thread is writing (i.e. holds a write lock) then stop here until no other thread is writing.

一旦授予锁定,就不允许其他线程写入(即进行写锁定)直到锁定被释放。

Once the lock is granted no other thread will be allowed to write (i.e. take a write lock) until the lock is released.

这意味着如果任何其他线程正在读取,停在此处等待,直到没有其他线程正在读或写。

This means that if any other thread is reading or writing, stop here and wait until no other thread is reading or writing.

一旦授予锁定,将不允许其他线程读取写入(即读取或写入锁定),直到锁定被释放。

Once the lock is granted, no other thread will be allowed to read or write (i.e. take a read or write lock) until the lock is released.

Co对于这些,您可以一次只安排一个线程进行写访问,但是除了线程写入之外,您可以同时读取多个读取器。

Combining these you can arrange for only one thread at a time to have write access but as many readers as you like can read at the same time except when a thread is writing.

换句话说。每当您想要从结构读取时,请执行读取锁定。每次要时,请执行写入锁定。这样一旦写入发生,没有人正在阅读(你可以想象你有独家访问权限),但只要没有人在写,就会有很多读者同时阅读。

Put another way. Every time you want to read from the structure, take a read lock. Every time you want to write, take a write lock. This way whenever a write happens no-one is reading (you can imagine you have exclusive access), but there can be many readers reading at the same time so long as no-one is writing.

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

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