Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (404)
games submitted by our members
Games in WIP (289)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Multithreaded or SelectNow?  (Read 792 times)
0 Members and 1 Guest are viewing this topic.
Offline squidi

Junior Newbie




Lord of the Pants


« Posted 2006-06-06 04:13:13 »

I'm writing a mud, and I've got it to a point where people can connect and walk around and all sorts of fun stuff. I'm having a little conceptual trouble with how to communicate between two threads. It actually works, but I don't understand enough about what I'm doing to know if it will always work, or if I'm doing something which may hurt me later. Working between multiple threads is new to me, and I'm wondering if it might just be easier to go single threaded.

Basically, what I'm doing is as each client finishes receiving a completed line of text, that text is put into a command queue for that client. Then the other thread goes through all the clients, checks if they have a command, and if they do processes it. I'm already aware that the client list may change in the middle, so I'm thinking I'll have to use a separate list to hold clients with commands pending. But the queue for each client is synchronized, so I don't think  moving the commands between threads is a problem. And while the client does change during the course of the program, the parts the world model uses and the parts the server uses are pretty separate, such that I don't think either one will step on the other's toes.

Basically, I'm asking if this approach will work, of if there is a better approach to this problem out there? I'm new to both multiple threads and to network programming, so I'm really in over my head and pretty unsure about everything.

Squidi
Visit my webcomic: www.squidi.net
Offline Sakazaki

Junior Newbie





« Reply #1 - Posted 2006-06-06 09:55:34 »

I think it works if all threads read/write commands queues in a synchronized environment, as you suggest.
If I understood your approach basically you have a server thread that pool all active clients queue, process each command, and send a response throw client connection.
The only thing to take care in such solution is to synchronize queues (both clients and server), well... it not soo difficult, but debugging can be a nightmare if something do not work as expected (there are something wrong in client threads? in server thread? in queues synchronization?).
When I developed a mud server I choose to do it in a different way.
Each player is an object that "speak" with the clinet throw a connection (so one player one thread as in your solution, but only for I/O), a player receive a command string and pass the command to a command manager with a reference to itself. No queue in the "client side", only a command queue in the "server side" (server thread).
When the command manager receive this instruction process it and, if there are some feedback for player, use the payer reference to perform back comunications.
It's a quite similar approach, but I have only one command queue and I don't have a thread that "pool" each active connection; my server thread waits command from players and process it in a First In First Out way.
I don't know if this is the "best way to do this"(TM), just an idea.
I think develop a mud environment is an interesting exercise, there are a lot of possibilities to do the same things  Grin

Have a good job  Wink
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (42 views)
2013-05-17 21:29:12

alaslipknot (51 views)
2013-05-16 21:24:48

gouessej (79 views)
2013-05-16 00:53:38

gouessej (79 views)
2013-05-16 00:17:58

theagentd (87 views)
2013-05-15 15:01:13

theagentd (81 views)
2013-05-15 15:00:54

StreetDoggy (123 views)
2013-05-14 15:56:26

kutucuk (147 views)
2013-05-12 17:10:36

kutucuk (147 views)
2013-05-12 15:36:09

UnluckyDevil (157 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.085 seconds with 20 queries.