Friday, November 25, 2005

Case of the stingy JVM

Did you know this ?
The Java virtual machine specification defines the range of values for each of the data types, but does not define their sizes. The number of bits used to store each data type value is a decision of the designers of individual implementations.
-Bill Venners, Inside the Java Virtual Machine
When you create an int you would think that the JVM allocates 32 bits to it. But wait, it is possible that it may allocate fewer bits if the value you assign to the int does not require all the 32 bits. What will happen if you assign it a larger value? I do not know for sure, but I think it will relocate the int to some other location on the heap and give it more memory.
Now is that 'stingy' or 'smart' ?


This year Bangalore Linux is being hosted as FLOSS.IN from Nov 29th - Dec 2nd.
The speakers and topics seem very interesting. I am sure the event will be a great learning/exploring oppurtunity for the participants. Check the link for more details.

Wednesday, November 23, 2005

The Open-Closed principle for software design

The Open-Closed principle states that the design of a software system should be open for extension, but individual classes should be closed for modification. This means that a software should be designed such that new functionality can be added by adding new classes, and by subclassing abstract classes that have already been defined. However once a class is implemented it should be closed for modifications.

Such a design can be achieved by using the "Program To An Interface" concept. This concept states that the client class must reference abstract classes instead of concrete classes. This way subclasses can be added to the system as new functionality is added, without breaking client code. Care must be taken so as not to instantiate concrete classes in the client code. The creation of concrete subclasses must be handled by object factories. The dependency inversion principle ensures proper implementation of the open-close principle.

To summarize: we must achieve extensibility in a software system by adding new classes (subclasses) and not by modifying existing classes.

Here is an example to better understand this principle.
Spec: We want to design a software which will play a selected tune on a selected musical instrument.
The system will have the following classes:
public class Tune {

public class Instrument {
public abstract void play(Tune tune);

public class Guitar extends Instrument {
public void play(Tune tune) {
//play the tune using a Guitar

public class Harmonium extends Instrument {
public void play(Tune tune) {
//play the tune using a Harmonium

The client code is the code which uses these objects and invokes the play method.
public class Jukebox {
public void playMusic() {
Tune tune = getUserSelectedTune();
Instrument instr = getUserSelectedInstrument();;

Notice that the client code (Jukebox) does not instantiate a particular instrument subclass. It simply gets the instrument (superclass) reference from a method that is responsible for creating a concrete instrument . If the specification changes and we wish to add more instruments in our system, all we have to do is create subclasses of Instrument, override the play method, and modify the method getUserSelectedInstrument(). The client code in Jukebox does not need to be changed. Incorporating the change in specification was easy and minimal code had to be touched because we used the open-closed principle.

Friday, November 18, 2005

Synchronizing static methods in Java threads

While teaching Java threads, a student asked me a very interesting question: When a thread calls a synchronized method of an object, it obtains the lock of the object, however how can a thread call a synchronized method that is static? An object may not even have been created when the static method is called.

To understand this better, let us look at a simple example of a class which contains two synchronized method: one static and one non-static.

public class SynchedClass {
public synchronized aMethod() {}
public static synchronized bMethod() {}

When a thread calls SynchedClass.bMethod(), it obtains a lock on the class java.lang.Class that represents the SynchedClass. Whenever the JVM loads a class it creates an instance of java.lang.Class which represents all objects of the loaded class. An instance of the class Class is created even before any object of that class is instantiated. When a synchronized static method is invoked the invoking thread obtains a lock to the class Class.
Note that if a thread calls a non-static synchronized method and obtains it's lock, another thread could concurrently call a static synchronized method of the same object, because they are locked on different objects.