Peter Haggar: Practical Java

•Use static, final, private methods to allow inlining
•To 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 from a getter methods
-clone mutable objects provided to the constructor
-impl deep clone if shallow copying is not enough
•Call super.clone() when impl clone()
•Other tricks to impl immutability
-Immutable interface
breakable by a simple cast
-Common interface or base class
not breakable by cast
interface IF{}
class Mutable implements IF{}
final class Immutable
implements IF{}
-Immutable Delegation class
not breakable by cast
useful when can not change src
•Do 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.
•Use care when calling non-final methods from constructors
•Prefer polymorphism to instanceof
•Use instanceof only when have to
avoid ClassCastException?
•Set object ref to null when not needed. Also could call System.gc()
Runtime rt = Runtime.getRuntime();
long mem = rt.freeMemory();
System.gc();
•Rules to follow when impl equals()
-check for comparison to this
-call super.equals() if any base class other than Object impl equals()
-prefer getClass() to instanceof
•Place try/catch blocks outside loops
•Do not use exceptions for flow contr
•Do throw exceptions from constructors
•Use StringBuffer not String for concatenation
•Minimize the cost of object creation
-use lazy loading
•Guard against unused objects
•Minimize synchronization
•Use stack variables whenever possible
•Uss static, final, and private methods to allow inlining
•Do not initialize instance variables
•Do initialize local variables
•Use primitive types for faster and smaller code
•for loop + get() is faster than Iterator, ListIterator, Enumeration
•Use System.arrayCopy for copying arrays
•Reuse objects whenever possible
•Use lazy evaluation
MULTITHREADING
•synchronization locks objects not methods or codes
•”synchronized static” locks Class object
•private byte[] lock = new byte[0];
is the cheapest object to create. It’s cheaper than Object o = new Object();
synchronized(lock){
//code to protect}
•Use 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.
•Aquire multiple locks in a fixed, global order to avoid deadlock
•Prefer notifyAll to notify since notify wakes up only one random waiting thread
•Use Specific Notification Pattern if you need to notify multiple threads in a particular order
•Use spin locks for wait and notify
public run(){
while(true){
synchronized(controller){
while(commands == null){//not if!
try{
controller.wait();
}catch(InterruptedException e){}
//do sth with commands
}
}
}
•Use wait and notifyAll instead of polling loops
•Do not reassign object ref of a locked object
•Do not invoke stop(corrupt data) or suspend(deadlocks) methods
•Terminate threads through thread cooperation
private volatile boolean stop;
public void run(){
while(!stop){
//do work…
}
//clean up…
}

This entry was posted in java. Bookmark the permalink.