File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads...

 
Higgledy Smith
Ranch Hand
Posts: 192
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Ruben Soto
Ranch Hand
Posts: 1032
  • 0
  • Mark post as helpful
  • send pies
  • 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.)
 
victor kamat
Ranch Hand
Posts: 247
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Rocky Singh
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • 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%
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic