3

Im making a turn based multiplayer game that works in rounds. Basically a player hosts a match and another person joins it and then the game plays out. My server will need to handle these matches and give updates to the clients every 5 seconds. I'd usually do this with a timer/a thread.sleep(). My problem is I'm unsure about how to do this for multiple matches simultaneously. I feel like using a thread per match would be ridiculous as it'd be sitting idle more than it would be doing anything.

My one idea is to only have one thread that updates every match in 5 second intervals. The only problem I feel there is with this is that when a lot of matches are going at once the games that are updated last could get quite a bit of a delay in them. (I don't know how critical this would be because of my game being time based but it seems primitive).

My other option was to use a ScheduledExecutorService (thread pool) which seems promising.

  1. Would I run into problems with my ScheduledExecutorService thread pool because I'm also running a thread pool for socket connections?

    2.Is there a better way to accomplish what I'm trying to achieve?

Just to clarify I'm looking for a way to basically call a function every 5 seconds per match. For example:

My game starts at 0 and your game starts at 2seconds. The server would call the update function:

For me: 0,5,10,15,20,25,etc...

For you: 2,7,12,17,22,27,etc...

Charles
  • 50,943
  • 13
  • 104
  • 142
Tukhes
  • 71
  • 1
  • 6
  • are these games written in Java Swing and running on the client side? – amphibient Jan 17 '13 at 01:43
  • this is network game ? for local network or ethernet ? what protocol you use for notifications (tcp, udp, etc ?) – iMysak Jan 17 '13 at 01:45
  • 1, no you wont have any problems with that other than resource management issues once your server gets busy. 2, It depends a little you could look at udp multicasing, also look at JMS topics. – BevynQ Jan 17 '13 at 01:52
  • JMS topics not guaranty strict time for receiving. its inappropriate for games – iMysak Jan 17 '13 at 01:54
  • They're written in Java. And they're not peer to peer hence why I have to host multiple matches in my one server application. Protocol shouldn't matter as all I'm asking is how to essentially have multiple scheduled tasks. Let's say I'm playing a game and you are as well. My game starts at 0 seconds and yours at 2. My server application will need to update my client at 0,5,10,15,etc.. and yours 2,7,12,17,etc... But now have hundreds/thousands of games going at once – Tukhes Jan 17 '13 at 01:54

1 Answers1

0

If I understand you correct "My one idea is to only have one thread that updates every match in 5 second intervals." will be better to run updates script each second, but updates match for which ID mod 5 == 0. this can reduce bad affect of HOT second.

Also thread.sleep() is bad and it not guaranty exactly strict time. thread.sleep(5000) — mean sleep for AT LEAST 5 second (but in bad case it can be 10 seconds and event more)

Look on Java Timer and http://www.javacodegeeks.com/2012/07/quartz-2-scheduler-example.html .

I think use TimerTask will good decision.

see also java timer for game

UPDate: for "topic" notifications looks on https://netty.io // if you don't use it yet :) with async non-blocking I/O for you will be enough just one thread for 10000 simultaneous connections. (on good instance 10 - 160 k connections its real on real commercial projects)

Community
  • 1
  • 1
iMysak
  • 2,170
  • 1
  • 22
  • 35