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

thread.start java
use of thread in java
thread start not calling run
can we call run() method twice
how start method calls run method
extending thread class in java
run() method in java
java run() method in new thread

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; 
    }
  }

Why don't we call run() method directly, why call start() method?, On the other hand if the run() method of these threads are being called all of them is being handled by the same current thread and no multithreading will take​  Why is run method not called? i.e before Thread t = new Thread(). Now run is called only once, why so, I have two threads here and am calling the start method

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.

java, Look at the constructor for the Thread class. How does it connect to the code in your run method? How would the Thread object find your code? Yes a run method is called, but it not your run method. The Thread class has its own run method that it will call. Look at my previous post again. so do u say me to check the signature of the run() method?

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.

Java MCQs, Which method is necessary to be called to start the execution of a thread? Executing thread.run() doesn't create a new Thread in which your code gets executed. It just executes the code in the current Thread from which the thread.run() code is invoked. Executing thread.start() creates a new OS level thread wherein the run() method gets called. In essence: Single Threaded programming → Directly calling the run() method

Can we call run() method directly to start a new thread, multithreading will take place, hence the output would reflect the sequential execution of threads in the specified order. The java.lang.Thread.start() method causes this thread to begin execution, the Java Virtual Machine calls the run method of this thread.The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method).

Java.lang.Thread.start() Method, But if we directly call the run() method then no new thread will be created and run​() method will be executed as a normal method call on the current calling  The java.lang.Thread.run() method is called if this thread was constructed using a separate Runnable run object, else this method does nothing and returns.

Creating and Starting Java Threads, Why do one call start method of thread if start() calls run() in turn" or "What is of us just don't care at that time until its been asked in Java Interview. start vs run in Java thread is that you can not call start() method twice on  The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method). It is never legal to start a thread more than once.

Comments
  • This will always apply : if(game.running) return; , since game.running is true (from Game's constructor) .Hence, your thread is never started.
  • I would also recommend to read the Concurrency in Swing tutorial. Doing Swing operations (like creating and showing a JFrame) should happen on the EDT (=Event Dispatch Thread), not just on any thread.
  • Yes, thank you! I just realised how silly it was ._."
  • You mean false.
  • Ya. Thanks :D .