What's the use of the Thread constructor with a string parameter?
Looking at Thread's constructors, I can see that there is one that takes a single string parameter. I have below code which is not helpful. I would like to know how to make fruitful use of this constructor and make something really run
public class ThreadTest {
public static void main(String[] args) {
Thread t = new Thread("abc");
t.start();
System.out.println("Complete");
}
}
Or is it not supposed to be used in the way I showed above?
I know perfectly well how to write multiple threads and execute :) I am just trying to figure out the correct use of this constructor? If it is only used when called super(name)
, extending Thread, not the way I use it above.
The thread class itself doesn't do that much. You have to extend it or create it at runtime so that it does the task at startup. From the doc:
start (): "Causes this thread to start, the Java Virtual Machine calls the method to start this thread."
run (): "If this thread was created using a separate Runnable run object then this Runnable object run method is called, otherwise this method does nothing and returns."
Therefore, creating a new thread in your style and starting it does nothing. One use of the Thread (String) constructor is in subclasses:
public class Worker extends Thread{
public Worker(int numb){
super("worker-"+numb);
}
@Override
public void run(){
//Stuff this thread actually does when run
//....
for(int i = 0; i < 10; i++)
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}
To answer your second question in the comments, you must write code that runs in parallel. Consider the above class and this main method:
public static void main(String[] args){
Worker w1 = new Worker(1);
Worker w2 = new Worker(2);
w1.start();
w2.start();
}
Start methods w1 and w2 will run in parallel. The order of the print statements will differ depending on the execution of the main method.
This particular constructor is used to provide a "name" for a thread, which can later be used to distinguish between instances of a particular thread type.
From the official Java API documentation ;
Topic
public Thread (string name)
Allocates a new Thread object. This constructor has the same effect as Thread (null, null, name).
Parameters : name - the name of the new stream
Once you have assigned a name Thread
, you can call a method getName()
on the instance Thread
to return the name it was given when it was created. This can be useful for debugging or for distinguishing between instances of the same subclass type Thread
.
Additional reading:
- Official Guide - Defining and Starting a Stream
If you just call this constructor, you end up Thread
with which does nothing. What for? Check out the source code java.lang.Thread
. It has a class variable private Runnable target;
. When you call this constructor, the variable target
remains set to null
(because this constructor just sets the name Thread
).
In addition, the method run()
java.lang.Thread
looks like this:
public void run() {
if (target != null) {
target.run();
}
}
This means that this method run()
does nothing, since it target
is null
.
To build / run Thread
that actually does something useful, read here:
Java tutorial - how to start a thread?