4 Replies Latest reply on Nov 14, 2007 12:00 PM by manik

    Queue structure

    aditsu

      Hi, is it possible to implement a FIFO queue in the cache, so that multiple threads (or apps) can add and remove elements in order, without conflicting?
      As I understand it, each node has a (Concurrent)HashMap of child nodes and a HashMap of data. HashMap is not particularly known for keeping the order of the entries, so there doesn't seem to be a way to, for example, get the "first" element.
      Is there any feasible way to implement this kind of structure over the cache, without locking and writing excessively? I thought maybe I can keep a counter (also in the cache), and increment it for every element, then use the counter as a key in a HashMap (probably for child node names), then later use a for loop to get the elements in order, but it feels like reading and incrementing the counter could be troublesome.

      Adrian

        • 1. Re: Queue structure
          manik

          You could use timestamp Longs as keys in the Node and sort the results of a Node.getData() call.

          • 2. Re: Queue structure
            aditsu

            Well, the millisecond precision is probably not good enough, and if several machines are involved then it would require very precise time synchronization.
            On the other hand, if the queue is kept in a node, then won't every add/remove operation lock the whole node (thus the whole queue)?

            Adrian

            • 3. Re: Queue structure
              manik

              Java 5's System.nanoTime(), perhaps?

              Clock syncing to that level of precision may become a problem in a large cluster though.

              Isn't it a good thing that the queue is locked for add/remove operations?

              • 4. Re: Queue structure
                manik

                although with a high degree of writes I think what you really need is a JMS queue.