Explanation of A findbugs check result

Source: Internet
Author: User

The prompt message of findbugs is:Incorrect lazy initialization and update of static field com. xxx. yyy. image. ImageLoade...

The initialization is delayed and static fields are updated incorrectly.

Theoretically, static fields should be initialized during class loading. For example:
Private static Xxxx yy = new Xxxx ();
Or write in the static block:

Private static Xxxx yy;

Static {
Yy = new Xxxx ();
}

Why is there a risk of problems detected by findbugs?
This static field will be read by a thread:

Private class ImageFetcher implements Runnable {

Multiple lines of code are omitted here.

Public void run (){

Multiple lines of code are omitted here.

// TODO Cyril: Use a AndroidHttpClient?
Bitmap = BitmapFactory. decodeStream (inputStream, null, (mOptions = null )? SDefaultOptions: mOptions );


The startup condition of this thread is:
Public Future <?> LoadImage (String url, ImageLoaderCallback callback, ImageProcessor bitmapProcessor, BitmapFactory. Options options ){
Return sExecutor. submit (new ImageFetcher (url, callback, bitmapProcessor, options ));
}

Non-static functions can avoid this risk. Fortunately, this bug will not become a real bug.
If it is a static function, this problem is serious. If it is a static function, the external class can start the thread. At this time, the thread will access an uninitialized static member. What will happen ?).
Findbugs found this kind of "sign" and prompted it. However, it is not a bug, but the risks are indeed hidden.
After all, a tool is a tool that cannot be intelligent to the extent of manual analysis. As for the current issue, the above explanation should be reasonable. For reference.

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.