简体   繁体   中英

Why run() method defined is not called on call of start by Thread?

I was just experimenting some code around Thread class and I get stuck at something, Well firstly have a look at my code

class ThreadExample implements Runnable
{
String threadName;
Thread thread;
public ThreadExample()
    {
    thread=new Thread();
    thread.start();
    }
public void run()
    {
    System.out.println("Thread "+getThreadName()+" is being executed");
    }
void setThreadName(String string)
    {
    threadName=string;
    thread.setName(string);
    }
String getThreadName()
    {
    return thread.getName();
    }
public static void main(String string[]) throws InterruptedException
    {
    ThreadExample threadExample= new ThreadExample();
    threadExample.setThreadName("Thread !");
    //threadExample=new ThreadExample();
    //threadExample.setThreadName("Thread 2");
    //threadExample=new ThreadExample();
    //threadExample.setThreadName("Thread 3");
    Thread.sleep(500);
    }
}

Well I think this code is very simple and Everyone should have got my intentions although When I am running this program It just get complete without even calling run() method even I make main Thread to wait for sometime until the child Thread which is ThreadExample completes. I am new to this so sorry if I have forgotten some thing. Thanks in advance.

You created a Runnable type and never passed it into a thread context. You'll want to add it to the Thread. I would do something like:

String threadName;
Thread thread;
public ThreadExample() {
    thread=new Thread(this);
}

public void startThread() {
   thread.start();
} 

The Thread class accepts a Runnable as an argument.

To run this implementation class, create a Thread object, pass Runnable implementation class object to its constructor. Call start() method on thread class to start executing run() method.
You missed following two lines:

Thread thread1 = new Thread(threadExample);
thread1.start();

   class ThreadExample implements Runnable
    {
    String threadName;
    Thread thread;
    public ThreadExample()
        {           
        }
    public void run()
        {
        System.out.println("Thread "+getThreadName()+" is being executed");
        }
    void setThreadName(String string)
        {
        threadName=string;
        thread.setName(string);
        }
    String getThreadName()
        {
        return thread.getName();
        }
    public static void main(String string[]) throws InterruptedException
        {
        ThreadExample threadExample= new ThreadExample();
        threadExample.setThreadName("Thread !");
        //threadExample=new ThreadExample();
        //threadExample.setThreadName("Thread 2");
        //threadExample=new ThreadExample();
        //threadExample.setThreadName("Thread 3");
        Thread.sleep(500);
        Thread thread1 = new Thread(threadExample);
        thread1.start();
        }
    }

You never call run() method. You rather call start, which you are already doing in ThreadExample() constructor, but it has some mistakes I will explain:

In java you have 2 options to deal with Threads. First is to inherit from Thread class, so you can call start() method from it and the code inside run() will be executed. The second option is to create a Runnable, which seems the option you are choosing, but to run this you have to create a Thread like this:

ThreadExample runnable = new ThreadExample();
Thread myThread = new Thread(threadExample);

And then you can call myThread.start(); when you are ready to start your thread.

As John Vint has pointed out, the Thread class needs a Runnable target. I edited your program a little :

public class NewThreadExample implements Runnable{

    String threadName;

    public String getThreadName() {
        return threadName;
    }

    public void setThreadName(String threadName) {
        this.threadName = threadName;
    }

    public static void main(String[] args) throws InterruptedException {

        NewThreadExample threadTarget = new NewThreadExample();
        threadTarget.setThreadName("Dushyant");

        Thread thread = new Thread(threadTarget);
        System.out.println("Thread created and going to start");
        thread.start();
        System.out.println("Thread sleeping");
        Thread.sleep(2000);
        System.out.println("Program done");
    }

    @Override
    public void run() {
        System.out.println(this.getThreadName() + " is running...");
    }

}

gives

Thread created and going to start

Thread sleeping

Dushyant is running...

Program done

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