{"id":3612,"date":"2010-05-10T07:58:00","date_gmt":"2010-05-10T14:58:00","guid":{"rendered":"http:\/\/palblog.fxpal.com\/?p=3612"},"modified":"2010-05-10T10:38:31","modified_gmt":"2010-05-10T17:38:31","slug":"findbugs-fix-bugs","status":"publish","type":"post","link":"https:\/\/blog.fxpal.net\/?p=3612","title":{"rendered":"FindBugs; fix bugs"},"content":{"rendered":"<p>This weekend, I came across <a title=\"FindBugs | SourceForge\" href=\"http:\/\/findbugs.sourceforge.net\/\" target=\"_blank\">FindBugs<\/a>, an interesting application that analyzes Java programs for potential bugs.\u00a0 It was created by <a title=\"William Pugh | University of Maryland\" href=\"http:\/\/www.cs.umd.edu\/~pugh\" target=\"_blank\">William Pugh<\/a> at the University of Maryland, who is a four-time JavaOne <a title=\"William Pugh | Sun JavaOne Hall of Fame\" href=\"http:\/\/java.sun.com\/javaone\/rockstar_wall_of_fame.jsp#wpugh\" target=\"_blank\">Rock Star<\/a>.\u00a0 Google, for example,\u00a0 has used his tool for finding problems in their Java code.<\/p>\n<p>FindBugs made plenty of suggestions for some code I&#8217;ve been working on, and some of them looked like things that I should fix.\u00a0 I found one thing in particular of interest.\u00a0 I&#8217;ve been using the following pattern for the lazy instantiation of singletons:<\/p>\n<p><!--more--><\/p>\n<pre><code style=\"font-size: larger;\">\r\nclass Foo {\r\n    private Helper helper = null;\r\n    public Helper getHelper() {\r\n        if (helper == null)\r\n            synchronized(this) {\r\n                if (helper == null)\r\n                    helper = new Helper();\r\n            }\r\n        return helper;\r\n    }\r\n}\r\n<\/code><\/pre>\n<p>This <a title=\"Double-Checked Locking | William Pugh\" href=\"http:\/\/www.cs.umd.edu\/~pugh\/java\/memoryModel\/DoubleCheckedLocking.html\" target=\"_blank\">article<\/a> explains that that approach doesn&#8217;t work because instruction reordering in the compiler may cause the helper variable to be non-null in a thread that didn&#8217;t create the object but the Helper instance to be not yet initialized. It goes on to explain that for static variables one can get around this problem by using a separate class.<\/p>\n<pre><code style=\"font-size: larger;\">\r\nclass HelperSingleton {\r\n    static Helper singleton = new Helper();\r\n}<\/code><\/pre>\n<p>Without any synchronization, the class loader makes sure that the singleton variable will be fully initialized before any other thread can access it.\u00a0 Also, the class loader won&#8217;t load this second class before it is needed.<\/p>\n<p>Anyway, this finding alone should get Java programmers interested in trying out FindBugs in their applications. For more information, take a look at the <a title=\"Mistakes That Matter | William Pugh, University of Maryland\" href=\"http:\/\/www.cs.umd.edu\/~pugh\/MistakesThatMatter.pdf\" target=\"_blank\">talk<\/a> William gave at JavaOne last year.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This weekend, I came across FindBugs, an interesting application that analyzes Java programs for potential bugs.\u00a0 It was created by William Pugh at the University of Maryland, who is a four-time JavaOne Rock Star.\u00a0 Google, for example,\u00a0 has used his tool for finding problems in their Java code. FindBugs made plenty of suggestions for some [&hellip;]<\/p>\n","protected":false},"author":114,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[170],"tags":[89],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/blog.fxpal.net\/index.php?rest_route=\/wp\/v2\/posts\/3612"}],"collection":[{"href":"https:\/\/blog.fxpal.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.fxpal.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.fxpal.net\/index.php?rest_route=\/wp\/v2\/users\/114"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.fxpal.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3612"}],"version-history":[{"count":11,"href":"https:\/\/blog.fxpal.net\/index.php?rest_route=\/wp\/v2\/posts\/3612\/revisions"}],"predecessor-version":[{"id":3615,"href":"https:\/\/blog.fxpal.net\/index.php?rest_route=\/wp\/v2\/posts\/3612\/revisions\/3615"}],"wp:attachment":[{"href":"https:\/\/blog.fxpal.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3612"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.fxpal.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3612"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.fxpal.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3612"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}