0

I need my main Thread to block for a specific amount of time. I am currently working on a 2D game and it's pretty laggy just because the Thread.sleep(2) sleeps for ~17 seconds and when you move the character, graphics are lagging slightly. Sometimes it's working just fine (when I ran the application about 10 times) and sometimes it's not. I know that there is a delay on the Thread.sleep(long milliseconds) method but is there any way to avoid it or an alternative? I already tried LockSupport.parkNanos((long)2e6) but that didn't change anything.

user263980
  • 31
  • 1
  • 6
  • 1
    When using `sleep` in a loop on windows, you always must schedule another thread on the background that sleeps forever, else your sleep is inacurate. – Ferrybig Feb 04 '16 at 11:21
  • 2
    Thread.sleep(2) sleeps for ~17 seconds? There's a huge inaccuracy involved here, but I'm not sure whether to blame the computer or the person using it. – Kayaman Feb 04 '16 at 11:21
  • 3
    @Ferrybig Could you provide a citation or reasoning for why the Windows thread scheduler is affected by a background thread always sleeping in this way? – nanofarad Feb 04 '16 at 11:22
  • 3
    you're doing something wrong, it would be better if you add some code – AdamSkywalker Feb 04 '16 at 11:22
  • Maybe what you're looking for is a Timer. You want something that can provide some regularity with its calls. Take a look at [Timer.scheduleAtFixedRate](https://docs.oracle.com/javase/7/docs/api/java/util/Timer.html#scheduleAtFixedRate(java.util.TimerTask,%20java.util.Date,%20long)). Just don't schedule it to trigger too frequently or all the time will be spent continually drawing frames. – Neil Feb 04 '16 at 11:28
  • I didn't use the Thread.start() or Thread.run() methods because it's my main thread and there are no other Threads running simultaneously, it simply started by running the application. – user263980 Feb 04 '16 at 11:28
  • 1
    @user263980 How much other work you're doing in this thread? How long does that work take? There's your answer. – m0skit0 Feb 04 '16 at 11:29
  • @m0skit0 The renderer, the logic processor and the input processor are all running on below 1ms, these are my only processes. – user263980 Feb 04 '16 at 11:31
  • The guessing game has gone too far already :) Show us your code. – m0skit0 Feb 04 '16 at 11:32

3 Answers3

0

Your are mentioning graphics, so I assume you are using either Swing of JavaFX. It's generally a bad idea to block a UI thread, as it has to do a lot of work it abstracts away for you. If you are using Swing, look into java.swing.Timer. And make sure you're running in the event dispatch thread (EDT), while you're at it :-).

Michael Bar-Sinai
  • 2,729
  • 20
  • 27
  • Well, Ferrybig's solution did the trick for me so I guess I'll stick to that one. I do not use Swing or JavaFX I am programming with awt. Yea I know it's not recommended but it's fine, I didn't run into serious problems... yet. – user263980 Feb 04 '16 at 11:37
0

What you are looking for is the 'game loop' pattern. This explains it pretty well... http://www.java-gaming.org/index.php/topic,24220.0

Romain Hippeau
  • 24,113
  • 5
  • 60
  • 79
0

If you want your game to be responsive, you should delegate logic and graphic rendering to different threads. Running everything in one thread makes this application highly unpredictable in matter of calculating Thread.Sleep() effect.

Also please specify what you are using, AWT, Swing, JavaFX? If you use AWT there could be serious problems with rendering smooth animations. I suggest starting with Swing even for very basic games/animations.

As it comes to running threads my favorite is:

  1. Creating new Class that implements Runnable with private fileds Thread thread and boolean flag
  2. @Override Run() to have something like try { while(flag) { refresh(); Thread.sleep(10) } }
  3. In this class create a method startRendering() that sets flag=true and starts internal thread thread = new Thread(this).start()
  4. Create stopRendering() that sets flag=false

This allows you to start and stop your animation in your logic thread properly and with ease (without Thread.Abort() and such)

badsamaritan
  • 407
  • 3
  • 9
  • Thanks for all the effort. I already implemented your idea a little bit different and it's now working. I worked alot with Swing in the past but I don't really like it to be honest that's why i am using awt at the moment. I know it's problematic but I think I can handle that, I am not a beginner ;) – user263980 Feb 04 '16 at 12:13
  • Thanks for feedback :) Good to hear you have solved your problem. – badsamaritan Feb 04 '16 at 12:15