Should I use Thread.Sleep() for my program not to occupy the processor?

Asked

Viewed 89 times

-1

I have a small group of classes that implement a game of Snake with two players, the game is working as expected. But I’m using one while infinity that is operating at the maximum speed that the JVM allows, so leaving the application open for 10 seconds already starts to give problem pro processor.

Currently it is like this:

    public void tick() {
        if (p1.snakeBody.size() == 0) {
            p1.criarSnake(new Right(), new Coordenada(10, 10));
            p2.criarSnake(new Left(), new Coordenada(30, 5));
        }
        ticks++;
        if (ticks > 200000) {   //aqui está o problema
            ticks = 0;
            p1.proximaPosicao();
            p2.proximaPosicao();
            if (p1.snakeBody().size() > 15) {
                p1.snakeBody().remove(0);
                p2.snakeBody().remove(0);
            }
        }

    /**
     * Cria a Thread que inicia o jogo
     */
    public void start() {
        Thread t = new Thread(this, "game loop");
        t.start();
    }

    /**
     * O jogo fica rodando enquanto os dois jogadores tiverem mais de zero vidas.
     */
    public void run() {
        while(p1.vidas != 0 && p2.vidas != 0) {
            tick();
            repaint();
        }
        JOptionPane.showMessageDialog(null, "\nThe winner is: " + p1.vitorioso(p2) + "!");
        System.exit(0);
    }

But in this way the computer needs to calculate loops and conditionals 200000 times for just one move! I read the documentation on Threads, but there are some competition things that I didn’t understand very well, I think what I’m looking for is simpler.

You can use the method Thread.sleep() to prevent the processor from working so hard for so little?

  • 4

    What you want to do will only make the situation worse. You cannot stay in a loop doing this all the time, just do it when necessary. The whole architecture is wrong.

  • Do you have any reading recommendation? I would like to learn to architect correctly

  • I don’t remember anything about that.

  • don’t have a mode to only run after a timer? Kind to do once every 250ms

  • Yes, but I don’t know Java well enough to tell you about.

  • 1

    I don’t know if it’s what you’re looking for, but have you tried using one Timer?

Show 1 more comment

1 answer

0


This tick count is unnecessary. As Maniero said, the architecture is wrong. Also this thread there is unnecessary. It is only necessary to call (schedule) repaint() in the Event Dispatch Thread (EDT) periodically.

You can use the Thread.sleep() after the repaint(). It’s a way of doing it. That’s right? It’s not. It’s a scam.

I do not know how to answer to you exactly how to do, because I do not know much game programming. hkotsubo well remembered about Timer in the comments, I believe that is a valid solution.

I think you should look for a solution that is more robust and efficient. I suggest searching for games in Java or Java Swing or Javafx or ask https://gamedev.stackexchange.com/ on how to make the canvas drawing loop.

I previously suggested the ExecutorService and schedule (Schedule) the execution of a drawing event on the screen every X milliseconds, but I’m not sure if it would be a good solution. I believe the Timer is a better solution.

  • There are things that only testing, repaint would probably help, but nothing like a test by the AP itself, even to see what rate affects, it is worth remembering that the repaint, as well as other events in a "game" should isolate specific features to prevent one from having to wait for another, such as user control, direction flags and the "frames" update itself, but this depends a lot on what is done, I’m not from the gaming area to state everything and whether there is any "cake recipe" for such.

  • I have a certain notion because I’ve played with game programming and multithreaded systems that were looped, so the answer is not without basis. So much so that I thought it was too much to downvote her, whoever she was, turns out she’s not wrong. So I don’t think just testing, are suggestions that work, is an experience-based response.

  • I also got to use the java.util.concurrent.Executors To control the competition, if there are multiple operations from different sources, maybe in a multiplayer game help, but I’m just speculating, in my case it was an HTTP server that I created in Java. PS: the downvote is not mine, I do not know what the motivation also.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.