Concurrent Programming in Java [reading note]
- Overriding run() in Thread vs. Using Runnable as an argument in Thread constructor
Default strategy is to define Runnable as a separate class and supply it in a Thread constructor. Isolating code within a distinct class relieves you of worrying about any potential interactions of synchronized methods or blocks used in the Runnable with any that may be used by methods of class Thread.
A small proof for Composition over Inheritance.
- Method Adapter patterns
Besides adapter, sub-classing, template method, the most flexible approach to before/after control is to define a class whose entire purpose is to invoke a particular method on a particular object. In the command object pattern and its many variants, instances of such classes can then be passed around, manipulated, and ultimately executed. In some other languages, function pointers and closures are defined and used to achieve some of these effects.
interface Callable {
Object call(Object arg) throws Exception;
}
- Collection Traveral
Lock the whole collection and operate on each item may be time consuming. JDK's Iterator is a fast-fail iterator that throw a ConcurrentModificationException if the collection is modified in the midst of a traversal using the version approach.
No comments:
Post a Comment