Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (426)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (466)
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 933 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.

 

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

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (72 views)
2014-04-15 18:08:23

BurntPizza (68 views)
2014-04-15 03:46:01

UprightPath (79 views)
2014-04-14 17:39:50

UprightPath (65 views)
2014-04-14 17:35:47

Porlus (80 views)
2014-04-14 15:48:38

tom_mai78101 (104 views)
2014-04-10 04:04:31

BurntPizza (164 views)
2014-04-08 23:06:04

tom_mai78101 (260 views)
2014-04-05 13:34:39

trollwarrior1 (210 views)
2014-04-04 12:06:45

CJLetsGame (220 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!