ReadWriteLock
ReadWriteLock 翻译说明:
A ReadWriteLock maintains a pair of associated locks, one for read-only operations and one for writing. The read lock may be held simultaneously by multiple reader threads, so long as there are no writers. The write lock is exclusive.
一个ReadWriteLock持有两个相关的锁,一个只读锁和一个写锁,当没有写的时候,读锁可以同时被多个读同时调用,写锁是独占的
All ReadWriteLock implementations must guarantee that the memory synchronization effects of writeLock operations (as specified in theLock interface) also hold with respect to the associated readLock. That is, a thread successfully acquiring the read lock will see all updates made upon previous release of the write lock.
所有的ReadWriteLock实现必须保证writeLock操作要与相关的readLock内存同步,也就是说,一个现场获得读锁的时候能够看到之前写锁的更新
A read-write lock allows for a greater level of concurrency in accessing shared data than that permitted by a mutual exclusion lock. It exploits the fact that while only a single thread at a time (a writer thread) can modify the shared data, in many cases any number of threads can concurrently read the data (hence reader threads). In theory, the increase in concurrency permitted by the use of a read-write lock will lead to performance improvements over the use of a mutual exclusion lock. In practice this increase in concurrency will only be fully realized on a multi-processor, and then only if the access patterns for the shared data are suitable.
一个读写锁比一个独占锁允许更高的级别权限去访问共享数据。事实上只有一个线程在同一时间上能更新共享数据,大多情况下任何多的线程都能并发读数据(强化了读线程),理论上,这种并行进入的读写锁比独占有更好的并发性能,在实践中,只有在多核处理器并且在访问共享数据的时候才适用
Whether or not a read-write lock will improve performance over the use of a mutual exclusion lock depends on the frequency that the data is read compared to being modified, the duration of the read and write operations, and the contention for the data - that is, the number of threads that will try to read or write the data at the same time. For example, a collection that is initially populated with data and thereafter infrequently modified, while being frequently searched (such as a directory of some kind) is an ideal candidate for the use of a read-write lock. However, if updates become frequent then the data spends most of its time being exclusively locked and there is little, if any increase in concurrency. Further, if the read operations are too short the overhead of the read-write lock implementation (which is inherently more complex than a mutual exclusion lock) can dominate the execution cost, particularly as many read-write lock implementations still serialize all threads through a small section of code. Ultimately, only profiling and measurement will establish whether the use of a read-write lock is suitable for your application.
是否一个读写锁的性能高于独占所,取决于这个数据读的频率高于写的频率,以及数据的争用,多个线程同时试着去读或者写数据。举个例子,一个初始化数据完了的集合,更新很少,但是被频繁的查找(比如搜索一个目录)就很适合使用读写锁。假如,它更新频繁了,时间大部分都被独占锁占用,就算存在并发增强,也是微不足道的。更进一步的说,如果读操作太短
待续。。。。。。
浙公网安备 33010602011771号