Reputation: 823
public class GetCurrentThread implements Runnable {
Thread th;
public GetCurrentThread(String threadName) {
th = new Thread(this,threadName); //<----DOUBT
System.out.println("get threadname "+th.getName());
th.start();
}
public void run() {
System.out.println(th.getName()+" is starting.....");
System.out.println("Current thread name : " + Thread.currentThread().getName());
}
public static void main(String args[]) {
System.out.println("Current thread name : " + Thread.currentThread().getName());
new GetCurrentThread("1st Thread");
//new GetCurrentThread("2nd Thread");
}
}
Can someone explain what the 2nd line of above code is doing? My understanding for "th = new Thread(this,threadName)" is, it will create thread object with the name given; let say name "1st Thread".Now, What "this" keyword is doing here?because when i remove this and try get name of the thread I got the name with no issues, but it never started run().can someone explain in simple terms rather than with one single line answer. i really appreciate everyone's help.
Upvotes: 2
Views: 9341
Reputation: 116908
th = new Thread(this, threadName); //<----DOUBT
Your 2nd line is constructing a new Thread
object with your GetCurrentThread
class as the target (this
) and a thread name of "threadName"
. Your this
can be the target because it implements Runnable
. Inside of the Thread
class, when the thread is started, it calls Thread.run()
which does:
public void run() {
if (target != null) {
target.run();
}
}
Your GetCurrentThread
is the target since you pass this
in to the constructor. Really it should not be called GetCurrentThread
since it isn't a thread. Two lines after you have constructed your Thread
, you start it running:
th.start();
The start()
method does the actual work of creating a separate working native thread. The first thing the thread does is to call the run()
method in your GetCurrentThread
class.
As a comment, typically it is not recommended for a class to start()
itself in its constructor. There are race conditions that are inherent with this that can cause problems. It is better to have a start()
method on your GetCurrentThread
/** start the underlying thread */
public void start() {
th.start();
}
Your main would look like:
public static void main(String args[]) {
System.out.println("Current thread name : " + Thread.currentThread().getName());
GetCurrentThread thread1 = new GetCurrentThread("1st Thread");
thread1.start();
}
Upvotes: 3
Reputation: 33544
Try this,
th = new Thread(this,threadName);
In the above line "this" keyword
will signify the object of the class which has implemented
Runnable Interface.
Now i will try to show you this in more detail:
public class A implements Runnable{
public void run(){
// some work
}
}
public class B {
public static void main(String[] args){
A a = new A;
Thread t = new Thread(a);// a is an obj ref variable of class A which implements Runnable
t.start();
}
}
Upvotes: 1
Reputation: 1827
this
here is the object whose run method is called, in this case is an instance of the GetCurrentThread
class. There is no guarantee that after you do th.start()
the thread starts execution straight away. What happens here is that the main thread finishes execution before you see the output lines in the run method. Add th.join()
after the object creation in main so you wait for the newly created thread to finish:
public class GetCurrentThread implements Runnable {
Thread th;
public Thread getThread(){
return th;
}
public GetCurrentThread(String threadName) {
th = new Thread(this, threadName); // <----DOUBT
System.out.println("get threadname " + th.getName());
th.start();
}
public void run() {
System.out.println(th.getName() + " is starting.....");
System.out.println("Current thread name : "
+ Thread.currentThread().getName());
}
public static void main(String args[]) {
System.out.println("Current thread name : "
+ Thread.currentThread().getName());
GetCurrentThread currentThread = new GetCurrentThread("1st Thread");
// new GetCurrentThread("2nd Thread");
try {
Thread thread = currentThread.getThread();
if (thread != null) {
thread.join();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
The output you get is this:
Current thread name : main
get threadname 1st Thread
1st Thread is starting.....
Current thread name : 1st Thread
Upvotes: 0
Reputation: 17923
You are referring to the constructor Thread(Runnable target, String name)
, where first parameter target
is any Runnable
instance having a run()
method.
In this particular case, target
object is the instance of very class GetCurrentThread
itself (which is Runnable) where the thread is created and started. So here this
represents the instance of GetCurrentThread
The Runnable.run()
method is actually the task to be performed by a thread (which is represented by Thread
object). If you pass a null instead of it, there wont be any task to thread to be performed and hence no run()
method will be called.
Upvotes: 0
Reputation: 62459
Well the first parameter to the constructor of Thread
is an object whose type implements Runnable
, which is exactly what this
represents. Note the class declaration:
public class GetCurrentThread implements Runnable
Hence, when the thread starts the run
method of the current instance (this
) is executed.
Anyway, I try to avoid as much as possible this kind of code. It only leads to confusion.
Upvotes: 1