Posts

Showing posts from September, 2010

Singleton - Double locking mechanism

Let us see the Singleton code which we use generally to create a singleton instance: public static synchronized Singleton getInstance() { if (instance == null) //1 instance = new Singleton(); //2 return instance; //3 } The code in Listing 2 works fine for multithreaded access to the getInstance() method. However, when you analyze it you realize that synchronization is required only for the first invocation of the method. Subsequent invocations do not require synchronization because the first invocation is the only invocation that executes the code at //2, which is the only line that requires synchronization. All other invocations determine that instance is non-null and return it. Multiple threads can safely execute concurrently on all invocations except the first. However, because the method is synchronized, you pay the cost of synchronization for every invocation of the method, even though it is only required on the first invocation. In an effort to mak...

How to use Derby database

Well, I directly jump into the subject matter instead of taking you through its history and detail; because you may be already aware or if not you can google it. Please follow these following steps:- 1. Download the Apache Derby binary distribution (e.g., v10.6) from the Apache Derby web site ( http://db.apache.org/derby/ ) and extract it to a directory of your choice to complete the installation. Say for instance you have extracted it into 'c:\derby' folder. 2. Set ‘DERBY_INSTALL’ as user environment variable in you computer first. 3. Go to the physical location of ‘DERBY_INSTALL’ .and start the bat file ‘C:\derby\bin\startNetworkServer.bat’ by double clicking it . This will start the database server. Minimize this started command prompt. 4. Now, open a new command prompt 5. set CLASSPATH=%DERBY_INSTALL%\lib\derby.jar;%DERBY_INSTALL%\lib\derbytools.jar;%CLASSPATH% 6. To run the sysinfo utility, type the following in a command window or shell: java org.apache.derby.tools.sysin...