This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Recommended data structure Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Recommended data structure" Watch "Recommended data structure" New topic
Author

Recommended data structure

Ranadhir Nag
Ranch Hand

Joined: Mar 09, 2006
Posts: 138
I need to select the most apt data structure to hold a set of xml strings.
These strings wouldbe dispatched to MQ by a set of threads - each thread deleting off the string it has picked up, from the container.
Moreover,when the pending number of messages reduces below a threshold - the container is replenished with a fresh set of additional messages.

Given this scneario where every message read from the container ,also results in a write(deletion) - whats the best available option.
I was planning to use ConcurrentHashMap(or a derived ConcurrentHashSet) - but think HashTable to be equally apt.

I will be doign a perf tes tfor both - but wanted to get an opinion in any case.
A completely fresh line of thinking (data structure) is most welcome too.

Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

Do you actually need the container to enforce the uniqueness? If not, one of BlockingQueue implementations would probably fit the bill nicely.

If you do need to use a set, it might be better to explain why.
Ranadhir Nag
Ranch Hand

Joined: Mar 09, 2006
Posts: 138
NOt exactly sure how a blocking queue may help in this case - maybe a task queue with threadpool?Though not sure how.
The idea that I needed help on is whether a used-case like this - where every read is accompanied by a removal of the read data from the container is a good case for ConcurrentHashmap.
This is considering the fact taht a set of threads will be oeprating on the container at any given time.
Or is it better to go with a HashTable(fully synchronized).
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Ranadhir Nag wrote:NOt exactly sure how a blocking queue may help in this case - maybe a task queue with threadpool?Though not sure how.
The idea that I needed help on is whether a used-case like this - where every read is accompanied by a removal of the read data from the container is a good case for ConcurrentHashmap.


No, a Map of any sort is a container for accessing data based on a Key. A Queue is the perfect type of container for a Read/Remove in an orderly fashion. The method you use is remove(). As Martin said, one good sub-family of Queue is a BlockingQueue. These allow consumers (the threads taking things from the collection) to wait until there is data available, and for producers to wait until there is room in the Queue. You would use take() and put() for these tasks respectively. The BlockingQueue implementations are nice in multi-threaded environments because the consumers and producers work at different ends of the Queue, reducing the synchronization needs, and also have built-in behavior for what should happen in cases of empty or full situations.

Ranadhir Nag wrote:This is considering the fact taht a set of threads will be oeprating on the container at any given time.
Or is it better to go with a HashTable(fully synchronized).


No, don't use Hashtable. Firstly, it isn't fully synchronized, to use it safely you will have to add your own synchronization. Second, the data structure doesn't fit your requirements as specified so far. And third, if you do need a Map interface then any other Map implementation in the java.util.concurrent package will do a better job at thread-safety and efficiency than Hashtable.

So: why do you think you need a Map? Do you have Key/Value pairs? So far your description does not mention any. Do you need uniqueness in the values stored in the collection?


Steve
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Recommended data structure