top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

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

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

Share this question
Facebook Share Button Twitter Share Button Google+ Share Button LinkedIn Share Button Multiple Social 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 by Frank Lee
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...