Can I create a Thread directly

J

Jenny

HI,

Can I create a thread directly and then start it and run it as the code below?

Thread tt = new Thread(); (or Thread tt = new Thread("thread");)
tt.start();
tt.run();


If this is not possible, what is the use of these two methods?
 
P

Paul Lutus

Jenny said:
HI,

Can I create a thread directly and then start it and run it as the code
below?

Thread tt = new Thread(); (or Thread tt = new Thread("thread");)
tt.start();
tt.run();

You don't do both of these. The calling code calls start(), the thread code
calls run(). If you call run() also, havoc may result. Also, ordinarily,
you give the thread something to do.
 
N

Niels Dybdahl

Can I create a thread directly and then start it and run it as the code
below?
Thread tt = new Thread(); (or Thread tt = new Thread("thread");)
tt.start();
tt.run();

If this is not possible, what is the use of these two methods?

You should do it as follows:

Thread tt = new Thread() {
public void run() {
// Add your thread code here
}
};
tt.start();

Niels Dybdahl
 
N

Nick Pomfret

Jenny said:
HI,

Can I create a thread directly and then start it and run it as the code below?

Thread tt = new Thread(); (or Thread tt = new Thread("thread");)
tt.start();
tt.run();


If this is not possible, what is the use of these two methods?


Only call thread.start()! It will in turn call thread.run() but in a new
thread.
 
J

Joona I Palaste

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top