6
\$\begingroup\$

What is a reasonable number of threads for a simple 2D mmo in Java? Is it reasonable to have two threads per connection, one for the input stream and one for the output stream?

The reason I ask is because I use a blocking method on the input stream, and a workaround seems unnecessarily complex if I were to try to get around it without adding threads.

This is mostly for my own edification; I don't expect to have 5 million people playing it ever, or even 5, but I'm wondering what a good scalable solution is, and if this is reasonable for a small server (<30 connections).

\$\endgroup\$
  • 1
    \$\begingroup\$ Possible duplicate: gamedev.stackexchange.com/questions/7338/… \$\endgroup\$ – MichaelHouse Nov 11 '13 at 3:06
  • \$\begingroup\$ Could you have one central thread for output? (I have no idea about MMO design) Or even a thread pool of output workers? \$\endgroup\$ – ThorinII Nov 11 '13 at 5:35
  • \$\begingroup\$ @Byte56: That link seems to be about client threading, whereas this question seems to be about scalability of threads on server. Regarding number of threads in Java, search for Non-blocking I/O (NIO) versus Blocking I/O. E.g. stackoverflow.com/questions/4752130/… \$\endgroup\$ – msell Nov 11 '13 at 5:39
1
\$\begingroup\$

I think the better solution is to not reinvent the wheel here. Many Java libraries have been created to handle java networking & scalability, for example Netty or KryoNet. Another thing you either failed to consider or list was what kind of server architecture are you building out? Is this distributed, centralized, what? There is different approaches for different architectures, but these libraries are a good starting point.

Sorry if this doesn't answer "how many threads should I use", but these libraries will take care of all of this for you and it's really the better approach.

\$\endgroup\$
0
\$\begingroup\$

To quote coding horror: "Everything is fast for small N". Your N is small enough to make it really not matter just about anything should work. Your approach isn't memory efficient so it isn't going to scale to thousands of users.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.