Hashtable issues

Hi,

I am using a lock synch block for my singleton class where i am protecting a Hashtable, but surprisingly even after entering a key value pair and the size being shown as 1, when immediately i try to acces the hashtable it says the size is 0 and returns null for my key ..the code is as given below

public class UserController implements java.io.Serializable

{

private Hashtable<String,UserInterface> UsrStore = new Hashtable<String,UserInterface>();

private static UserController userCont= null;

String uname=null;

String mailId=null;

String phnNo=null;

String addr=null;

String pin=null;

String city=null;

private UserController()

{

//constructor private ...singleton instance

}

public int Store(String key,UserInterface val)

{

int ret = 0;

ReentrantLock lockPut = new ReentrantLock();

lockPut.lock();

try {

UsrStore.put(key, val);

System.out.println("ok put "+key+" val "+((ExUser)val).getUserName()+" size "+UsrStore.size());

ret = 1;

}

catch(Exception err)

{

System.out.println("Error while inserting into hash map "+err);

ret = -1;

}

finally {

lockPut.unlock();

}

return ret;

}

public UserInterface getObj(String key)

{

ReentrantLock lock = new ReentrantLock();

UserInterface EUW = null;

lock.lock();

try {

System.out.println("ok getting "+key);

EUW = UsrStore.get(key);

ExUser es = (ExUser)EUW;

if(es!=null)

System.out.println("the val is "+es.getUserName()+" size "+UsrStore.size());

else

System.out.println("the val is null"+" size "+UsrStore.size());

}

catch(Exception err)

{

System.out.println("Error while getting data from hash map "+err);

}

finally {

lock.unlock();

}

if(EUW!=null){

System.out.println("hooraah returning not null");

return EUW;

}

else{

System.out.println("shit returning not null");

return null;

}

}

public static UserController getInstance()

{

if(userCont == null)

return new UserController();

else

return userCont;

}

}

Store shows the correct value but when i call getObj i get nothing ..pls help

[2402 byte] By [pumplovea] at [2007-11-26 18:16:29]
# 1

I'm thinking your Singleton access is wrong.

public static UserController getInstance() {

if (userCont == null)

return new UserController();

else

return userCont;

}

This method never sets userCont to the instance so every call to getInstance()

will return a new UserController Object and hence an empty Hashtable.

try..

public static UserController getInstance() {

if (userCont == null) {

userCont = new UserController();

}

return userCont;

}

simonkent1a at 2007-7-9 5:50:03 > top of Java-index,Core,Core APIs...
# 2

> I'm thinking your Singleton access is wrong.

>

> > public static UserController getInstance() {

> if (userCont == null)

> return new UserController();

> else

> return userCont;

> }

>

>

> This method never sets userCont to the instance so

> every call to getInstance()

will return

> a new UserController Object and hence an empty

> Hashtable.

>

> try..

>

> > public static UserController getInstance() {

> if (userCont == null) {

> userCont = new UserController();

> }

> return userCont;

> }

>

Incidentally, this isn't thread safe. Imaging two threads, A and B. Say thread A calls getInstance() first. It will find that userCont is null and attempt to create a new instance by calling the UserController constructor. Now suppose the OS performs a context switch during this construction, switching execution to thread B. If thread B calls getInstance() the check for 'null' will still be true and this thread will begin construction of a second UserController instance.

This method needs synchronization or the userCont instance needs to be initialized where it is declared, i.e.:

private static final UserController userCont = new UserController();

Alternatively, double checked locking can be used along with the 'volatile' keyword, but be warned that this only works correctly in recent versions of Java. See:

http://en.wikipedia.org/wiki/Double_checked_locking_pattern

Niceguy1a at 2007-7-9 5:50:04 > top of Java-index,Core,Core APIs...
# 3
for those of you who are familiar with Bian Goetz, here is one from him on DCL http://www.javaworld.com/javaworld/jw-02-2001/jw-0209-double.html
kilyasa at 2007-7-9 5:50:04 > top of Java-index,Core,Core APIs...