aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads..." Watch "Threads..." New topic
Author

Threads...

Higgledy Smith
Ranch Hand

Joined: Mar 07, 2006
Posts: 192
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

Joined: Dec 16, 2008
Posts: 1032
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.)


All code in my posts, unless a source is explicitly mentioned, is my own.
victor kamat
Ranch Hand

Joined: Jan 10, 2007
Posts: 247
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

Joined: Mar 12, 2009
Posts: 8
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
 
subject: Threads...