2

I am learning the multithreading concept in Java and came across a program that creates a new thread by implementing the runnable class. Here is a part of the code where I have my doubts:

class Demo implements Runnable
{
    Thread t;
    Demo()
    {
        t=new Thread(this,"child_thread1");
        System.out.println("Thread Info:"+t);
        t.start();
    }
}

Now, could anyone please explain to me what is the use of this? Instead of passing this I tried passing an object of the Demo class. It turned out that child_thread1 didn't even run!

honk
  • 9,137
  • 11
  • 75
  • 83
Anish
  • 29
  • 1
  • 1
    `this` is a reference to the current instance, which is `Runnable`, which a Thread can call – Bohemian Aug 02 '14 at 07:17
  • 3
    you(thread code) are already inside the object of `Demo` class that can be accessed by `this`. – Braj Aug 02 '14 at 07:17
  • 1
    The two comments above are better than both answers below :) – Nir Alfasi Aug 02 '14 at 07:30
  • If you pass a new instance of `Demo` instead of `this`, it will likely instantiate an infinite number of threads recursively before they are started... – Bastien Jansen Aug 02 '14 at 07:33
  • Wow, can't say I've ever encountered the self-running thread pattern before, and I don't think I like it. Seemed fishy to me at a glance, there is already discussion here on why that is: http://stackoverflow.com/questions/5623285/why-not-to-start-a-thread-in-the-constructor-how-to-terminate If you are just learning threaded programming for the first time you should definitely be starting with more basic and traditional constructs: http://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html – The111 Aug 02 '14 at 07:44
  • if u pass the object of demo , then u will have a recursion and stack overflow..a similar question that i asked: http://stackoverflow.com/questions/21205321/a-recursive-instantiating-of-objects – user2837260 Aug 02 '14 at 08:03
  • Passing this to a thread in the constructor is highly discouraged! There might happen very strange things like `run` gets called before the object construction has been finished. – isnot2bad Aug 02 '14 at 08:45
  • possible duplicate of [What is the meaning of "this" in Java?](http://stackoverflow.com/questions/3728062/what-is-the-meaning-of-this-in-java) – Joe Aug 02 '14 at 13:52

2 Answers2

0

"this":-

this keyword can be used to refer current class instance variable.

this() can be used to invoke current class constructor.

this keyword can be used to invoke current class method (implicitly).

this can be passed as an argument in the method call.

this can be passed as argument in the constructor call.

this keyword can also be used to return the current class instance.

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
Deepanshu J bedi
  • 1,530
  • 1
  • 11
  • 23
0

Here 'this' means a Object of a Thread/Runnable . Since Demo Class is inherited Runnable Interface Demo class can be considered as a thread. so parsing key word ' this' which refers to the current object , when we call ' t.start()' it execute run() method in a new thread. And of course you can parse new Demo Object as well but create that object with a different constructor because otherwise when we create Demo object it will call constructor in a loop.Here is an Example

public class Demo implements Runnable{
Thread t1,t2;
public Demo(){
    //Create Child 1 with First constructor
    t1=new Thread(new Demo(true),"Child-1");
    System.out.println("Thread Info:"+t1);
    t1.start();
    //Create Child 2 with Second constructor
    t2=new Thread(new Demo(1),"Child-2");
    System.out.println("Thread Info:"+t2);
    t2.start();
}
public Demo(boolean b){
    t1=new Thread();
    t2=new Thread();
}
public Demo(int i){
    t1=new Thread("Constructor 2"){
        @Override
        public void run() {
            System.out.println("Internal Thread "+Thread.currentThread().getName());
        }
    };
    t1.start();
}
@Override
public void run() {
    //Work goes Here/
    System.out.println("Thread Works :"+Thread.currentThread().getName());
}
/**/

public static void main(String[] args) {
    //Default Constructor
    Demo demo=new Demo();
}

}

Here When we call Second constructor , 'Constructor-2' named thread is invoked through 'Child-2' Thread.

prasadmadanayake
  • 1,415
  • 15
  • 23