对游戏类使用单例模式

对游戏类使用单例模式

问题描述:

我正在使用HTML5 websockets和Java作为后端的网页游戏。目前,为每个玩家创建一个游戏类的新实例,同时创建一个带有计时器任务的计时器,以运行游戏循环并以60fps的速度向前端发送更新。由于这些计时器在很多玩家玩的服务器资源上会非常繁重,所以我想在游戏类中应用Singleton模式并保留一组匹配。我没有为每个玩家创建一个计时器,而是创建1个单独的计时器,用数组中的每个匹配的for循环更新游戏循环。对游戏类使用单例模式

我不知道是否有更好的方法,因为我听说有很多缺点与单体模式,特别是单元测试。

假设我明白你的问题吧,你想用1个定时器所有的比赛,并为每场比赛使用for-loop来更新比赛。

这是一个可怕的想法。任何沿着那条线的任何形式的渲染都会影响整个服务器。如果第一场比赛的人向服务器发送大量数据,则会阻止该线程并减慢每场比赛的的FPS。

是的,定时器在服务器上很重。但是,如果一次有太多的活动匹配,则对所有匹配使用一个计时器会导致线程阻塞,因为单线程无法处理该高负载并以60 FPS运行。

设计任何给定游戏服务器的最佳方法是使用线程。

您可以使用Thread.sleep创建延迟并维护给定的FPS,并使用线程而不是定时器来减轻负载。 IT仍然很重,但使用线程比定时器要轻。

至于实际的线,这是它的一部分:

public void run(){ 


long lastLoopTime = System.nanoTime(); 
    final int TARGET_FPS = 60;//The FPS. Can be reduced or increased. 
    final long OPTIMAL_TIME = 1000000000/TARGET_FPS;//1 second = 10^9 nanoseconds 
    long lastFpsTime = 0;//Used to calculate delta 
    while(running){ 
     long now = System.nanoTime();//Get the current time 
     long updateLength = now - lastLoopTime;//get the time it took to update 
     lastLoopTime = now;//set the last time the loop started to the current time 
     double delta = updateLength/((double)OPTIMAL_TIME);//Calculate delta 

     lastFpsTime += updateLength; 
     if(lastFpsTime >= 1000000000){ 
      lastFpsTime = 0; 
     } 

     //Right here you place your code. Update the servers, push and read data, whatever you need 

     try{ 
      long gt = (lastLoopTime - System.nanoTime() + OPTIMAL_TIME)/1000000;//calculate the time to sleep, and convert to milliseconds 
      Thread.sleep(gt);//And finally, sleep to maintain FPS 
     }catch(InterruptedException e){ 
     } 
    } 
} 

类扩展条条都有一个名为running布尔值。布尔值允许线程停止而不必抛出异常。

您为每个比赛创建一个线程(这是一个重要的点,对每个玩家都这样做,并且你会杀死资源,为所有比赛做一个,除非你有一台超级计算机,否则你不可能维持60个FPS在匹配的数量上)),并且具有用于管理连接和匹配线程的主线程

为singelton设计模式最简单的规则是 -

  1. 创建一个私有构造
  2. 这个地方里面创建静态嵌段 - >要在整个项目仅实例化一个时刻的代码
  3. 创建一个静态方法,然后返回该对象
  4. 要完成步骤3,请声明一个私有静态实例变量。

考虑例如休眠的配置和buildSessionFactory方法,使本作singelton我的建议是

public class Singelton { 

    private static SessionFactory sessionFactory=null; 
    static { 
     Configuration configuration = new Configuration(); 
     configuration.configure(); 
     sessionFactory = configuration.buildSessionFactory(); 
    } 

    public static SessionFactory getSessionFactory() { 
     return sessionFactory; 
     } 

    private Singelton() { 
    } 
}