Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hashtable problem

Status
Not open for further replies.

wijnanda

Technical User
Jun 4, 2001
2
US
I am working (for my study) on a multithreaded server program. The server is supposed to respond to a client request (in the form of a character) with a response, depending on the value of the request character. I need to store all client activity in a table, which I chose to be a Hashtable. The hashtable keeps an entry for each client, with info about the last request and the last response sent to the client. At each request, I retrieve the client entry from the hashtable with

ClientEntry cEntryIn = (ClientEntry) clientTable.get(client);

Then I check if this entry == null. If so, I create a new client entry, and write it to the hashtable with a put command.

When the entry not equals null, I need to update a few fields in the client entry, and write it back again to the client table. However, everything works fine with one client, but as soon as other clients become active, it looks as if the fields that I am writing to one client in some cases are read back with another client entry. I have looked and looked, and cannot figure it out.

Anybody experience with hashtables, who recognize this problem? Or maybe just some programming enthusiast who likes to dive into a bug?

I would be happy with any good ideas.

Wini
 
I'm sure the problem you are having is not specific to the type of data structure you are using (in this case a hashtable). It is probably a basic "race condition" problem, typical of multi-threaded application accessing common resources.
See, in this case you have a single data structure (your hashtable) and multiple-threads (I'm guessing your server is spawning a separate thread per client request) accessing the same hashtable. It will help if you enclose all the code that accesses any shared resource in a synchronized block.
 
You might be right, but the reason why I chose a hashtable is that it offers synchronization implicitly. Besides, there is only one thread currently that accesses the table. The server owns the table, and the thread that processes the requests, stores and retrieves the information from the hashtable.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top