Skip to main content

Posts

Showing posts from November, 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' ?

FLOSS.IN

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.

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 exten

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. N