Summary of Java Code optimization (III.)

Source: Internet
Author: User
Tags constant definition thread class

(35) separate operation for Close () of resources
Although there is some trouble, it can avoid resource leakage. We think, if there is no modified code, in case Xxx.close () throws an exception, then into the catch block, Yyy.close () will not execute, YYY this resource will not be recycled, has been occupied, such a lot of code, is likely to cause the disclosure of resource handle. And instead of the following wording, it is guaranteed that XXX and yyy will be close off anyway.

(36) for threadlocal before use or after use must first remove
All of the current basic projects use thread pooling technology, which is very good, you can dynamically configure the number of threads, you can reuse threads.
However, if you use the threadlocal in your project, be sure to remember to use it or remove it before using it. This is because the thread pooling technology is doing a thread reuse, which means that during code execution, a thread is used and is not destroyed but waits for the next use. Let's take a look at the thread class, which holds the Threadlocal.threadlocalmap reference.
The thread does not destroy means that the data in the threadlocal.threadlocalmap of the previous thread set is still present, so when the next thread reuses the thread, it is likely to get the data from the last thread set rather than the content it wants.
This is a very vague question, and if this is the cause of the error, there is no relevant experience or no solid foundation is very difficult to find this problem, so when writing code to pay attention to this point, which will give you a lot of work to reduce the workload

(37) Remember to replace the devil number in a constant definition, the presence of the devil number will greatly reduce the readability of the code, and whether the string constant is defined using a constant is dependent on the case

(a) long or long initial assignment, use uppercase l instead of lowercase l, because the letter L is easily confused with the number 1, this point is very detailed, it is worth noting

(39) All overridden methods must retain @override annotations
There are three reasons to do this:

    • (1) It is clear to know that this method is inherited from the parent class.
    • (2) GetObject () and Get0bject () method, the former fourth letter is "O", the latter fourth mother is "0″, added @override annotations can immediately determine whether to rewrite the success
    • (3) Modify the method signature in an abstract class, and the implementation class will immediately report a compilation error

(40) It is recommended to use the newly introduced Objects tool class in JDK7 to compare the equals of the object, directly a.equals (b), the risk of having a null pointer exception

(41) Do not use "+" in the loop to string stitching, and directly use StringBuilder constantly append

(42) Do not capture run-time exception classes defined in the Java class library that inherit from RuntimeException
Exception handling is inefficient and runtimeexception run-time exception classes, most of which can be circumvented by programmers, such as:

    • ArithmeticException can be avoided by judging whether the divisor is empty.
    • NullPointerException can be avoided by judging whether the object is empty
    • Indexoutofboundsexception can be avoided by judging the array/string length
    • ClassCastException can be avoided by instanceof keywords
    • Concurrentmodificationexception can use iterators to circumvent

(43) Avoid the use of random instances by multithreading, although sharing the instance is thread-safe, but can be caused by competition for the same seed performance degradation, after JDK7, you can use Threadlocalrandom to get random numbers
When multiple threads acquire random numbers at the same time, they compete for the same seed, resulting in reduced efficiency.

(44) Static classes, singleton classes, factory classes set their constructors to private
This is because static classes, singleton classes, factory classes, and so on, we do not need to externally add them out, after the constructor is set to private, it is guaranteed that these classes do not produce an instance object.

Summary of Java Code optimization (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.