File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes What is The use of Thread class constructors? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "What is The use of Thread class constructors?" Watch "What is The use of Thread class constructors?" New topic

What is The use of Thread class constructors?

shankara me

Joined: Nov 30, 2012
Posts: 14
Hi!My doubt is on Thread class constructors
1)public Thread()
2)public Thread(String name)
The above two are two constructors of thread class.
First constructor is used to create object of thread class.
Second constructor is also used to create object of thread class with required name.
If we write
1)Thread t1=new Thread();
This creates a thread class object t1.
2)Thread t2=newThread(“MYTHREAD”);
This also creates Thread class object t2 .It also creates thread with name "MYTHREAD".
My questions are
1)What is the benefit of above t1,t2;
2)How can we use the thread class objects t1,t2?
3)Is it possible to execute run method of the thread "MYMTHREAD"?
4)Suppose we have extended class named “a” from Thread class like below.
Then how can we use t1,t2 on below class “a”?
Is it possible to create thread to below class “a” with required name by using above constructors?

Lalit Mehra
Ranch Hand

Joined: Jun 08, 2010
Posts: 384

Hi Shankara,

Since you have asked too many questions at once let me introduce you to the answers in the same manner.

Ans 1: Both t1 and t2 are valid Thread Objects, only one has a decided name and other doesn't. Both run in the same manner as any other thread.
Ans 2: You can use both the objects in a variety of ways as per the requirement in your project.
Ans 3: yes it is possible to execute run() of the Thread 'MYTHREAD" using t2.start();
Ans 4: Since class a extends Thread it itself "is a" Thread now. You can have a run() in your Class "a" which will be called on any object of the type "a"
Ans 5: yes it is possible to create a thread with a required name using the second constructor you mentioned i.e. new Thread(String name);

And yes, your class should start with a CAPITAL Letter and also access specifiers work only when in "small" letters your one is incorrect.
Jeff Verdegan

Joined: Jan 03, 2004
Posts: 6109

Note, however, that you will probably never have a good reason to extend thread. You're not creating a specialized type of Thread. You're just creating a task to be executed, so you should implement Runnable instead.
I agree. Here's the link:
subject: What is The use of Thread class constructors?
It's not a secret anymore!