Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Hashtable with Vectors or HashMap with ArrayLists - please help  RSS feed

 
Sathvathsan Sampath
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
My application will be run under a mutilthreaded environ.
I have a job queue represented as a Hashtable. Each key in hashtable contains a vector as its corresponding value.
This is done to bring about one-2-many relations between key & values in the hashtable.

For eg:

So, what I am specifically intrested are:
1)Is the code above good enough to be run under multi-threaded environ.
My ans: YES indeed, becoz all the methods of hashtable and vector are synchronized. So, I need not do anything at all in the above code and it run with multiple threads.
Am i correct?
2) Does the above data structure affect my performace or is good enough? Would I be better off by using HashMap and ArrayList and then using syncronized wrapper classes like Collections.synchronizedList() instead of hashtable & vector in the above code ??
3) Any specific precautions I need to take in the above code?
Please can someone get back to me on this?
Many thanks in advance,
------------------
- Sathvathsan Sampath
[This message has been edited by Marcela Blei (edited June 06, 2001).]
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1)Is the code above good enough to be run under multi-threaded environ.
My ans: YES indeed, becoz all the methods of hashtable and vector are synchronized. So, I need not do anything at all in the above code and it run with multiple threads.
Am i correct?

No, you're not. Do a skeleton implementation of isAnyTaskPending() and ask yourself the question again.
2) Does the above data structure affect my performace or is good enough? Would I be better off by using HashMap and ArrayList and then using syncronized wrapper classes like Collections.synchronizedList() instead of hashtable & vector in the above code ??
That wouldn't make a significant difference in performance. I'd personally use the Collection framework any time, but that's a matter of taste. Also, consider replacing your Map by an array or an ArrayList/Vector.
3) Any specific precautions I need to take in the above code?
Other than (1), none that I can think of.
- Peter
 
Sathvathsan Sampath
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter,
Thanks a lot for a u r useful suggestion.
Yes, the isAnyTaskPending will not work correctly coz I need to grab a lock on this and then check whether each of the vectors/lists are empty or not.
I need to check whether all the 3 vectors in the hastable are empty atomically.
Therefore, instead of a vector, I could go for HashMap with a synchronized wrapper on it.
Also, as instead of a hashtable that stores 0,1,2.. etc as keys to those vectors, I might as well use an ArrayList with a syncronized wrapper and still use the 0,1, 2 as array indices.
Thanks for yout suggestions,


------------------
- Sathvathsan Sampath
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the isAnyTaskPending will not work correctly coz I need to grab a lock on this and then check whether each of the vectors/lists are empty or not.
You're clearly seeing the issue, but the solution may not be quite there yet. If you grab a lock on this, that won't prevent another thread from calling addTask, etc, which will mess up your test unless the odd inaccuracy doesn't matter that much. Everything would have to synchronize on this, in which case you'd be better of using unsynchronized data structures.
You hit upon one of my pet peeves, actually. Developers over-use Hashtable and Vector in multi-threaded code "because they are threadsafe". Yes, they are threadsafe by themselves, but the code constructed around them usually ends up being very, very thread-unsafe. Better start out using unsynchronized classes and be on your toes all the time.
- Peter
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!