Using Java constructs for highly scalable applications

Source: Internet
Author: User
Tags in python

When the CPU enters the multi-core era, the performance tuning of the software is no longer a simple matter. Programs that do not have parallelization may run more slowly on new hardware than before. When the number of CPUs increases, it is wise for the chip manufacturer to reduce the frequency of CPU running in order to achieve the best performance/power ratio. Using Java to write multithreaded applications has been a lot simpler than C + + programmers. However, it is still not easy for multithreaded routines to achieve high performance. For software developers, it is not surprising to find that parallel programs are no faster than serial programs when testing, after all, the widely accepted guidelines for concurrent software development are often simplistic and arbitrary before the multicore era.

In this article, we will describe the general steps to improve the performance of Java multithreaded applications. By using some of the simple rules provided in this article, we can get scalable applications with high performance.

Why is performance not growing?

The large increase in the performance of multiple nuclear power is easily observed through simple tests. If we write a multithreaded program and add a local variable to each thread, we can easily see the multiple performance improvements that multi-core and parallelism bring. It's very easy to do, isn't it? We give an example of the reference resources. However, contrary to our testing, we rarely see such perfect scalability in real-world software applications. There are two factors that prevent us from getting the perfect extensibility. First of all, we are faced with theoretical constraints, followed by the software development process is often the problem of implementation. Let's take a look at the three performance curves in Figure 1:

Figure 1. Performance curve

As the perfect software engineer, we want to see the linear growth of the program's performance as the number of threads grows, which is the Blue line in Figure 1. The last thing we want to see is a green curve, no matter how many new CPUs we put in, there's no growth in performance. (The curve of performance degradation as the CPU grows) also exists in the actual project. The red line in the diagram shows that the usual 90-10 rules do not apply to scalability. Assuming that 10% of the computation in the program is only sequential, its extensibility curve is shown as a red line. As the graph shows, when 90% of the code can be perfectly parallel, in the case of 10 CPUs, we can only get about 5 times times the performance. If the task has parts that cannot be parallel, then in the real world our performance curve will roughly be in the gray area of Figure 1.

In this article, we will not try to challenge the theoretical limit. We want to explain how a Java programmer can get as close to the limit as possible, which is not an easy task.

What caused the poor scalability?

There are many reasons for the poor scalability, the most notable of which is the misuse of locks. There is no way, we are so educated: "Want to multithreading security?" Then put a lock on it. " Consider the notorious Global intepreter Lock in Python, and the Collections.synchronizedxxxx () series in Java, what's wrong with following the Giants? Yes, it's convenient to use locks to protect critical areas, and it's easier to ensure correctness, but locks also mean that only one process can enter critical areas, while other processes are waiting! If you observe CPU idle and software execution is slow, it is wise to check the use of locks.

For Java programs, the Java Lock Monitor in Performance Inspector is a good open source tool.

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.