• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread, start(), run() confusion

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is making me a little crazy:



Ok. So the first thread.run() calls the run() method defined in new Runnable(), but why isn't thread.start() necessary before calling thread.run()?

I told myself maybe thread.start() isn't needed when you are using Runnable.

So then why doesn't thread.start() cause some kind of exception because the thread is already running?

By the time we get to the 2nd thread.run(), I don't know what the heck is going on.

Are there 2 different threads? What did thread.start() really start?

Why does this thing work?

Mark
 
Greenhorn
Posts: 9
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

thread.run();


The above is like calling a method on object.It is executed in the same call stack.Does not create new thread.

thread.start();


Calling start() actually creates new Thread of execution.

If you add the above SOP to your run method you can see what happens.
 
mark juszczec
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, NOW I get it.

I was stuck thinking in "Thread mode" where everything in the problem was a thread.

 
Ranch Hand
Posts: 89
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is really a good one! Keep up in knowing the pitfalls
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic