Performance comparisons of various synchronization methods (Synchronized,reentrantlock,atomic)

Source: Internet
Author: User

Synchronized
In the case of the resource competition is not very intense, occasionally there will be synchronization situations, synchronized is very appropriate. The reason is that the compiler usually optimizes synchronize as much as possible, and is also very readable, understandable by programmers who don't use more than 5 threads.

Reentrantlock:
Reentrantlock provides a variety of synchronization, such as time-limited synchronization, can be interrupt synchronization (synchronized synchronization is not interrupt) and so on. In the case of the resource competition is not intense, the performance is slightly more than synchronized. But when the synchronization is very intense, the performance of synchronized can fall dozens of times times. And Reentrantlock can still maintain the norm.

Atomic:
Similar to the above, in a less intense situation, performance is slightly worse than synchronized, and in times of intense, it can also maintain the norm. In intense times, the performance of atomic will be better than that of Reentrantlock. However, there is a disadvantage, is that only one value can be synchronized, a section of code can only appear a atomic variable, more than one synchronization is invalid. Because he can't sync between multiple atomic.

So, when we write synchronization, the priority is synchronized, if there is a special need, and then further optimization. Reentrantlock and atomic if not used well, not only can not improve performance, but also can bring disaster.

Performance comparisons of various synchronization methods (Synchronized,reentrantlock,atomic)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.