why we should call Thread.start(),not directly call Thread.run()?

why we should call Thread.start(),not directly call Thread.run()?

Post by junzhang19 » Wed, 18 Jun 2008 16:28:35


3ks
 
 
 

why we should call Thread.start(),not directly call Thread.run()?

Post by Roedy Gree » Wed, 18 Jun 2008 17:42:09

On Tue, 17 Jun 2008 00:28:35 -0700 (PDT), XXXX@XXXXX.COM
wrote, quoted or indirectly quoted someone who said :

I hope now you see why you should echo your question in the body of
your post.

Try it. run is your method. Nothing exciting will happen. It will
just execute your method on the current thread. When you call start,
it execute's Sun's code that does the magic of creating a new thread,
then calling run on it.

See http://www.yqcomputer.com/
--

Roedy Green Canadian Mind Products
The Java Glossary
http://www.yqcomputer.com/

 
 
 

why we should call Thread.start(),not directly call Thread.run()?

Post by junzhang19 » Sat, 21 Jun 2008 11:29:56

On Jun 17, 4:42 pm, Roedy Green < XXXX@XXXXX.COM >


first ,thanks you answer,but l still have a little problem

for example:

class A{
void testThread(){
MyThread thread = new MyThread();
//we often call thread .start(),not thread.run() to run a
thread,
//now that l am already create a new Thread above,
//why l need call sun's code which is method start()
// to create a new thread?why call run() is not ok?
}
}

class MyThread extends Thread{
public void run(){
//do something
}
}
 
 
 

why we should call Thread.start(),not directly call Thread.run()?

Post by Roedy Gree » Sat, 21 Jun 2008 21:16:25

On Thu, 19 Jun 2008 19:29:56 -0700 (PDT), XXXX@XXXXX.COM
wrote, quoted or indirectly quoted someone who said :


I suggest rereading my answer and the link I pointed you to at least
10 times. Threads baffle even the most experienced programmers.

I also suggest having a look at the source code for run and start.

Run is an abstract method. You override it. When you run it there is
NOTHING there BUT your code. It could not possible do anything fancy
like start a thread. Some other method, namely start, has to do
that.

There is no mechanism in Java to insert extra code in the front of
some a method.

So no way Sun could insert code to start a thread at the top of your
run method. There has to be a second method that starts the thread,
then calls your run.

--

Roedy Green Canadian Mind Products
The Java Glossary
http://www.yqcomputer.com/
 
 
 

why we should call Thread.start(),not directly call Thread.run()?

Post by Lew » Sat, 21 Jun 2008 23:16:48


In Runnable, yes. In Thread it is a concrete method that does nothing.


That is true.

--
Lew
 
 
 

why we should call Thread.start(),not directly call Thread.run()?

Post by Roedy Gree » Sun, 22 Jun 2008 00:12:36

On Fri, 20 Jun 2008 12:16:25 GMT, Roedy Green
< XXXX@XXXXX.COM > wrote, quoted or indirectly quoted
someone who said :


I have an assembler background. So they way I approach such a problem
is trying to get a model in my head of how it works UNDER THE HOOD.
Then I don't have to remember a million details. They are all natural
consequences of how that rough model works inside.

One of the first things I do is have a look at source code to try to
get a general picture of how things work. When I do that, all manner
of quirky behaviours cease to seem so quirky, just the side effect of
overly bottom up designing.

So have a look inside src.zip. Most IDEs have tools to let you rapidly
navigate around, or single step programs to watch how it all fits
together.
--

Roedy Green Canadian Mind Products
The Java Glossary
http://www.yqcomputer.com/