views:

62

answers:

2

I want opnion about multithreading design in java. Between thread-per-character and thread-per-map/zone. Which is more advantage (or other way) and game server can handles 3000+ players.

+1  A: 

Generally speaking, threads do not scale up. You will have a serious performance problem with 3000+ threads.

Arafangion
+12  A: 

Neither of those are going to give you great scalability. Threads take up quite a bit of space - e.g. by default the stack size is 256K on 32-bit systems, so for 3000 users, you will need 750MB just to start 3000 threads, and that's before they've allocated any memory for data to do actual work.

Thread-per-user will put a hard limit on the number of users available, which may be artificially low compared to what the server might handle with a different design. Thread per zone may be slightly better in this respect but it may also limit the number of zones.

Large numbers of threads have significant task switching overhead. To avoid this, I would try to remove "ownership" of threads from the design and use a work pool instead, such as an ExecutorService. The game processing is split into units of work which you then submit to the pool. The pool is usually set to allow the same number of threads as cores, so that you get the most efficient execution. (If threads are I/O bound, you can use more threads than cores.)

mdma
256K as a default is fairly conservative. I thought it was more like 1MB on windows? And 8MB on some linux systems? The point remains. +1!
Arafangion
Thanks for the vote. The default on windows is 256K, and larger on some other platforms, although I'm not sure as high as 8MB. See http://forums.sun.com/thread.jspa?threadID=649476, and also the -XX:ThreadStackSize hotspot param at http://java.sun.com/javase/technologies/hotspot/vmoptions.jsp
mdma