[3892] in java-interest
Re: [Fwd: start() method confusing -- a really good question.]
daemon@ATHENA.MIT.EDU (Gary Aitken)
Thu Nov 30 22:25:32 1995
Date: Thu, 30 Nov 1995 14:32:19 -0700
From: garya@village.org (Gary Aitken)
To: "John D. Kane" <john@insightnews.com>, java-interest@java.sun.com
In-Reply-To: <30BD0195.5B90@insightnews.com>
>Why is an instance of a Thread often made inside the start method
>with another call to what looks like the same start method to
>start that new thread?
>For example,
> class whatever {
> ...
> public void start() {
> Thread threadToRun = new Thread(this);
> threadToRun.start();
> }
> ...
> }
>I can't understand why this isn't recursive. It looks like start
>calling start calling start calling ... and so on.
>I think it looks like this because the new thread is an instance of
>the same class. Shouldn't the thread call it's own start method,
>which is really the same one it's already in?
>
>I need some help with this. I can write applets using this method,
>but it drives me crazy that I can't understand why it works. If
>anyone knows the answer or has any theories, or knows where there's
>some good documentation on the whole issue of start(), run(),
>stop(), but more specifically start(), please let me know.
Assuming your example above is correctly typed:
Class whatever has a start method, but is not derived from class Thread.
Consequently, it is totally unrelated to Thread, and is not itself a thread.
It has a method named start, which appears to match the same name in the
Thread class, but this is mere coincidence. Your whatever::start() does
*not* override Thread::start, and could just as well be named something else.
Consequently, in order to get it to run in a thread, you need to instantiate a
Thread, and pass it the object which is to run in the thread, which must
implement the runnable interface (i.e. override the run() method. If class
whatever was derived from Thread, as in
class whatever extends Thread {...}
then you would have a problem with recursion.
Before going into an explaination of how start and run interact, (I'll be happy
to do so, but it takes time to write anything coherently), try looking at the
source code for Thread. It's in the java/src/java/lang subdir. If it's still
gonzo confusing after that, beat me up.
Gary Aitken garya@village.org
-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com