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.