Can I create a Thread directly  
Author Message
jenny_jones_79





PostPosted: 2004-8-30 6:59:00 Top

java-programmer, Can I create a Thread directly 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?
 
Liz





PostPosted: 2004-8-30 7:34:00 Top

java-programmer >> Can I create a Thread directly sure, why not just try it

"Jenny" <email***@***.com> wrote in message
news:email***@***.com...
> 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?


 
Paul Lutus





PostPosted: 2004-8-30 7:50:00 Top

java-programmer >> Can I create a Thread directly Jenny wrote:

> 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.

--
Paul Lutus
http://www.arachnoid.com

 
 
Niels Dybdahl





PostPosted: 2004-8-30 15:33:00 Top

java-programmer >> Can I create a Thread directly > 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


 
 
Nick Pomfret





PostPosted: 2004-8-30 18:57:00 Top

java-programmer >> Can I create a Thread directly "Jenny" <email***@***.com> wrote in message
news:email***@***.com...
> 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.
--

** http://www.tabletoolkit.com **
Aggregate a JTable to an arbitrary level to create your own pivot tables



 
 
Joona I Palaste





PostPosted: 2004-8-30 23:30:00 Top

java-programmer >> Can I create a Thread directly Nick Pomfret <email***@***.com> scribbled the following:
> "Jenny" <email***@***.com> wrote in message
> news:email***@***.com...
>> 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.

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***@***.com) ------------- 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