Tuesday, 28 April 2009

The Recursive Mechanics of java.lang.ThreadGroup

A ThreadGroup is a set of threads. Containment - a ThreadGroup can contain other ThreadGroups (which will be like "subtrees" of the original ThreadGroup). Threads within a group can share properties e.g. setMaxPriority will set max priority in group (method is called recursively, for every ThreadGroup in the ThreadGroup). A thread may only access information about its ThreadGroup.

ThreadGroup has a final checkAccess method to determine if the currently running Thread has permission to modify the ThreadGroup. If not, a SecurityException is thrown.

BeanInfo class - to control behaviour of new components/beans in Visual Editor

Implement java.bean.BeanInfo class to provide information about your bean. SimpleBeanInfo is a support class to make it easy to make BeanInfo classes. It has a method getBeanDescriptor which by default returns null. A BeanDescriptor provides global information about a bean. This is needed by builder tools.

Other critical things to know about JavaBeans:
1. Beans use events to communicate with other beans. Beans wanting to receive events (listener beans) register interest with source beans (which fire events).
2. Persistence enable JavaBeans to save their state so they can be restored later.

JavaBean configuration may be defined in a java.util.ResourceBundle object (which contains locale-specific objects). When a JavaBean needs a local-specific resource it can load it from a ResourceBundle for the specific locale. This has been around since JDK 1.1 and is consequently a very basic building block of Java.

The Legacy Oswego Libraries and their Manifestation in Modern Day Java

The CopyOnWriteArrayList of Java 1.5 has its origins in the legendary Oswego by Doug Lea. This is a thread-safe ArrayList implemented by (expensively) copying the underlying array for every mutative operation. It implements the generic List interface ("sequence" interface).

Another oswego staple imported into 1.5 is the ThreadFactory, an object that creates new threads on demand. The newThread method takes a Runnable interface and returns a Thread.

The Stronghold of Mark Reinhold - Java Collection Classes

Yoda may be a master Jedi, but Mark Reinhold is the primary implementor behind many of the Java collection classes. He has also been involved in the area of NIO high-performance APIs.

A recent interesting class of Mark's I stumbled upon recently (using jdb and typing 'classes' to pull a list of the currently known classes) is the PreHashedMap.java collection class in "package sun.util". This is build on top of java.util.AbstractMap (a skeletal implementation of the Map interface) and has been in orbit since JDK 1.5.

PreHashedMap use memoization to store precomputed hashes. Hashes are computed as the hashCode of an Object, right shifted by a fixed offset and binary AND'ed with a mask. The mask and offset are configured at startup in the constructor of the PreHashedMap.

Sunday, 26 April 2009

PMD - "Project Meets Deadline"

PMD, also known by its backronym "Project Meets Deadline", is a static code checker for Java (kind of a Java lint if you will) currently on version 4.2.5.

You run pmd via bin/pmd java-source-file text rulesets/unusedcode.xml, as an example. There are lots of different rulesets and you can even roll your own ruleset. It is quite interesting to see how pmd defines rules.

I ran pmd on some of my own modest programming efforts using options: basic,imports,unusedcode and found no problems.

Tuesday, 7 April 2009

The final keyword in Java can be applied to almost anything

The final keyword can be applied to many types of "code objects" in Java, classes, methods and variables. For example:

final class - implies that the class that cannot be subclassed
final method - implies that the method that cannot be overriden
final value - implies that the value that cannot be reassigned (but is NOT, I repeat NOT, immutable)

final has nothing to do with immutability. It also has no meaning in packages (unless I suppose all the containing classes are final).