Only call thread.start()! It will in turn call thread.run() but in a new
thread.
To clarify: Calling Thread.start() is the only way to actually get a new
thread parallel to the currently executing thread. However, what happens
in this new thread is a bit more complicated. Every Thread object needs
to have hold of of an object implementing the Runnable interface, which
specifies a method called run(). This method can be thought of meaning
"do whatever this new thread is supposed to actually do". The Thread
class itself implements Runnable, so you have two ways of telling your
new thread what to do:
1) Use the Thread object itself. Create the object with new Thread().
The default implementation of run() in Thread is empty, so if your
object is of class Thread and nothing else, you end up with a thread
that ends straight away. But you can subclass Thread and override the
run() method to do something different. Because Thread has a default
constructor Thread() with no arguments, Java should automatically call
it for you from your constructor.
2) Use a different Runnable object. Create the object with new
Thread(runnable) where runnable is an object implementing Runnable.
You have to construct this runnable object yourself, of course. How you
do it depends on the object.
Regardless of whether you do (1) or (2), you must NOT call any run()
method yourself! The method start() in Thread will call it for you.
If you call run() yourself, the method will execute in the currently
executing thread - NOT in your new thread - and you might end up with
two threads doing the same thing at the same time.
Of course if you like, you can forget about the Thread class and call
the run() method in a Runnable object yourself, but then it's the same
thing as calling any other method in any other class. You get a normal,
blocking execution, not a parallel execution in another thread.
--
/-- Joona Palaste (
[email protected]) ------------- Finland --------\
\--
http://www.helsinki.fi/~palaste --------------------- rules! --------/
"You can pick your friends, you can pick your nose, but you can't pick your
relatives."
- MAD Magazine