• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Threads...

 
Ranch Hand
Posts: 193
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On the exam, if I ever see code that tries to start a Thread object by calling the run() method. Considering the question is about threads, is there ever an instance were this could be correct?

I think I am correct in saying that the run() method does not start a thread but just executes the code in the run() method. Just like it would execute in any old non-threaded method call where the method just happens to be named run(). Correct?
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Higgledy Smith wrote:On the exam, if I ever see code that tries to start a Thread object by calling the run() method. Considering the question is about threads, is there ever an instance were this could be correct?

I think I am correct in saying that the run() method does not start a thread but just executes the code in the run() method. Just like it would execute in any old non-threaded method call where the method just happens to be named run(). Correct?


Yes, you are absolutely correct. If you call run() on a Thread object you are just starting a new method stack in the current thread. You need to call start() on the thread, and that starts a new thread of execution which will eventually run the run() code (either of the Runnable object passed to the Thread constructor, or the run code in the Thread object itself.)
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that if you call the t.run() (t is a thread)
the run() method executes but not in a separate thread.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I think I am correct in saying that the run() method does not start a thread but just executes the code in the run() method.



When you write t.run() , a seperate call stack is not created. Whereas when you write t.start() a seperate call stack is created and the thread moves from new state to runnable state. It becomes a thread of execution.
Again the order is which t.start() is called on seperate threads which thread will run first is not guaranteed.It depends upon the VM which decides as per the priority of the theads.
Please note if you call t.start() on the same thread instance it causes a runtime error.




SCJP 5.0 --> 95%
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic