title: Limit Frame per second
body: <p>Hi all I'm attempting to limit my FPS to 30 with the below code, but I'm not sure it's working. </p>
  
  <pre><code>while (runthread){
  
              c = null;
              timestart = System.currentTimeMillis();                         //Get time at start of loop for FPS calc
  
              try{
              c=mySurfaceHolder.lockCanvas();                                 //Set c (Canvas) to locked
  
              synchronized(mySurfaceHolder){
              framesskipped = 0;                                              // resetting frames skipped (as this is about to render to the screen, the frames skipped variable is only incremented when the onDraw() method is skipped, once it's run, reset it back to 0 for the next count
              doDraw(c);                                                      //Draw game world
              updateMenu();  //Update game logic
  
              }
              }
              finally{
                  if (c != null){
  
                  mySurfaceHolder.unlockCanvasAndPost(c);                     //Unlock and post
  
                  }
              }
  
              //work out timings
  
                  timeend = System.currentTimeMillis();                       //get end time for current frame (for FPS) 
                  frametime = timeend-timestart;                              //Set the frametime variable to the time the frame took to render &amp; update (end time - start time)
                  sleepfor = (int) (30.3030303030303-frametime);              // this is the time that the thread will sleep for if &lt;target time (1000/33 = apx. 30FPS
  
  
                  if (sleepfor&gt;0){                                            // If the 'sleepfor' variable is &gt;0 then set the thread to sleep for it's value (expressed in milliseconds)
  
                      try {
                          MainThread.sleep(sleepfor);                         //send thread to sleep for value of sleepfor (determined above).
  
                      } catch (InterruptedException e) {}                     //in case of exception
                  }                                                           //close if statement
  
  
                      while (sleepfor&lt;0 &amp;&amp; framesskipped&lt;maxframesskipped){   //if sleepfor is &lt; 0 (ie, frame took longer to render than target time and the maxframesskipped has not reached it's limit)
                          updateMenu();                                       //Update game logic only
                          sleepfor+=33;                                       //time to sleep plus the time frame took to render
                          framesskipped++;                                    //add one to framesskipped variable so this only skips a certain number of frames
                          frame=0;
                          }
  </code></pre>
  
  <p>Output from my logging that I place in my onDraw method.</p>
  
  <p><img src="https://i.stack.imgur.com/XerNW.png" alt="enter image description here"></p>
  
  <p>As you can see, it doesn't look to be limiting to 30fps, I'm not sure why, my figures must be wrong.</p>
  
  <p>Secondly, anyone have any ideas why my frame count drops down to as low as 5 every now and then?!</p>
  
  <p>Thank you</p>