Java Concurrency Programming and Scalability (iii)

Source: Internet
Author: User

Avoid hotspot domains

For example, a hashmap size. The optimization method is to add a counter. But for Concurrenthashmap. This counter needs to be changed for each put and remove when concurrency is performed on it. So in this class, this counter is called the hotspot domain, which is the bottleneck of scalability. So the way to avoid hot spots here is to apply a hash bucket with a lock fragment, and size in Concurrenthashmap enumerates each fragment and adds the number of elements for each fragment. This value is maintained by the lock for each segment.

methods to override exclusive locks

The effect of exclusive locks on performance is still relatively large, and not all synchronous requirements must be implemented using exclusive locks.

Readwritelock implements a locking rule in the case of multiple read operations and a single write operation.

Atomic variables provide a way to reduce the overhead of updating hotspot domains. For example, a static counter, a sequence generator, or a reference to a head node in a linked list data structure.

Monitor CPU Utilization

If the CPU fails to perform well during the test, it is not possible to get a relatively accurate result, and if the CPU is not fully utilized it needs to find out why, usually the following:

    • Not enough load
    • I/O intensive
    • External restrictions (Database or Web service)
    • Lock competition
Say no to the object pool

In earlier JVMs, the allocation of objects and the execution of garbage collection was very slow, so many developers used object pooling techniques to solve this problem. Now the object memory allocation in Java is faster than the C language. So if you're still using this technique and you're applying it in a concurrent program, you can already consider whether you should give it up. The cost of the lock competition problem is much greater than the overhead of object allocation. However, in J2ME or RTSJ, object pooling techniques are required to improve the efficiency of memory management or responsiveness management.


Reduce the overhead of the context

The 1th is to reduce the holding time of the lock. Because the shorter the holding time, the less the competition, the less context switching. Response in the process is not too long to request the service time.

Reduce the source of lock competition. For example, by transferring the I/O operation from the thread that handles the request to a dedicated thread.





Java Concurrency Programming and Scalability (iii)

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.