Thursday, July 31, 2008

Updating the state of a Singleton Object after specific interval of time

I've created a Singleton Object which holds some values which are fetched from the database table. Following are the class specs:

1) All setter methods are private
2) All getter methods are public
3) One configuration method that sets all the values in the related attributes of class using their private setters

The need was to update the state of this singleton object when there are any changes made in the configuration table in the database. But to achieve this, I need to restart the server each time when any changes has been made in the configuration table.

So I created one WatchDog class in a thread that continuously changes the state of a singleton configuration object after a specific interval of time, & this time interval in-turn is fetched dynamically from the same configuration table from the database :)

Tuesday, July 22, 2008

Some experience with Log4j

I was doing some basic practice programs which implements Log4j for logging. I was getting following warning message on the console. Here I was using PropertyConfigurator to configure Log4j using the log4j.properties file.



I found the description about the above warning on many of the technical forums which gave a reason that this warning occurs when the log4j.properties file has not been found by the program in the specified path.

But if that was the case, I purposely tried giving wrong file path to the same program, and whoa I got the following exception:



Later I found that there was a small spelling mistake in my log4j.properties file, due to which it was failing to initialize the log4j system. So, when I corrected that mistake & tried to run the program, I got the expected output :)

Thursday, July 3, 2008

-C-O-N-C-E-P-T- Series : Java

Synchronizing a static method and its effect in the multi-threading environment:

Since static code can modify only the static data, you will protect a static data with synchronized keyword. We know that static data and methods have only one copy for all the objects of that class. Therefore, you only need one lock per class to synchronize static methods. Object locks are not required for static methods. Static methods use a class lock for synchronization. This special lock is also an object lock.

Consider following java code:


Fig1


Fig2

Both the figure above are self explanatory, but I would like to elaborate more on Fig2. When we synchronize an instance method, the thread accessing that method acquires "Object Lock" on the object such that no other thread can access that method unless it completes its execution. Now for "synchronized static" method, it acquires "Class Lock" on that class. This class lock also implicitly has object lock, it's because as non-static methods can modify static variables, so we need to also put a lock on synchronized instance variables & this is internally done by the class lock.
Hence if one thread is executing a static synchronized method of a class then it holds a lock on all other synchronized methods of that class and in effect no other thread can call any of the synchronized static methods of that class until this current thread releases the lock.

This concept is really very interesting to learn :)

Stay tuned... :)


_________________________________________________________________________________________________________________

"Look at the sky. We are not alone. The whole Universe is friendly to us and conspires only to give the best to those who dream and work."

- Dr. A P J Abdul Kalam
_________________________________________________________________________________________________________________