Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: What is The use of Thread class constructors?

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    13
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default What is The use of Thread class constructors?

    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?
    Class a extends Thread
    {
    Public void run()
    {
    System.out.println(“Hi”);
    }
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: What is The use of Thread class constructors?

    1) The first has a name assigned to it by the JVM, the second has a name you assigned. If you are ever which to access the Thread by name at a later date, knowing that name (eg assigning the Thread a name) will allow you to do so. But read the API beforehand:
    http://docs.oracle.com/javase/6/docs...ng/Thread.html
    2)
    Thread (Java Platform SE 6)
    Lesson: Concurrency (The Java™ Tutorials > Essential Classes)
    3) See (2)
    4) What did you try? Did it work? Compile? Run properly?

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    13
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default 2-what is the use of thread class constructors

    Hi !last time I asked one doubt with heading “what is the use of thread class constructors”.But I could not ask that doubt in a right
    manner.Now I am asking the same doubt in a simple and clear manner.My doubt is on Thread class constructors. My doubts based on below simple code.
    In below code ,in main method there is a statement as ” Thread C4=new Thread (new first(),"FIRSTTHREAD");”
    This Statement Will create a Thread FIRSTTHREAD for the class “first”.
    Here we used Thread class constructor public Thread(Runnable target,String name);
    There are some other constructors in Thread class like
    1)public Thread();
    2)public Thread(String name);
    The first constructor is used to create an object of Thread class.
    The second constructor is used to create an object of Thread class but with required name.
    Then my question is
    -->What is the benefit of creating object to thread class?
    -->A class named with” four” is available in below code.By using second constructor of above constructors is it possible to write “four f1=new four(“MYTHREAD”);”
    ------------------------------------------------------
    class first implements Runnable 
       {  
       public void run() 
       {  
       System.out.println(Thread.currentThread().getName());
       }  
       }  
       class four extends Thread  
       {  
      public void run() 
      {  
      System.out.println(Thread.currentThread().getName()); 
      }
      } 
      class Third  
      { 
      public static void main(String args[])  
      {  
     
     Thread C4=new Thread (new first(),"FirstThread");
     C4.start(); 
     
     }  
     } 
     
     Output:  
     
     FirstThread

  4. #4
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: 2-what is the use of thread class constructors


  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: 2-what is the use of thread class constructors

    Quote Originally Posted by me_shankara View Post
    Hi !last time I asked one doubt with heading “what is the use of thread class constructors”.
    Yes you did. Please see the reply in post #2.
    If you do not understand please ask another question.

    Threads merged.

  6. The Following User Says Thank You to jps For This Useful Post:

    copeg (May 7th, 2013)

Similar Threads

  1. How to use constructors and class objects together.
    By IkeIII in forum Object Oriented Programming
    Replies: 5
    Last Post: April 1st, 2013, 11:16 AM
  2. Replies: 18
    Last Post: March 30th, 2013, 09:11 AM
  3. Replies: 8
    Last Post: August 9th, 2011, 08:25 PM
  4. [SOLVED] Overloading constructors(Multiple Constructors)
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: May 11th, 2011, 12:55 PM
  5. adding get mothods to a class extending thread
    By aliaa2a in forum Object Oriented Programming
    Replies: 6
    Last Post: August 3rd, 2009, 06:41 AM