Reentry lock and read-write lock in Java Concurrent Programming

Reentry lock

Reentry lock, as its name suggests, is a lock that supports reentry. It means that the lock can support repeated locking of resources by a thread. Reentry means that any thread can acquire the lock again without being blocked by the lock after acquiring the lock. The implementation of this feature needs to solve the following two problems.

1. The thread acquires the lock again. The lock needs to identify whether the thread obtaining the lock is the thread currently occupying the lock. If so, it will be obtained successfully again.

2. Final release of the lock. The thread repeatedly obtains the lock n times, and then after releasing the lock n times, other threads can obtain the lock. The final release of the lock requires that the count of the acquisition of the lock increases automatically. The count indicates the number of times the current lock is repeatedly acquired. When the lock is released, the count decreases automatically. When the count is equal to 0, it indicates that the lock has been successfully released.

The built-in lock (synchronize) and lock (reentrant lock) in Java are reentrant

Synchronized instance

Lock instance

The final results of both examples are correct, and the results are as follows:

The biggest function of reentrant lock is to avoid deadlock

Read write lock

Read / write locks maintain a pair of related locks, one for read-only operations and one for write operations. As long as there is no writer, the read lock can be held by multiple reader threads at the same time. Write locks are exclusive.

Reentrant read write lock

The reentrantreadwritelock object provides readlock () and writelock () methods to obtain read and write locks

Read locks can be held by multiple reader threads at the same time, while write locks can only be held by one writer thread at most

Usage of read / write lock: the frequency of reading shared data is much higher than that of modifying shared data In the above cases, using read - write lock to control the access of shared resources can improve the concurrency performance

If a thread already holds a write lock, it can hold a read - write lock again Conversely, if a thread already holds a read lock, it cannot hold a write lock until the read lock is released

You can call the newcondition () method of the write lock to obtain the condition object bound to the write lock. At this time, it is no different from the ordinary mutex lock However, calling the newcondition () method that reads the lock will throw an exception

example

Output as

As can be seen from the figure, multiple threads can read at the same time, but only one thread can write, that is, writing data and writing data are completed together.

summary

The above is all about re-entry lock and read-write lock in Java Concurrent Programming. I hope it will be helpful to you. You are welcome to refer to other topics on this site. If you have any questions, you can leave a message at any time. Xiaobian will reply to you in time. Thank you for your support.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>