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

What is the difference between synchronized and ReentrantLock

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

What is the difference between synchronized and ReentrantLock? I believe many inexperienced people don't know what to do about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Similarities:

Atomicity, visibility, ordering, reentrability

Differences:

Synchronized is an unfair lock, and ReentrantLock can create fair and unfair locks.

Synchronized cannot interrupt a thread that is waiting to acquire a lock, and ReentrantLock uses lockInterruptibly to be aware of interrupting the operation of acquiring the lock.

ReentrantLock can realize locking of non-block structure.

ReentrantLock uses tryLock (time) to implement timing locks and avoid deadlocks.

When implementing the producer-consumer model, synchronized locks only all threads waiting for the lock to wake up Object.notifyAll (). ReentrantLock can use condition to reduce the granularity of waiting locks, and Condition.signal () can be used to wake up threads against conditional queues when waking up threads to improve efficiency.

While waiting for the lock, the thread using synchronized enters the BLOCKED state, and the thread using ReentrantLock enters the WAITING state.

Select:

ReentrantLock is not an alternative to built-in locks, but as an optional advanced feature when the built-in locking mechanism is not applicable.

In Java5, ReentrantLock can provide better competitive performance than built-in locks, but after Java6, the improved algorithm is used to manage built-in locks, resulting in smaller and smaller differences between the two, so it is preferred to use built-in locks, and ReentrantLock can be considered only if the built-in locks can not meet the requirements.

PS:

On fair locks, threads acquire locks in the order in which they make requests, but on unfair locks, you are allowed to jump the queue: when a thread requests an unfair lock, if the lock state becomes available at the same time as the request is made, then the thread will skip all waiting threads in the queue and acquire the lock. Take a scene in my life as an example. in a meat pie shop that I often go to at night, the meat pie is very slow, and we all need to pay for the number to wait. As a result, a customer came to order the meat pie, and when he ordered the meat pie, the meat pie was out of the pot. If it is an unfair scene, the clerk ignores the customer who is waiting in front of us and gives the meat pie directly to this customer.

There are four necessary conditions for deadlock: mutual exclusion condition, request and hold condition, non-deprivation condition, cyclic waiting condition, and tryLock destroys the non-deprivation condition.

After reading the above, have you mastered the difference between synchronized and ReentrantLock? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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

Internet Technology

Wechat

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

12
Report