Java's implementation of PriorityQueue?

Hi,

In my networked car game, I am going to use a priority queue for incoming messages. That way, messages with higher proiority (player death, new player joined etc etc)
will be handled before those of lower priority such as position updates.

Is it a good idea to use java.utilPriorityQueue directly? Since this class works with Objects and Comparable.compareTo() the performance might be lower than if I would implement my own PriorityQueue using just “int” and “>” for comparison.
What do you think, would it be worth the effort?

Regards Anders

Probably not. ISTR there’s a bug in it in 1.5 as well, something to do with remove() using the comparator rather than ==.

Cas :slight_smile:

In the vast majority of cases, premature optimisation will cause you pain for little to no benefit. Unless you know for sure that it is a problem, don’t worry about it.
It’s more important that your game works correctly in the first instance, so using java.util.PriorityQueue is probably the best and easiest way to go for now.
If you get to the point that you have profiled your game and PriorityQueue is showing as a bottleneck, then you can worry about writing a drop-in replacement.

However, be aware of this. It caused me much puzzlement.

As an aside, wouldn’t it be better to process events in the order that they happened? For example, if you receive the movement updates that show some car has piled into a wall, and the death event for the now-flaming wreck in the same frame, you might end up drawing the explosion in the wrong place if you process the death event before the movement updates. Similarly, if death events have a higher priority than player join events, you might end up with the situation where a non-existent player dies!

code against an interface replace later if needed.

I have created a networked car game, and I simply used one game thread, and one thread for reading incoming messages. The message reader contained almost no logic, and just some set some variables or flagged events, that then were used in the gameloop. This may or may not be applicable for you depending on what sort of game you are doing.

Jojoh