Java concurrent programming and scalability (3)

Source: Internet
Author: User

Java concurrent programming and scalability (3)
Avoid hotspot domain

For example, the size of a hashmap. The optimization method is to add a counter. But for ConcurrentHashMap. When performing concurrent operations on the counter, the counter needs to be changed each time put and remove. Therefore, in this class, this counter is called a hot point domain, which is the bottleneck of scalability. So the method to avoid the hot point domain here is to enumerate each segment and add the number of elements in each segment for the size in ConcurrentHashMap of the hash bucket that uses lock segments. And maintain this value through the lock of each segment.

How to replace an exclusive lock

The impact of exclusive locks on performance is still relatively large. Not all synchronization requirements must be implemented using exclusive locks.

ReadWriteLock implements a locking rule for multiple read operations and a single write operation.

Atomic variables provide a way to reduce the overhead of updating a hot-point domain. For example, static counters, sequence generators, or references to header nodes in the Linked List data structure.

Monitor CPU usage

If the performance of the cpu fails to be fully utilized during the test, the results cannot be relatively accurate. If the cpu is not fully utilized, you need to find out the reasons, usually the following:

Insufficient Load
I/O intensive external restrictions (database or web Service) Lock competition to object pool say no

In early JVMs, object allocation and garbage collection were very slow, so many developers used the object pool technology to solve this problem. Nowadays, the object memory allocation speed in java is faster than that in C language. So if you are still using this technology and the application is in a concurrent program, you can consider whether you should discard it. The cost of lock competition is much higher than that of object allocation. However, the object pool technology is required to improve the efficiency of memory management or responsiveness management in the j2_rts_j or rts_j.


Reduce context overhead

The first point is to reduce the lock hold time. Because the shorter the holding time, the less competition will occur, and the fewer context switches. The response should not be too long for the program to request the service.

Reduce the source of lock competition. For example, I/O operations are transferred from the request processing thread to a special thread.





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.