Java Thread


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Java Thread

  1. #1
    Kevin Chien Guest

    Java Thread


    A very simple question for all Java gurus.
    I have a program looks like this:

    ........
    public class TestThread extends Thread
    { ........
    ........
    public static void main(String[] args)
    {
    TestThread t1 = new Thread();
    TestThread t2 = new Thread();
    t1.start();
    t2.start();
    }

    public void run()
    {
    while(true)
    {
    // do something
    }
    }
    }
    =====================================
    My question is: Does the main thread get terminated right after spawning
    two threads?

    Thanks,

  2. #2
    John Timney (MVP) Guest

    Re: Java Thread

    No ...

    If you think about the context the threads run in, they belong to the
    TestThread class, so if it dies so do the threads it has spawned. The thing
    about threads is that they can run while something else is happening hence
    there is no reason for the spawning bit to die.

    --
    Regards

    John Timney
    Microsoft MVP
    (http://support.microsoft.com/support/mvp/program.asp)
    Co-Author Professional JSP
    ISBN: 1-861003-62-5



    Kevin Chien <kvnchien@yahoo.com> wrote in message
    news:39b95a20$1@news.devx.com...

    A very simple question for all Java gurus.
    I have a program looks like this:

    ........
    public class TestThread extends Thread
    { ........
    ........
    public static void main(String[] args)


    TestThread t1 = new Thread();
    TestThread t2 = new Thread();
    t1.start();
    t2.start();
    }

    public void run()


    while(true)


    // do something
    }
    }
    }
    =====================================
    My question is: Does the main thread get terminated right after spawning
    two threads?

    Thanks,



  3. #3
    Mark Brubaker Guest

    Re: Java Thread


    WRONG! Sorry to disagree with you John, but this is incorrect. You are making
    the typical mistake of confusing a thread with an instance of the java.lang.Thread
    class.

    In the sample code that Kevin wrote, there are 3 threads, but only 2 java.lang.Thread
    objects. The first thread is the main thread which starts executing the
    main() method, which is a static class method - there is no instance of this
    class yet. This main thread then creates the two java.lang.Thread objects
    t1 and t2 and starts two other threads of execution when t1.start() and t2.start()
    are called. The main thread then dies.

    The fact that the original main thread dies does NOT mean that either t1,
    t2, or the TestThread class die. The program will continue to execute until
    all non-daemon threads die. In this case, the threads represented by the
    t1 and t2 Thread objects are non-daemon threads, so the program continues.

    The main thing to remember is this: a thread of execution is NOT an object.
    A java.lang.Thread object has methods which allow the creation of threads
    of execution.

    Here is some revised code which should demonstrate the fact that the main
    thread does indeed die:

    public class TestThread extends Thread
    {
    public static void main(String[] args)
    {
    Thread theMainThread = Thread.currentThread();
    System.out.println("The Main Thread is: " + theMainThread);
    TestThread t1 = new TestThread(theMainThread);
    TestThread t2 = new TestThread(theMainThread);
    t1.start();
    t2.start();
    }

    private Thread checkThread;

    TestThread(Thread otherThread)
    {
    checkThread = otherThread;
    }

    public void run()
    {
    Thread thisThread = Thread.currentThread();
    while(true) {
    try {
    System.out.print("In " + thisThread + ", " + checkThread + " is ");
    if (checkThread.isAlive()) {
    System.out.println("ALIVE");
    } else {
    System.out.println("DEAD");
    }
    Thread.currentThread().sleep(5000);
    } catch (Throwable t) {
    System.out.println("Caught: " + t);
    }
    }
    }
    }


    "John Timney (MVP)" <xyztimneyj@btinternet.com> wrote:
    >No ...
    >
    >If you think about the context the threads run in, they belong to the
    >TestThread class, so if it dies so do the threads it has spawned. The thing
    >about threads is that they can run while something else is happening hence
    >there is no reason for the spawning bit to die.
    >
    >--
    >Regards
    >
    >John Timney
    >Microsoft MVP
    >(http://support.microsoft.com/support/mvp/program.asp)
    >Co-Author Professional JSP
    >ISBN: 1-861003-62-5
    >
    >
    >
    >Kevin Chien <kvnchien@yahoo.com> wrote in message
    >news:39b95a20$1@news.devx.com...
    >
    >A very simple question for all Java gurus.
    >I have a program looks like this:
    >
    >........
    >public class TestThread extends Thread
    >{ ........
    > ........
    > public static void main(String[] args)
    >
    >
    > TestThread t1 = new Thread();
    > TestThread t2 = new Thread();
    > t1.start();
    > t2.start();
    > }
    >
    > public void run()
    >
    >
    > while(true)
    >
    >
    > // do something
    > }
    > }
    >}
    >=====================================
    >My question is: Does the main thread get terminated right after spawning
    >two threads?
    >
    >Thanks,
    >
    >



  4. #4
    John Timney (MVP) Guest

    Re: Java Thread

    You have me there Mark ... it was a long day (feeble excuse) and thats a
    good answer.

    My apologies, Marks explanation is much more in tune with what happens to
    the actual thread processing behind the scenes in your example, in your
    example there are no dependencies on the main thread, so the thread could
    indeed happily die and leave the remaining threads in state.

    --
    Regards

    John Timney
    Microsoft MVP
    (http://support.microsoft.com/support/mvp/program.asp)
    Co-Author Professional JSP
    ISBN: 1-861003-62-5



    Mark Brubaker <mark@infinitetechnology.com> wrote in message
    news:39be2c24$1@news.devx.com...

    WRONG! Sorry to disagree with you John, but this is incorrect. You are
    making
    the typical mistake of confusing a thread with an instance of the
    java.lang.Thread
    class.

    In the sample code that Kevin wrote, there are 3 threads, but only 2
    java.lang.Thread
    objects. The first thread is the main thread which starts executing the
    main() method, which is a static class method - there is no instance of this
    class yet. This main thread then creates the two java.lang.Thread objects
    t1 and t2 and starts two other threads of execution when t1.start() and
    t2.start()
    are called. The main thread then dies.

    The fact that the original main thread dies does NOT mean that either t1,
    t2, or the TestThread class die. The program will continue to execute until
    all non-daemon threads die. In this case, the threads represented by the
    t1 and t2 Thread objects are non-daemon threads, so the program continues.

    The main thing to remember is this: a thread of execution is NOT an object.
    A java.lang.Thread object has methods which allow the creation of threads
    of execution.

    Here is some revised code which should demonstrate the fact that the main
    thread does indeed die:

    public class TestThread extends Thread
    {
    public static void main(String[] args)
    {
    Thread theMainThread = Thread.currentThread();
    System.out.println("The Main Thread is: " + theMainThread);
    TestThread t1 =
    new TestThread(theMainThread);
    TestThread t2 = new TestThread(theMainThread);
    t1.start();
    t2.start();
    }

    private Thread checkThread;

    TestThread(Thread otherThread)
    {
    checkThread = otherThread;
    }

    public void run()
    {
    Thread thisThread = Thread.currentThread();
    while(true) {
    try {
    System.out.print("In " + thisThread + ", " + checkThread + " is ");
    if (checkThread.isAlive()) {
    System.out.println("ALIVE");
    } else {
    System.out.println("DEAD");
    }
    Thread.currentThread().sleep(5000);
    } catch (Throwable t) {
    System.out.println("Caught: " + t);
    }
    }
    }
    }


    "John Timney (MVP)" <xyztimneyj@btinternet.com> wrote:
    >No ...
    >
    >If you think about the context the threads run in, they belong to the
    >TestThread class, so if it dies so do the threads it has spawned. The

    thing
    >about threads is that they can run while something else is happening hence
    >there is no reason for the spawning bit to die.
    >
    >--
    >Regards
    >
    >John Timney
    >Microsoft MVP
    >(http://support.microsoft.com/support/mvp/program.asp)
    >Co-Author Professional JSP
    >ISBN: 1-861003-62-5
    >
    >
    >
    >Kevin Chien <kvnchien@yahoo.com> wrote in message
    >news:39b95a20$1@news.devx.com...
    >
    >A very simple question for all Java gurus.
    >I have a program looks like this:
    >
    >........
    >public class TestThread extends Thread
    >{ ........
    > ........
    > public static void main(String[] args)
    >
    >
    > TestThread t1 = new Thread();
    > TestThread t2 = new Thread();
    > t1.start();
    > t2.start();
    > }
    >
    > public void run()
    >
    >
    > while(true)
    >
    >
    > // do something
    > }
    > }
    >}
    >=====================================
    >My question is: Does the main thread get terminated right after spawning
    >two threads?
    >
    >Thanks,
    >
    >





Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center