Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use lock.lock ()

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "how to use lock.lock ()". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Lock.lock ()

There is a code in the Oracle document that describes the use of locks. Let's take ReentrantLock as an example. The code is as follows:

ReentrantLock lock = new ReentrantLock (); lock.lock (); try {/ / access the resource protected by this lock} finally {lock.unlock ();}

Q: why put lock.unlock () in the finally statement block?

A: in order to ensure that when an exception occurs during the execution of the current thread, the lock can still be released to avoid deadlock

Let's change the above code and see what the impact will be.

ReentrantLock lock = new ReentrantLock (); try {lock.lock (); / / access the resource protected by this lock} finally {lock.unlock ();}

There seems to be no problem, why is it not recommended to use it in the beginning? Let's talk about the problems that may exist first.

Exception stack loss

If you add a lock exception (never a bar) in the lock.lock method, it will enter the finally statement block to unlock it.

Follow up and see how it is handled in the lock.unlock () source code.

Lock.lock () throws an exception that may not have acquired the lock, so it is certainly not equal to compare the current thread with the lock thread in the unlock source code, so an IMSE (IllegalMonitorStateException) exception will be thrown.

I rewrote the logic of the ReentrantLock locking code and threw an exception in it to see what happens.

Final void lock () {/ / simulated locking failed to throw an exception if (true) {throw new RuntimeException ("wrong!!") ;} if (compareAndSetState (0,1)) setExclusiveOwnerThread (Thread.currentThread ()); else acquire (1);}

According to the following picture, you can see that the exception stack was "swallowed" when the lock was added, and disappeared quietly. Of course, this is just an example, but who can guarantee that an exception will not be thrown if the lock is not successful?

Real BUG

In the above code examples, lock is written on the first line of try, and the possibility of a problem is extremely low. Here to provide you with a negative example, do not have such similar behavior

In the sample code, the lock is put into the try statement block, and then the abnormal code may be generated before the lock is locked. This kind of code is cold, who uses the cold kind.

That's all for "how to use lock.lock ()". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report