7. Debugging

1. Add traces around variables

2. Add traces around this object, toString() would print object state

System.out.println(" State : " + this);

3. Put a separate main method in each class. Inside it, you can put a unit test stub that lets you test the class in isolation

4. Put the stack trace for an exception object

e.printStackTrace();

You don't even need to catch an exception to generate a stack trace.

Thread.dumpStack();

5. Capture System.err and System.out to a file

java MyProgram 2>&1 error.txt

6. Run java with -verbose flag

7. Concurrency Issues

a. Thread Interference Errors (Race Conditions) - Thread interference errors can be avoided by synchronizing access to shared variables.

b. Memory Consistency Errors (Compiler\Processor optimization) - when different threads have inconsistent views of the same data. - use volatile keyword

References