Archive for May 2013

SINGLETON in Multithreaded Environment

with 12 comments

  Most of the time, we are confined to sequential programming.However,several parts of our program can also be executed parallely by concurrent programming. In concurrent programming, the whole program is split into independent running tasks.Using multithreading, each of these independent tasks (also called subtasks) is driven by a thread of execution.The advantages of concurrency is faster execution,improving code design(espeically in Simulation Applications) etc.,

  To be simple, Unless multithreading had been added to Java, any concurrent Java programs wouldn’t have been portable to the Macintosh and similar platforms, thus breaking the “write once/run everywhere” requirement (Eckel,Bruce et al., 2005).

  However the real problem with this concurrency occurs, when each of the tasks begin to interfere with eachother.I mean at times,correct programs can also exhibit incorrect behavior. 

Let’s see what incorrect behaviour, a Singleton Class can execute in a mutithreaded environment.

   I have written the following code with respect to the MusicStore example that I have discussed in my previous blog( I have made variations in my Singleton class to signify the importance and disadvantages of each implementation.

private static MusicStre getMusicInstance(){
    musicInstance=new MusicStore();
return musicInstance;


  • In the above method, the instance is created via Lazy Initialisation.i.e.,the object is created only when needed.
  • Lazy Initialisation is usually done for Performance Optimisation.

Here let’s consider that two threads are accessing this class.

The flow goes this way,

  • Thread 1 enters the class
  • Thread 1 accesses the method getMusicInstance().
  • Initially the musicInstance is null , so the control goes inside the if()block to create new Music Instance for the class.
  • Consider thread 2 also enters the class parallely with thread 1.Here there are two chances,

         Chance 1:Thread 1 completing its process before thread 2 accesses the getMusicInstance method.If so, the musicInstance for thread 2 will not be null.Thread 2 gets the same Instance as Thread 1.
         Chance 2: Thread 1 has not yet completed its process(getting Instance). If so, musicInstance for thread 2 will be null.Therefore the thread 2 control goes inside the if()block to create new Music Instance for the class.



Here for Chance 2 the result will be as above.Thread 1 has new Instance and thread 2 also has a new Instance.It’s very obvious from the picture that the MusicStore class(supposed to be a Singleton class) cannot be called a singleton class because it has violated the behaviour by giving different instances.Therefore Thread Safety has not been achieved here.

Let’s see how to achieve Thread-safety.

1.Using Synchronized Block- LAZY Initialization:

The above problem of thread-safety can be achieved by using a Synchronized block.

public static MusicStore getMusicInstance(){
synchronized (MusicStore.class) {
   if(musicInstance==null){  musicInstance=new MusicStore();
return musicInstance;

Synchronized block allows only one thread to run at a time . The part of method that has to be synchronized is placed inside the block.Here Thread 1 comes in . It acquires the lock. Now the thread 2 has to wait till Thread 1 comes out of the synchronized block and releases the lock.

Here we do synchronisation, to get newMusicInstance object for threads having NULL music Instance.In the above case we can notice that, whether the music Instance of threads is null or not , the thread has to wait. It’s a waste of time for Threads which has musicInstance!=null.

To avoid this we use another null check condition before the synchronized block as shown below.

public static MusicStore getMusicInstance(){
   synchronized (MusicStore.class) {
     musicInstance=new MusicStore();
 }return musicInstance;


  • Thread 1 enters the method getMusicInstance(). Here initially musicInstance is null.
  • Now Thread 1 enters the synchronised block and acquires the lock. If no thread has passed the synchronized block earlier, the musicInstance of thread 1 still remains null.
  • Thread 1  gets the new musicInstance.
  • Thread 2 enters the method parallely with the thread 1.(Consider thread 1 has entered the synchronised block and has not finished complete execution)So, the music Instance of thread 2 will also be null.
  • Thread 2 tries entering the synchronized block.Since thread 1 has acquired the lock, thread 2 waits till thread 1 completes it’s execution and releases the lock.
  • Thread 1 releases the lock
  • Thread 2 enters Synchronized block.
  • NewMusicInstance got by thread 1. So musicInstance for Thread2!=null.So,thread 2 does not enter into if() block but will get the same instance as Thread 1,thus achieving thread safety.

This process of double checking is called DOUBLE CHECKED LOCKING PATTERN

Note: Here I have not synchronized the whole method but have synchronized only a part of the code .

Synchronization is useful to control access to resources within the same JVM. If the resources are present in different JVM then synchronized block alone is not sufficient, we will have to get an external lock .

2.Using Lock-LAZY Initialization:

public static MusicStore getMusicInstance(){
 final Lock lockObj=new ReentrantLock();
     musicInstance=new MusicStore();
return musicInstance;

This lock is a mutually Exclusive one(MUTEX).i.e, allows only one thread at a time.

Here the execution is the same as the synchronised method. However,this implemetation can used, when multiple JVM’s come into picture.

3. EAGER Initialisation:

private static final MusicStore musicInstance=new MusicStore();

private MusicStore(){

public static MusicStore getMusicInstance(){
return musicInstance;
  • Here Instantiation happens once when the class is called.It is a very straight-forward implementation.
  • If the object does not cost too high, then this would be a better approach.But when there are situations where, lazy loading is needed, then this approach cannot be used.

Every coin has its two sides.Likewise,all the above types of implementation has it’s own advantages and disadvantages. So,it’s all left upto the developer/designer to choose the right type of implementation based on the needs.

I would also like to add on that, if the proper designing of the Singleton class is not done and is used in applications related to Banking, DB resource sharing etc., then it would for sure cause a disaster.

Life is Simple just because Design is always around but designing that Design Pattern is not that Simple.


Written by thangaveluaishwarya

May 24, 2013 at 11:22 PM

Global Access Point – SINGLETON Design Pattern

with 6 comments

*Click on image to view it better


                          Design Patterns provides better recurring solutions for commonly occurring problems says Gang Of Four and SINGLETON is one such DESIGN PATTERNS.

There must be only one instance to a class and that instance must be the MAIN IMPORTANT GLOBAL POINT OF ACCESS to all other related Classes.

A Singleton Class must have 3 important things,
1. A private static Instance member
2. A private constructor
3. public static getInstance() – to get Instance Member specified in POINT 1

Before entering into Singleton, let us get into a quick discussion on CLASS ACCESS.

Access specifiers in Java are a real gift to the Java Developers because they are much like a CONTROL SYSTEM of our project.It is in our hands to decide what to expose and what not to expose to the world by using the right specifier at the right place.It is generally a good practice to make fields in the class as much PRIVATE as possible, if that field’s need is not outside the class.

Here l also want you to notice that a Class can be either PUBLIC OR DEFAULT and cannot be PRIVATE OR PROTECTED

  • Public Class can be accessed by any class from any Package
  • Default class can be accessed by the classes within the same Package.

Are you with me??? I don’t want to bore you anymore…Let’s go into a SINGLETON example now.


Consider , You have a MUSIC STORE in your IPod– That has many categories(as of now in program I have considered Devotional Songs and Pop Songs).


PopSongs and DevotionalSongs are two classes that implement the Interface IMusicAction .

Consider the interface has a method Play(), that is implemented in both the classes PopSongs and DevotionalSongs, but performing different playAction.


Hold On…! Have you noticed that both the classes are of DEFAULT type … and NOT Public.
Can you reason out why……????

Tat’s bacause I do not want these classes to be accessed out anywhere out of this package. i.e., It’s access is only within that package(com.aishu.practice.access).

Now let’s create the MUSICSTORE.We are going to make it Singleton – which means a SINGLE STORE(a SINGLE INSTANCE) having different song types(holding TONS OF INSTANCES)


Follow these steps,

  1. Create private static Instance member private static final MusicStore musicStoreInstance = new MusicStore();
  2. Create a CONSTRUCTOR and make it PRIVATE– Why PRIVATE ????

Make it Private because I don’t want to create the OBJECT OF MY CLASS anywhere else excepting ONLY INSIDE MY SAME CLASS ITSELF.

  1. Create public static method gets the Instance Member specified in Point 1
  2. Create Instances for the song types, Devotional and Pop Songs and PLAY ON

Here MusicStore class is PUBLIC. So no restrictions..let’s try calling it from a class outside this package and create instance of it to get our desired music played on…


In the main method , you will not be able to create new Object for MUSICSTORE class.You can only get its instance.How many ever times you try creating a new instance, the Instance still remains the same SINGLE INSTANCE (Marked in Pink).Now after getting this singleInstance, you can get any type of song Played On .


Don’t you think Singleton is much more related to SUBSET FUNCTIONS we learnt in Mathematics by drawing Venn Diagram during our school days….untitled




Okay…….In my next blog I will tell you, how Multi threads work on a Singleton Class.

Written by thangaveluaishwarya

May 17, 2013 at 1:00 PM

How to Reset PASSWORD for a “LOCKED” Oracle SQL- Developer Account

with 16 comments

ORA- 28000: the account is locked.”

Our mind becomes almost very restless when we have an important project to be completed within the next few hours and our Oracle DB gets locked due to attempting maximum number of incorrect password hits.

Error Msg

Now to unlock the account and reset the password you have 3 choices,

CHOICE 1: Wait till the LOCK_TIME is complete and then try the next maximum attempts.
CHOICE 2: Send a mail to the DB Administrator and wait till the Administrator Unlocks the account.
CHOICE 3: Follow the steps in this BlogPost 

Before following the below steps, please confirm that, you have logged into your Windows OS as an Administrator.

Now follow these steps,


Step 2: Type sqlplus /nolog
This command helps you to log into the sqlPlus Editor.
*Note- there is a space between sqlplus and /nolog

Step 3: Enter command CONNECT SYS as SYSDBA

This command helps you to connect to the SQL Server as Database Administrator.

Here SYSDBA is the ROLEName.

Oh wait, you are not yet connected… , SYS is the userName, now you have to enter the password to get connected

Step 4: Next Enter SYS_password
You will see the command “SQL>Connected”
Now you are the ADMINISTRATOR. So, you can control your SQL Server, which means you can control your DB Account even.

Let’s see how …

Step 5: Next Enter the following command

select username,password,account_status from dba_users;

This command retrieves the UserName , Password and Account status of all the accounts in your SQL Server.
You will also find your Oracle DB name that is “LOCKED” and the LOCK that is TIMED.

Account Locked

Step 6: Now our next step is to Unlock the account,
Enter alter user USERNAME identified by password;

Here my userName is AISHU. So my command goes like this

alter user AISHU identified by password;

STEP 7: Enter alter user USERNAME account unlock;

This command helps you unlock your account

You can check if your account has been unlocked by typing the command as in “STEP 5″. The Result should be as below.

Account Open

Step 8: Now Open your SQL Developer.

  1. Right Click on your DB Name.
  2. Click on Properties.
  3. Set your DBName,UserName Password. Configure the rest of the settings.
  4. Click on Test. If the Test proves SUCCESS, Click on Save .
  5. Click on Connect to connect to your database.

Hope you are happy in unlocking your account.

Written by thangaveluaishwarya

May 10, 2013 at 9:01 AM

Posted in Oracle, Technical Posts

Tagged with ,