Effective Java tenth concurrency avoid excessive synchronization of reading notes

Source: Internet
Author: User

Avoid over-synchronization

In order to avoid active failure and security failures, a synchronized method or code is fast, never give up control of the client.
Because it's alien, it's not controlled.

Move the foreign method call out of the synchronized code fast.
Create a Snapshot
Use concurrent collections, copyonwritearaylist. A Variant that belongs to ArrayList, where all writes are implemented by copying the entire underlying array. Because the internal data is used for no change, the iterations do not require locking and are very fast. However, if used extensively, performance will be greatly affected.

Foreign methods that are called outside the synchronization area are called open calls.
In addition to avoiding thinking, open calls can also greatly increase concurrency. Foreign methods may run for any length of time. If a foreign method is called within the synchronization region, other threads are denied access to the protected resource unnecessarily.

If a mutable class is to be used concurrently, this class should be made thread-safe, with internal synchronization, and you can gain significantly higher concurrency than locking the entire object externally.

StringBuffer instead of StringBuilder.

If you synchronize classes internally, you can use different methods to achieve high concurrency, such as split-lock, Split-lock, non-blocking concurrency control.

If a method modifies a static domain, access to the domain must be synchronized, even if it is often used only for a single thread.

Effective Java tenth concurrency avoid excessive synchronization of reading notes

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.