简体   繁体   中英

Run is not being called when i start the thread. Java

So, i started writing code for a game and, this time I wanted to use separate classes for the launcher and the game. So basically it is a single threaded game and the thread is made and started in the launcher, and the Game class is "Runnable" with the run method. The problem is that even though I start the thread the run method won't get called.

Launcher :

public class Launcher {

private Thread thread;

private static Game game;

public static void main(String[] args) {
    Launcher obj = new Launcher();
    game = new Game();

    obj.start();
}

public synchronized void start() {
    if(game.running) return;
    game.running = true;

    thread = new Thread(game);
    thread.start();
}

public synchronized void stop() {
    if(!game.running) return;
    game.running = false;

    try {
        thread.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

}

Game class :

public class Game extends JFrame implements Runnable{

public boolean running = false;
private static int WIDTH = 1280 , HEIGHT = 720;

private Screen screen;

public Game() {
    this.setTitle("Game");
    this.setSize(WIDTH, HEIGHT);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLocationRelativeTo(null);

    this.running = true;

    this.setVisible(true);

    init();
}

public void init() {
    screen = new Screen(WIDTH, HEIGHT);
    this.add(screen);
    screen.init();

    this.pack();
}

public void render() {
    Screen.clearScreen();


    Screen.update();
}

public void update() {

}

@Override
public void run() {
    System.out.println("called"); //Checking if it was called, and it is not being called

    long lastTime = System.nanoTime();
    long now;
    int fps = 60;
    double timePerTick = 1000000000/fps;
    double delta = 0;

    while(running) {
        now = System.nanoTime();
        delta += (now - lastTime) / timePerTick;
        lastTime = now;

        if(delta >= 1){
            update();
            delta--;
        }

        render();
    }

}

}

When you create new Game() you set running = true . Later you check, in your launcher if(game.running) return , which is true and returns before.

Unrelated: In favour of encapsulation don't expose public fields as in the case of running. Just expose some method that will tell you this information like:

  public class Game{
    private boolean running;
    ...
    public boolean isRunning(){ 
      return running; 
    }
  }

In this constructor

public Game() {
    this.setTitle("Game");
    this.setSize(WIDTH, HEIGHT);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLocationRelativeTo(null);

    this.running = true;

    this.setVisible(true);

    init();
}

You have to set this.running = false; inorder to start your Game thread.

When you create a Game, you set running to true:

public Game() {
    //...
    this.running = true;

When you call start, you check whether it's already running and, if it is, you return early:

public synchronized void start() {
    if(game.running) return;
    //...
}

So you always exit this method before doing anything.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM