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,
09-10-2000, 05:36 PM
John Timney (MVP)
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.
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,
09-12-2000, 09:14 AM
Mark Brubaker
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();
}
"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,
>
>
09-12-2000, 05:17 PM
John Timney (MVP)
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.
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();
}
"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,
>
>