简体   繁体   English

套接字上的Java Swing计时器同步

[英]Java Swing Timer Synchronization over Socket

I am making a Pictionary game between two instances of a class. 我在一个类的两个实例之间制作一个Pictionary游戏。 One instance is the host (host of the Socket), the other is the guest. 一个实例是主机(套接字的主机),另一个实例是访客。

During the program several Swing Timers are initialized and run. 在程序执行过程中,几个Swing计时器被初始化并运行。 These Timers need to run at the same speed on both devices. 这些定时器需要在两个设备上以相同的速度运行。 What's the best way to synchronize both devices? 同步两个设备的最佳方法是什么?

  • Synchronize start of timer over Socket 通过套接字同步计时器的启动
  • Synchronize every update of the timer over Socket 通过套接字同步计时器的每次更新

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). 一个简短的例子-计时器必须倒计时60秒(在60秒之后-> timer.stop() ,每隔一秒钟更新一次JLabel )。

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) 我是否需要通过套接字发送JLabel每个更新,而不是在客户机上初始化Timer 在两个设备上都初始化Timer,而是同步timer.start() (忽略连接的等待时间)

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. 连接的延迟几乎可以肯定会使两个JVM中的任何时序偏差相形见war,特别是因为总共只涉及60个计时器事件。 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. 如果两个参与者之一由于诸如GC,线程调度,虚拟内存交换等不可控因素而暂停,则可能会出现特殊情况。如果您想涵盖这些细节,则需要双向同步,因为这些事件可能发生在两端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM