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

R

Roedy Green

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://mindprod.com/jgloss/thread.html
 
J

junzhang1983

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.

Seehttp://mindprod.com/jgloss/thread.html

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
}
}
 
K

Knute Johnson

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

Calling run() directly causes the code of the run() method to be
executed in the same thread that you called it from. Calling
Thread.start() causes the run() method to be executed in another thread.

Parallel execution versus linear execution.
 
R

Roedy Green

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

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

Roedy Green

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

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.
 

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
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top