In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "what is ReentrantLock". In daily operation, I believe many people have doubts about what ReentrantLock is. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "what is ReentrantLock?" Next, please follow the editor to study!
When codereview, I saw that someone created a reentrantLock of static to use, and only used lock () and unlock (). What must be wrong? : (
I asked myself: what can I say about ReentrantLock? Then I went to read the API document. :)
ReentrantLock is a queryable, timed and interruptible locking mechanism. I don't understand what is unconditional. )
It solves the problem of built-in locks (used by synchronized): the thread waiting to acquire the lock cannot be interrupted and the lock must be released in the block of code that acquired the lock.
1) "searchable" is done through tryLock (). For built-in locks, once you try to acquire a lock, you will be block until you get the lock. This blocking method corresponds to the lock () method of ReentrantLock.
2) timing means that you can pass a timeout to the tryLock () method.
3) if the lock is block when the lock is acquired by the lockInterruptibly () method, it can be terminated by thread interrupt.
4) Don't forget finally {unlock ();}
5) Fair locks can be created by constructing parameters.
6) only if you really need: interruptible, timed, querable, fair queue, non-block structure lock is reconsidered, otherwise use synchronized!
7) ReentrantLock also has some other methods such as getHoldCount (), getQueueLength () and so on, which are good for non-concurrent library developers. I may not need these API for the rest of my life.
Going back to code review, it seems that my confusion is correct.
Here are the methods defined by the Lock interface:
At this point, the study of "what is ReentrantLock" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.