NIO Packet Queue

In receiving data from an nio client/server app, what would be the best way to store these packet for use later?

Data comes in on a channel, a “Message” is created from it(if its complete) and the Message is put into a “MessageQueue”. Then another object loops thru the queue and acts on the message content.

What is the best object to use for that queue? Vector, HashMap, LinkedList, etc…?

…or… is it better to act on the data that just came in(if it’s complete) the moment it comes in?

Think multiplayer shooter.

If you can, I’d call selectNow() each frame and evaluate messages immediately. Lowest overhead, no sync issues, no MT.

If you cannot … Vector? Doesn’t matter I think.

Just remember that:

  • Vector is “not really supported; use ArrayList instead”
  • ArrayList falls to pieces with more than about 5,000 entries

So, just in case, I’d go with a LinkedList, which should scale perfectly to an (almost) infinite number of messages. This is assuming,of course, you only want to process them in FIFO order - hence the ArrayList has zero advantage, and LL is much faster.

Thanks a bundle guys…

OT

[quote]…to an (almost) infinite number of messages.
[/quote]
Isn’t (almost) infinite, still infinite? Or maybe that is near infinite? Infinity minus one? :wink:

[quote]OT

Isn’t (almost) infinite, still infinite? Or maybe that is near infinite? Infinity minus one? :wink:
[/quote]
:slight_smile:

I meant “for most practical intents and purposes, infinite. But the way that a linked-list works means that if you had a really, really big linked list you could get some nasty problems with where and how in physical RAM it was being laid-out, and what this does to iteration speed. And I suspect that if I don’t qualify this statement, then one of the 2 or 3 people on this forum who work with really large data structures is going to point out I’m wrong at the extreme end of the scale”

:P.

But I’m cutting back on hot air, so I just said “almost” ;D.

[quote]But the way that a linked-list works means that if you had a really, really big linked list you could get some nasty problems with where and how in physical RAM it was being laid-out
[/quote]
It doesn’t even take a big linked-list. All it takes is for the allocations of each node to take place in a slightly different area of memory. Every time you visit a new node, you end up not having the value in cache and wasting tons of cpu cycles. Any algorithms that work via random access (like a binary search) also perform like a dog on a LinkedList.

As a general-purpose container (inserts, removals, iteration, sorting, searches), ArrayList beats LinkedList hands down. For anything above 10,000 elements, you may want to start thinking about using your own specialized container. Where the container is serving as a request buffer, you will also realistically need to bound the buffer to something reasonable.

God bless,
-Toby Reyelts

I suggested Vector for it’s built-in sync.

(yes, I know about syncing ArrayLists … :-* )