top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What will happen if we don’t override thread class run() method in java?

0 votes
721 views
What will happen if we don’t override thread class run() method in java?
posted Feb 12, 2018 by Jon Deck

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Thread class run() method code is as shown below.

public void run() {
    if (target != null) {
        target.run();
    }
}

Above target set in the init() method of Thread class and if we create an instance of Thread class as new TestThread(), it’s set to null. So nothing will happen if we don’t override the run() method. Below is a simple example demonstrating this.

public class TestThread extends Thread {

    //not overriding Thread.run() method

    //main method, can be in other class too
    public static void main(String args[]){
        Thread t = new TestThread();
        System.out.println("Before starting thread");
        t.start();
        System.out.println("After starting thread");
    }
}

Output:
It will print only below output and terminate.

Before starting thread
After starting thread

answer Feb 13, 2018 by Frank Lee
...