Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By PhHein

Thread: what is the use of thread class constructors

  1. #1
    me_shankara is offline Member
    Join Date
    Dec 2012
    Posts
    5
    Rep Power
    0

    Default what is the use of thread class constructors

    Hi !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”);”
    -----------------------------------------------
    Java Code:
    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

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default Re: what is the use of thread class constructors

    doWhile likes this.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    viki1719 is offline Member
    Join Date
    May 2013
    Posts
    8
    Rep Power
    0

    Default Re: what is the use of thread class constructors

    Quote Originally Posted by me_shankara View Post
    -->What is the benefit of creating object to thread class?
    Thread class defined several constructors to initialize a thread object (you can see the API for details), like
    1-Thread()
    it just allocates a thread object with the default thread name assign by JVM like "Thread-0" it would assign a name for youe first newly created thread if your are not assigning a name for your thread explicitly.
    2-Thread(String name)
    this constructor would assign a name with whatever string you are passing .
    now come on to your doubt...
    actually you are implementing Runnable interface in your class ,so you have to deal your class with the Thread class because Runnable interface contains only run() method (which is responsible for your thread job) but here if you look the concept of threading ,how you can start a thread then only one thing is responsible to do this job that is start() method which is available into Thread class ,so you have to pass your target Runnable(your class object which implement Runnable) object to appropriate constructor then you would be able to register your thread with the thread scheduler.where in the case of extending Thread class start() visible to your class.

    Quote Originally Posted by me_shankara View Post
    -->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”);”
    Yes, you can!

    feel free to correct me if i am wrong..a

Similar Threads

  1. Using multiple constructors from a class.
    By BradMD in forum New To Java
    Replies: 6
    Last Post: 08-09-2012, 02:53 AM
  2. Replies: 11
    Last Post: 03-05-2012, 11:18 AM
  3. Class and constructors question.
    By Meta in forum New To Java
    Replies: 19
    Last Post: 04-29-2010, 02:58 AM
  4. Define class and constructors
    By HaDesT in forum New To Java
    Replies: 2
    Last Post: 03-04-2010, 05:02 PM
  5. Replies: 1
    Last Post: 05-19-2009, 11:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •