Guest User

Untitled

a guest
Apr 14th, 2016
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. //Say fps = 30 i.e. you must generate a frame every 1/30 seconds which is about 33 ms)
  2.  
  3. ScheduledThreadPoolExecutor pool = new ScheduledThreadPoolExecutor(1); //threadpool of size 1
  4.  
  5. //in your loop
  6. pool.scheduleAtFixedRate(<runnable task>, 0, 33, TimeUnit.MILLISECONDS);
  7.  
  8. You can of course cancel the pool or do an orderly shutdown whenever required depending on your loop conditions. Since the pool only has one thread, if a task overshoots its running time, the next task will be delayed and the rate of generation of frames would slow down affecting the game.
Add Comment
Please, Sign In to add comment