Forums Register Login

Threads...

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
 

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.)
+Pie Number of slices to send: Send
Note that if you call the t.run() (t is a thread)
the run() method executes but not in a separate thread.
+Pie Number of slices to send: Send
 

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%
If you live in a cold climate and on the grid, incandescent light can use less energy than LED. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 766 times.
Similar Threads
Internal Implementation of run()
doubt on threads?
Synchronization
Marcus Green mock exam 1 question 31????
Threads and calling methods on it explicitly
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 07:22:00.