creating a new thread extending thread class

Discussion in 'Java' started by Aditi, Jun 24, 2005.

  1. Aditi

    Aditi Guest

    Consider the following program of creating a new thread extending
    thread class.
    //creating a new thread extending thread.
    class NewThread extends Thread {

    NewThread() {
    super("Demo Thread");
    System.out.println("child thread"+this);
    start();}
    public void run() {
    try {
    for(int i=5;i<0;i--){
    System.out.println("child thread:"+i);
    Thread.sleep(500);}
    }
    catch (InterruptedException e)
    {System.out.println("child interupted");
    }
    System.out.println("exiting child thread");
    }
    }
    class test {
    public static void main(String args[]) {
    new NewThread();
    try {
    for(int i=5;i>0;i--) {
    System.out.println("main thread"+i);
    Thread.sleep(500);
    }
    } catch(InterruptedException e) {
    System.out.println("main interupted");
    }
    System.out.println("exiting main thread");
    }
    }
    the output which i am getting is
    child threadThread[Demo Thread,5,main]
    main thread5
    exiting child thread
    main thread4
    main thread3
    main thread2
    main thread1
    exiting main thread

    The child thread is entered and exited quickly without being executed
    ..I guess output of programs with sleep method may vary based on
    processor speed and task load. Is this a correct explanation of such
    behaviour or is it something else???
    Aditi, Jun 24, 2005
    #1
    1. Advertising

  2. Aditi

    Chris Smith Guest

    Aditi <> wrote:
    > Consider the following program of creating a new thread extending
    > thread class.


    First of all, please learn to format your code. I am having a hard time
    imagining how you could come up with something so unreadable as what you
    posted. Line wrapping by your news reader might be part of it, but
    other pieces are just utterly odd.

    Second, this has nothing to do with threads. You for loop condition in
    the child thread is inverted.

    --
    www.designacourse.com
    The Easiest Way To Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
    Chris Smith, Jun 24, 2005
    #2
    1. Advertising

  3. Aditi

    Aditi Guest

    Re: creating a new thread extending thread class[SOLVED]

    Thanks. :p
    Aditi, Jun 24, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,746
    Thomas Weidenfeller
    Oct 12, 2005
  2. Fred
    Replies:
    6
    Views:
    386
  3. Simon Elliott
    Replies:
    0
    Views:
    375
    Simon Elliott
    Jan 11, 2005
  4. Matthias Kaeppler
    Replies:
    1
    Views:
    333
    Kanenas
    Apr 22, 2005
  5. Thomas E Enebo

    Class.new and extending it...

    Thomas E Enebo, Sep 11, 2005, in forum: Ruby
    Replies:
    2
    Views:
    110
    Thomas E Enebo
    Sep 11, 2005
Loading...

Share This Page