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

Discussion in 'Java' started by junzhang1983@gmail.com, Jun 17, 2008.

  1. Guest

    3ks
     
    , Jun 17, 2008
    #1
    1. Advertising

  2. Roedy Green Guest

    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
     
    Roedy Green, Jun 17, 2008
    #2
    1. Advertising

  3. Guest

    On Jun 17, 4:42 pm, Roedy Green <>
    wrote:
    > 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.
    >
    > 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
    }
    }
     
    , Jun 20, 2008
    #3
  4. wrote:
    > On Jun 17, 4:42 pm, Roedy Green <>
    > wrote:
    >> 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.
    >>
    >> 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
     
    Knute Johnson, Jun 20, 2008
    #4
  5. Roedy Green Guest

    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
     
    Roedy Green, Jun 20, 2008
    #5
  6. Roedy Green Guest

    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
     
    Roedy Green, Jun 20, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marc Twain
    Replies:
    1
    Views:
    377
    Roedy Green
    Jan 13, 2004
  2. Kerry Kimbrough
    Replies:
    6
    Views:
    15,008
    Chris Smith
    Feb 27, 2004
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    986
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,238
    Smokey Grindel
    Dec 2, 2006
  5. Replies:
    2
    Views:
    133
Loading...

Share This Page