Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > why we should call Thread.start(),not directly call Thread.run()?

Reply
Thread Tools

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

 
 
junzhang1983@gmail.com
Guest
Posts: n/a
 
      06-17-2008
3ks
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      06-17-2008
On Tue, 17 Jun 2008 00:28:35 -0700 (PDT),
wrote, quoted or indirectly quoted someone who said :

>3ks

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

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
 
 
 
junzhang1983@gmail.com
Guest
Posts: n/a
 
      06-20-2008
On Jun 17, 4:42 pm, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:
> On Tue, 17 Jun 2008 00:28:35 -0700 (PDT), junzhang1...@gmail.com
> wrote, quoted or indirectly quoted someone who said :
>
> >3ks

>
> 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
> --
>
> Roedy Green Canadian Mind Products
> The Java Glossaryhttp://mindprod.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
}
}
 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      06-20-2008
wrote:
> On Jun 17, 4:42 pm, Roedy Green <see_webs...@mindprod.com.invalid>
> wrote:
>> On Tue, 17 Jun 2008 00:28:35 -0700 (PDT), junzhang1...@gmail.com
>> wrote, quoted or indirectly quoted someone who said :
>>
>>> 3ks

>> 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
>> --
>>
>> Roedy Green Canadian Mind Products
>> The Java Glossaryhttp://mindprod.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
> }
> }


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.

--

Knute Johnson
email s/nospam/knute2008/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      06-20-2008
On Thu, 19 Jun 2008 19:29:56 -0700 (PDT),
wrote, quoted or indirectly quoted someone who said :

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

--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      06-20-2008
On Fri, 20 Jun 2008 12:16:25 GMT, Roedy Green
<> wrote, quoted or indirectly quoted
someone who said :

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

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Object.prototype.toString(): directly invoking vs. using call() --why different results ? javadesigner@yahoo.com Javascript 2 04-07-2009 01:06 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
AWT: Why not call update() directly? Marc Twain Java 1 01-13-2004 10:43 PM
Preview image directly on PC, save directly to HD Patrick M. Digital Photography 3 01-07-2004 08:29 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57