1. Principles and methods for developing high-performance Java programs:
The performance of a Java program is determined by a variety of factors, including the preliminary system structure design, underlying platform architecture design, coding in application development, and the use of skills, software configuration of the system platform, hardware performance of the system platform, and so on. Here we only provide some principles and methods for coding.
Compared with other aspects of Java, Java is most criticized in the performance field. Many people think that if Java wants to develop better, it must solve performance problems. However, the real fact is that Java is constantly improving in terms of compiler, JVM, and runtime optimization, and Java is running faster and faster. In fact, many performance problems are caused by poor design and have nothing to do with Java itself. In the face of poor design and coding, no matter how many runtime optimizations you use, no matter how advanced your compilation technology is, no matter how powerful your hardware platform performance is, the final code operation is still inefficient.
Similarly, no matter how powerful the runtime optimization technology, compilation technology, and hardware platform are, using technologies that have been proven to be beneficial to Java's running performance can always achieve very good benefits.
Precautions for optimizing program code:
1. Do not change it easily unless optimization is required;
2. Perform a careful test after the modification;
3. Among various JVM products, the optimization methods are different;
2. High-Performance Java coding principles:
(1) focus on design, data structure, and algorithms;
(2) do not rely on the compiler compilation optimization technology, correctly understand the code optimization technology during the runtime, and correctly use the code optimization during the runtime;
(3) Use the stringbuffer class to complete string connection;
(4) Minimize the object creation cost, that is, try to create lightweight objects;
1. reasonably design the object size
2. Inheritance depth of rational design classes
3. the constructor of the class should be short and lean
4. Do not create unnecessary objects
5. Delay object creation to the last moment, that is, the delay loading technology.
6. If possible, implement an Object Buffer Management pool to control the creation and recycling of objects within a reasonable range.
(5) reduce the impact of synchronization;
1. Identify whether synchronization control must be used properly and avoid it in design.
2. Control the scope of synchronization code to avoid affecting thread activity.
3. reasonably use lock variables to achieve thread separation, improve program concurrency, and further increase program Throughput
4. The objects used to lock variables should not be too many. To use multiple objects as lock variables, make sure to lock them in the same order from the beginning to the end in the code, otherwise, a deadlock may occur.
(6) use the stack variables and basic types as much as possible to complete necessary tasks;
(7) use static, final, and private methods to facilitate inlining;
(8) It is good to initialize the instance variable once. (that is, there will be an initial value in a certain sense, and there is no need to initialize it again below)
(9) Processing of cluster objects;
1. How can I traverse cluster classes correctly?
2. How to copy array elements?
3. How do I select the appropriate cluster class correctly?
(10) Reuse objects as much as possible;
(11) use of the delayed loading technology;
Of course, the above are both theoretical methods, and the practice needs to be done by yourself. The two codes do not know that the performance is better. javap looks at the bytecode execution and the steps. This is the most common method, it is the evidence that can best convince you!
Source www.boobooke.com/bbs-> J2EE area-> Blue Mountains work-secrets behind Java (Summary)