Who can tell me how this program runs?

Discussion in 'Java' started by Jack Dowson, May 2, 2007.

  1. Jack Dowson

    Jack Dowson Guest

    Hello Everybody:

    Here is a program written to learn the multithread character of java!
    class NewThread4 extends Thread{
    NewThread4(String name){
    super(name);
    }
    public void run(){
    for(int count = 10,row = 0; row<10 ; row++){
    for(int i = 0; i< count ; i++)
    System.out.print('*');
    System.out.println();
    }
    System.out.println(currentThread().getName() + " is over");
    }
    }
    class ThreadDemo4{
    public static void main(String[] args){
    NewThread4 thd = new NewThread4("new Thread");
    thd.start();
    for(int i =0 ; i<10; i++){
    System.out.println(Thread.currentThread().getName()+ " is running");
    }
    System.out.println(Thread.currentThread().getName() + " is over!");
    }
    }

    The output is out of my anticipation!
    I don't know how it works?


    Any reply will be greatly appreciated!
    Jack Dowson, May 2, 2007
    #1
    1. Advertising

  2. Jack Dowson

    Daniel Pitts Guest

    On May 2, 10:16 am, Jack Dowson <> wrote:
    > Hello Everybody:
    >
    > Here is a program written to learn the multithread character of java!
    > class NewThread4 extends Thread{
    > NewThread4(String name){
    > super(name);
    > }
    > public void run(){
    > for(int count = 10,row = 0; row<10 ; row++){
    > for(int i = 0; i< count ; i++)
    > System.out.print('*');
    > System.out.println();
    > }
    > System.out.println(currentThread().getName() + " is over");
    > }
    > }
    > class ThreadDemo4{
    > public static void main(String[] args){
    > NewThread4 thd = new NewThread4("new Thread");
    > thd.start();
    > for(int i =0 ; i<10; i++){
    > System.out.println(Thread.currentThread().getName()+ " is running");
    > }
    > System.out.println(Thread.currentThread().getName() + " is over!");
    > }
    > }
    >
    > The output is out of my anticipation!
    > I don't know how it works?
    >
    > Any reply will be greatly appreciated!


    Well, I'm not sure what you're seeing, but the point is that once you
    call thd.start(), a new thread is spawned, and the run method of your
    NewThread4 is executed in that other thread.

    What this means is that the code in your run() method can run at the
    same time as the code in your main method.
    Daniel Pitts, May 3, 2007
    #2
    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. yogesh
    Replies:
    1
    Views:
    349
    Victor Bazarov
    Mar 14, 2007
  2. broli
    Replies:
    14
    Views:
    536
    Keith Thompson
    Mar 13, 2008
  3. Mike A
    Replies:
    17
    Views:
    242
    Dr John Stockton
    Nov 19, 2003
  4. ä¼è™Ž
    Replies:
    1
    Views:
    371
    Keith Thompson
    Apr 21, 2012
  5. ä¼è™Ž
    Replies:
    3
    Views:
    561
    Nobody
    Apr 22, 2012
Loading...

Share This Page