neha sher wrote:Then why do we need to extend Thread or implement runnable, if we can simply create thread object like any other object and can use thread methods using this instance created.What is the difference?...
SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Avin Sharma wrote:Regards
Can anyone Tell me How many threads are running in Above Asked Program(Simple Main Method program with creating thread instance(with No extends or implements))
Thank you.
sr shashidhar wrote:
Avin Sharma wrote:Regards
Can anyone Tell me How many threads are running in Above Asked Program(Simple Main Method program with creating thread instance(with No extends or implements))
Thank you.
One Thread, "Main Thread"...
in each and every java program always one default thread will be running in the background i.e, "MAIN THREAD"
Steve
Steve Luke wrote:
sr shashidhar wrote:
Avin Sharma wrote:Regards
Can anyone Tell me How many threads are running in Above Asked Program(Simple Main Method program with creating thread instance(with No extends or implements))
Thank you.
One Thread, "Main Thread"...
in each and every java program always one default thread will be running in the background i.e, "MAIN THREAD"
It's actually a little harder to answer than that, because there are extra system threads hanging around for things like Garbage Collection. You probably need a Profiler to be sure of the running thread count at any given time.
Carlos Borrero wrote:I think I got your question, the answer of How can we create a instance of a Thread Class without having extended it on our current new fresh class is because Thread class is part of the java.lang package which is automatically imported in all the class we create.
Steve
Steve Luke wrote:
Carlos Borrero wrote:I think I got your question, the answer of How can we create a instance of a Thread Class without having extended it on our current new fresh class is because Thread class is part of the java.lang package which is automatically imported in all the class we create.
Hi Carlos, welcome to the Ranch!
That would be a good answer if Neha had asked why she could use (instantiate, reference, etc...) Thread Class without importing it. However s/he was asking about using it without extending it, which is different. You don't, for example, have to extend ArrayList in order to use it, even though it is not in the java.lang package. You just have to import it.
When I go back and read the questions again, I think the question is more about why we typically either extend the Thread class or provide a Runnable to it, when there is a way to make a Thread without doing either of these things (using the empty constructor Thread()). I don't think the question was satisfactorily answered. The answer really is: you can create a Thread using the default no-args constructor, and you can even run it, but because it doesn't have a Runnable and you haven't overridden the Thread's run() method, the newly created OS thread doesn't do anything. It runs and finishes right away. You extend Thread to provide a run() method which does something in the newly created OS thread. Or you provide a Runnable instance to execute the Runnable's run() method in the new OS thread. If you don't do either of these things no code is executed.
Raj Shekhars wrote:
@ steve luke, here i have the run method overridden, and it still works without extending THREAD or implementing Runnable
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |