Results 1 to 6 of 6

Thread: A small doubt

  1. #1
    ranganathan is offline Member
    Join Date
    Feb 2010
    Posts
    2
    Rep Power
    0

    Default A small doubt

    I've a small doubt as to how the new operator works in the following scenario :

    I came accross this while reading an example for a simple multithreaded program

    Java Code:
    class Newthread extends Thread{
    
    Newthread()
    {
     super("Demo Thread");
     System.out.println("Child Thread : "+this);
     start();
    }
    
     public void run()
    { 
     System.out.println("Child Thread running");
    }
    
    }
    
    public void run(){
    //some code
    }
    }
    
    class ThreadDemo{
    public static void main(String args[]){
    
    new Newthread(); //??
    
    // other code
    }
    My question is what does "new Newthread();" do ?

    Usually we create an object to the class and assign
    address created by the new to the object. so in this case
    why doesn't the java compiler report any error ?
    Last edited by ranganathan; 02-19-2010 at 07:25 AM.

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Maybe the NewThread contructor calls the run method? But yeah, I don't think this is a very good habit, as you lose control over the thread.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    new Newthread(); creates an instance of Newthread. As pointed out if you don't assign the returned reference to some variable you won't be able to interact with the instance that was created. (Did you maybe leave out the ".run()" bit in the code you posted?)

    One place you see this a lot is with anonymous classes;

    Java Code:
    whatever.addActionLister(new ActionListener() {
        // etc
    });

    In this case also an instance is created but you don't assign a reference to anything. Instead you just pass it as an argument.

  4. #4
    Aseem is offline Senior Member
    Join Date
    Mar 2009
    Location
    USA
    Posts
    127
    Rep Power
    0

    Default

    unless run method is invoked, there is not point creating a thread. Once a run method is called, a new thread kicks off. Since, there is no invocation of run method, it will just create an instance of NewThread class but as pbrock said you won't be able to interact with the instance.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Aseem View Post
    unless run method is invoked, there is not point creating a thread. Once a run method is called, a new thread kicks off. Since, there is no invocation of run method, it will just create an instance of NewThread class but as pbrock said you won't be able to interact with the instance.
    But to clarify, us mortals get the thread to start by calling its start() method, and it is this that will eventually call run.

  6. #6
    ranganathan is offline Member
    Join Date
    Feb 2010
    Posts
    2
    Rep Power
    0

    Default

    Yes Indeed, it does include a start() which calls run(), I didn't enter it,
    i felt it was unrelated to my question.

    Quote Originally Posted by pbrockway2 View Post
    new Newthread(); creates an instance of Newthread. As pointed out if you don't assign the returned reference to some variable you won't be able to interact with the instance that was created. (Did you maybe leave out the ".run()" bit in the code you posted?)

    One place you see this a lot is with anonymous classes;

    Java Code:
    whatever.addActionLister(new ActionListener() {
        // etc
    });

    In this case also an instance is created but you don't assign a reference to anything. Instead you just pass it as an argument.
    Quote Originally Posted by Aseem View Post
    unless run method is invoked, there is not point creating a thread. Once a run method is called, a new thread kicks off. Since, there is no invocation of run method, it will just create an instance of NewThread class but as pbrock said you won't be able to interact with the instance.

Similar Threads

  1. small code, need help
    By p0rnstar in forum New To Java
    Replies: 22
    Last Post: 01-22-2010, 02:57 PM
  2. small doubt
    By ravi kumar in forum Java Software
    Replies: 1
    Last Post: 08-02-2009, 04:50 PM
  3. A small Question
    By Eku in forum JDBC
    Replies: 7
    Last Post: 09-01-2008, 07:10 AM
  4. Small problem
    By ayoood in forum New To Java
    Replies: 2
    Last Post: 06-06-2008, 01:27 PM
  5. small error
    By ayoood in forum New To Java
    Replies: 23
    Last Post: 05-27-2008, 01:18 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
  •