How would I implement a SlotQueue in java?

I am currently writing a MUD server in Java and I use Jython scripting to make everything dynamic however there is one drawback with that.. exec() eval() and execfile() are very very expensive so I created a series of worker threads that run and stay in the Jython script and if I make any changes to any of the script it can reload them. On the event side it preforms pretty well and using a DelayQueue I was able handle 30,000 events/sec on a 2.5ghz intel celeron.

My problem has to do with the players.. Having one thread per player appears that it would be bad for performance as someday I may have hundreds. If I make one thread that handles all the IO and passes work into a queue read by the worker threads I run into a problem. There is a race condition where a user might attack someone and then go east, there is a chance two of the worker threads might get both requests at the same time and if the one to move the player easy executed fast the player would find himself in a room by himself trying to attack something that isn't there.

I need some way to guarantee that the commands players enter will be handled in the order that the player issued them.

My idea was sort of a slot queue. The requirements would be that each element (a player) could only enter one item in the queue at a time and could not enter another item until the worker threads were done answering that request.

The queue could be any type of queue and each player could have their own semaphore that inits to 1. When the io thread puts an object in there it acquire a permit on the player's semaphore and block if there was none. When the worker threads pull from the queue it could release a permit for that player's semaphore.

So my final question is.. in the long run should I fear any issues with this or will it even perform better concurrently than each user having its own thread which processes its own requests? Better yet, is there a better way to process commands from player in order without ruining all concurrency?

[2057 byte] By [deveka] at [2007-11-15]