Angus
Angus

Reputation: 12631

Why pass "this" to Thread constructor?

"this" is used to refer to the members of the current class. I was trying a program in java using multithreading.

this => object of the current class where it is referred

The program is

class Thread_child implements Runnable{
    Thread t;

    Thread_child()
    {
        t = new Thread(this,"DemoThread");
        System.out.println("ChildThread:"+t);
        t.start();
    }
    public void run(){
    char a[] = {'A','B','C','D','E','F','G','H','I','J'};
    try{
        for(int i=0;i<10;i++){
            System.out.println("ChildThread:"+i+"\t char :"+a[i]);
            Thread.sleep(5000);
        }
     }
     catch(InterruptedException e){
        System.out.println("ChildThread Interrupted");
     }
      System.out.println("Exiting from the Child Thread!");
    }
}
class Thread_eg{

    public static void main(String args[]){
        new Thread_child();
        try{
            for(int i=1;i<=10;i++){
            System.out.println("MainThread:"+i);
            Thread.sleep(3000);
        }
        }
        catch(InterruptedException e){
            System.out.println("MainThread Interrupted");
        }
        System.out.println("Exiting from the Main Thread!");

    }

}

What does this Thread() constructor do . why do we need to pass 'this' as a parameter to it. I tried to run it without giving the parameter but the child threads were not run.only the mainthread was printed . when i replaced the thread constructor with the parameter it ran the child threads. why is that so?

Upvotes: 2

Views: 1579

Answers (3)

Husain Basrawala
Husain Basrawala

Reputation: 1751

There are two ways of implementing a Thread. One is create a class that extends Thread class and this class runs as a thread in VM.

MyThread mt = new MyThread();
mt.start();

The start will result in execution of run method that was overridden from Thread.

In case you could not extend to Thread, you can implement Runnable which makes you implement run method. Now to run this class you need to pass an object of it to Thread.

MyRunnable mr = new MyRunnable();
Thread t = new Thread(mr); // telling the thread what needs to be execute through run method
t.start();

In your code since you are starting thread in constructor, you have passed this instead my mr in above example. Basically you need to tell Thread what it needs to do via run method.

This is how run of Thread looks like:

public void run() {
if (target != null) { //target is nothing but a Runnable.
    target.run(); 
}
}

Upvotes: 0

Kuldeep Jain
Kuldeep Jain

Reputation: 8598

Because this is the Runnable object (Thread_child) whose run() method gets called.

Upvotes: 3

Andrzej Doyle
Andrzej Doyle

Reputation: 103817

Have a look at the documentation for that constructor, and all should become clear. Pay particular attention to the part that states

If the target argument is not null, the run method of the target is called when this thread is started. If the target argument is null, this thread's run method is called when this thread is started.

(The underlying issue is that a Thread is just a thread, and doesn't inherently do anything. You need to tell it what to execute.)

Upvotes: 3

Related Questions