• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Recommended data structure

 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
I hired a bunch of ninjas. The fridge is empty, but I can't find them to tell them the mission.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic