Better way to make a thread sleep

2019-07-15 01:44发布

问题:

I've been doing a tutorial on 2D graphics in Java 8, when NetBeans gave me a hint that doing Thread.Sleep would affect performance. However, though I've been able to find several better ways, I haven't been able to find a way to include them without messing you the code.

package platformer;

import java.awt.*;
import javax.swing.*;
import java.util.Scanner;

@SuppressWarnings("serial")
public class Platformer extends JPanel {
    int x = 0;//Sets the starting coords. of the ball
    int y = 0;
    private void moveBall() {//How much the ball moves by
        x = x+1;
        y = y+1;
    }
    @Override
      public void paint(Graphics g) {//Essentially al the graphics functions
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.fillOval(x, y, 30, 30);
    }

    public static void main(String[] args) throws InterruptedException {
        Scanner reader = new Scanner(System.in);
        Scanner reader1 = new Scanner(System.in);
        System.out.print("Enter an x-value for the window (whole numbers only): ");
        int setx = reader.nextInt();
        System.out.print("Enter a y-value for the window (whole numbers only): ");
        int sety = reader.nextInt();

        JFrame gameFrame = new JFrame("Sample Frame");//Makes the window variable w/ the name in quotations.
        Platformer game = new Platformer();//'Copies' the graphics functions above into a variable
        gameFrame.add(game);//Adds the above variable into th window
        gameFrame.setSize(setx,sety);//Sets the resolution/size of the window(x,y)
        gameFrame.setVisible(true);//Makse the window visible
        gameFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//Makes the window close when the close button is hit

        while (true){
            game.moveBall();
            game.repaint();
            Thread.sleep(10);
        }
    }    
}

I'm wondering how I can include a better way to make the thread sleep in a loop, or otherwise make NetBeans just do the loop.

回答1:

Instead of using Thread.sleep(10) in a while(true), you can use ScheduledExecutorService to invoke moveBall() and paint() for every 10 seconds as shown below which gives much cleaner way as shown below:

public class Platformer extends JPanel implements Runnable {

    int x = 0;//Sets the starting coords. of the ball
    int y = 0;

    public void add() {
            JFrame gameFrame = new JFrame("Sample Frame");//Makes the window variable w/ the name in quotations.
        Platformer game = new Platformer();//'Copies' the graphics functions above into a variable
        gameFrame.add(game);//Adds the above variable into th window
        gameFrame.setSize(setx,sety);//Sets the resolution/size of the window(x,y)
        gameFrame.setVisible(true);//Makse the window visible
        gameFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//Makes the window close when the close button is hit
    }

      private void moveBall() {//How much the ball moves by
        x = x+1;
        y = y+1;
     }


     @Override
      public void paint(Graphics g) {//Essentially al the graphics functions
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.fillOval(x, y, 30, 30);
    }


     @Override
     public void run() {
        //this code will be executed for every 10 seconds
        moveBall();
        paint();
     }


    public static void main(String[] args) throws InterruptedException {
        Scanner reader = new Scanner(System.in);
        Scanner reader1 = new Scanner(System.in);
        System.out.print("Enter an x-value for the window (whole numbers only): ");
        int setx = reader.nextInt();
        System.out.print("Enter a y-value for the window (whole numbers only): ");
        int sety = reader.nextInt();

        JFrame gameFrame = new JFrame("Sample Frame");//Makes the window variable w/ the name in quotations.
        Platformer game = new Platformer();//'Copies' the graphics functions above into a variable
        gameFrame.add(game);//Adds the above variable into th window
        gameFrame.setSize(setx,sety);//Sets the resolution/size of the window(x,y)
        gameFrame.setVisible(true);//Makse the window visible
        gameFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//Makes the window close when the close button is hit

        //schedule it to run for every 10 seconds
        //this calls the run() method above
        ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
        scheduler.scheduleAtFixedRate(game, 0, 10, TimeUnit.SECONDS);

    }    
}


回答2:

though I've been able to find several better ways

Well don't you think you should tell us the ways you have found so we don't spend time making suggetions you already know about? We are not mind readers we can't guess what you have tried.

I haven't been able to find a way to include them without messing u the code.

Well, your code should be redesigned anyway.

The animation of your code should be a function of your game panel, not the main() method. So you should have methods like startGame() and stopGame() built into your panel that you can invoke.

Netbeans gave me a hint that doing thread.sleep would affect performance.

Yes in general you should not use Thread.sleep() because usually code is executed as a result of some user Action and the code executes on the Event Dispatch Thread (EDT). Since the EDT is responsible for painting the GUI is you keep telling it to sleep then this obviously will affect performance.

However, in your case the looping logic is not executed on the EDT so it should not be an issue (other than the overall design issue).

In reality you should not be using a loop. Instead you should be using a Swing Timer to schedule the animation.

Also check out the section from the tutorial on Concurrency in Swing which will explain more about the EDT and why Swing components should be updated on the EDT.