Dushyant Suthar
Dushyant Suthar

Reputation: 671

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.

Upvotes: 0

Views: 102

Answers (4)

PVR
PVR

Reputation: 915

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

Upvotes: 0

Arun
Arun

Reputation: 11

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

Upvotes: 0

Camilo Ortegón
Camilo Ortegón

Reputation: 3692

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.

Upvotes: 0

John Vint
John Vint

Reputation: 40256

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.

Upvotes: 5

Related Questions