{"id":567,"date":"2009-02-18T14:48:09","date_gmt":"2009-02-18T19:48:09","guid":{"rendered":"http:\/\/jianmingli.com\/wp\/?p=567"},"modified":"2009-02-18T14:48:09","modified_gmt":"2009-02-18T19:48:09","slug":"peter-haggar-practical-java","status":"publish","type":"post","link":"https:\/\/jianmingli.com\/wp\/?p=567","title":{"rendered":"Peter Haggar: Practical Java"},"content":{"rendered":"<p>\u2022Use static, final, private methods to allow inlining<br \/>\n\u2022To implement an immutable class<br \/>\n-declare all data private<br \/>\n-no setter methods<br \/>\n-set all data in the constructor only<br \/>\n-declare class final(no surclassing)<br \/>\n-clone mutable objects before returning a ref to them from a getter methods<br \/>\n-clone mutable objects provided to the constructor<br \/>\n-impl deep clone if shallow copying is not enough<br \/>\n\u2022Call super.clone() when impl clone()<br \/>\n\u2022Other tricks to impl immutability<br \/>\n-Immutable interface<br \/>\n\tbreakable by a simple cast<br \/>\n-Common interface or base class<br \/>\n\tnot breakable by cast<br \/>\n\tinterface IF{}<br \/>\n\tclass Mutable implements IF{}<br \/>\n\tfinal class Immutable<br \/>\n\t\timplements IF{}<br \/>\n-Immutable Delegation class<br \/>\n\tnot breakable by cast<br \/>\n\tuseful when can not change src<br \/>\n\u2022Do not rely on finalize() for non-mem rsc cleanup. You should impl your own public cleanup mechanism and put it in finalize() and always call super.finalize() in the finally block.<br \/>\n\u2022Use care when calling non-final methods from constructors<br \/>\n\u2022Prefer polymorphism to instanceof<br \/>\n\u2022Use instanceof only when have to<br \/>\n\tavoid ClassCastException?<br \/>\n\u2022Set object ref to null when not needed. Also could call System.gc()<br \/>\n\tRuntime rt = Runtime.getRuntime();<br \/>\n\tlong mem = rt.freeMemory();<br \/>\n\tSystem.gc();<br \/>\n\u2022Rules to follow when impl equals()<br \/>\n-check for comparison to this<br \/>\n-call super.equals() if any base class other than Object impl equals()<br \/>\n-prefer getClass() to instanceof<br \/>\n\u2022Place try\/catch blocks outside loops<br \/>\n\u2022Do not use exceptions for flow contr<br \/>\n\u2022Do throw exceptions from constructors<br \/>\n\u2022Use StringBuffer not String for concatenation<br \/>\n\u2022Minimize the cost of object creation<br \/>\n-use lazy loading<br \/>\n\u2022Guard against unused objects<br \/>\n\u2022Minimize synchronization<br \/>\n\u2022Use stack variables whenever possible<br \/>\n\u2022Uss static, final, and private methods to allow inlining<br \/>\n\u2022Do not initialize instance variables<br \/>\n\u2022Do initialize local variables<br \/>\n\u2022Use primitive types for faster and smaller code<br \/>\n\u2022for loop + get() is faster than Iterator, ListIterator, Enumeration<br \/>\n\u2022Use System.arrayCopy for copying arrays<br \/>\n\u2022Reuse objects whenever possible<br \/>\n\u2022Use lazy evaluation<br \/>\nMULTITHREADING<br \/>\n\u2022synchronization locks objects not methods or codes<br \/>\n\u2022&#8221;synchronized static&#8221; locks Class object<br \/>\n\u2022private byte[] lock = new byte[0];<br \/>\nis the cheapest object to create. It&#8217;s cheaper than Object o = new Object();<br \/>\nsynchronized(lock){<br \/>\n\t\/\/code to protect}<br \/>\n\u2022Use private data with an accessor method instead of public or protected data. Also clone the object if the accessor method returns a ref to a mutable object.<br \/>\n\u2022Aquire multiple locks in a fixed, global order to avoid deadlock<br \/>\n\u2022Prefer notifyAll to notify since notify wakes up only one random waiting thread<br \/>\n\u2022Use Specific Notification Pattern if you need to notify multiple threads in a particular order<br \/>\n\u2022Use spin locks for wait and notify<br \/>\npublic run(){<br \/>\n while(true){<br \/>\n   synchronized(controller){<br \/>\n     while(commands == null){\/\/not if!<br \/>\n\ttry{<br \/>\n\t\tcontroller.wait();<br \/>\n}catch(InterruptedException e){}<br \/>\n\/\/do sth with commands<br \/>\n     }<br \/>\n   }<br \/>\n}<br \/>\n\u2022Use wait and notifyAll instead of polling loops<br \/>\n\u2022Do not reassign object ref of a locked object<br \/>\n\u2022Do not invoke stop(corrupt data) or suspend(deadlocks) methods<br \/>\n\u2022Terminate threads through thread cooperation<br \/>\nprivate volatile boolean stop;<br \/>\npublic void run(){<br \/>\n\twhile(!stop){<br \/>\n\t\t\/\/do work&#8230;<br \/>\n\t}<br \/>\n\t\/\/clean up&#8230;<br \/>\n}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u2022Use static, final, private methods to allow inlining \u2022To implement an immutable class -declare all data private -no setter methods -set all data in the constructor only -declare class final(no surclassing) -clone mutable objects before returning a ref to them &hellip; <a href=\"https:\/\/jianmingli.com\/wp\/?p=567\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[14],"tags":[],"class_list":["post-567","post","type-post","status-publish","format-standard","hentry","category-java"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8cRUO-99","_links":{"self":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/567","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=567"}],"version-history":[{"count":0,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/567\/revisions"}],"wp:attachment":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=567"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=567"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=567"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}