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.
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...