jdepypere
jdepypere

Reputation: 3553

Java Swing Timer Synchronization over Socket

I am making a Pictionary game between two instances of a class. One instance is the host (host of the Socket), the other is the guest.

During the program several Swing Timers are initialized and run. These Timers need to run at the same speed on both devices. What's the best way to synchronize both devices?

A short example - a Timer has to count down 60 seconds (after 60 seconds -> timer.stop(), every second a JLabel is updated with the amount of seconds).

Do I need to send every update of the JLabel over the socket and NOT initialize a Timer on the guest or initialize a Timer on both devices but synchronize the timer.start() (ignoring the latency of the connection)

Upvotes: 1

Views: 153

Answers (1)

Marko Topolnik
Marko Topolnik

Reputation: 200168

The latency of the connection will almost certainly dwarf any timing deviations in the two JVMs, particularly since there are a total of just 60 timer events involved. Therefore I suggest no network sync during countdown.

The exceptional case may appear if one of the two players experiences a pause due to uncontrollable factors like GC, thread scheduling, virtual memory swapping, etc. If you wanted to cover for those particulars, then you would need two-way synchronization because these events may happen at either end.

Upvotes: 1

Related Questions