If you look at the documentation for the Thread class, you'll see there's a constructor that takes a Runnable object. So you can create a Runnable object, and pass that to a plain Thread.
Have you read about Thread instantiation by Implementing Runnable Interface?That could be other way to create a thread without extending Thread Class..
neha sher
Greenhorn
Joined: Apr 07, 2011
Posts: 7
posted
0
Thanks for reply.
I think my question was not clear.
I want to know how is it possible to create thread object within a class without extending thread class or implementing Runnable interface?
Following code works fine without any compilation error.
public class Try {
public static void main(String[] args) {
Thread t = new Thread();
}
}
Is it because Thread is a sublass of Object class and this is example of composition?
I am not sure...
Thread is just a class like any other class. You can create a Thread in exactly the same way as you create an instance of any other class - with the new keyword. Do you understand how you create objects?
neha sher
Greenhorn
Joined: Apr 07, 2011
Posts: 7
posted
0
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? Please explain.
Question may be very simple For most of the friends here, but want to understand this.
isha krishnan
Ranch Hand
Joined: Nov 10, 2008
Posts: 50
posted
1
Hi Neha,
There are two options to create and execute a thread:
1.Class thread extends Thread
2 Class thread implements Runnable
Your Question is why do we need to extend Thread when we can directly instantiate it.
Thread t= new Thread();
This is simple intstance of Thread class.How will you customize it to your usage?
if you call t.start()
it will start a thread but it won't be your class which you wish to run as Thread since it dosen't know about your run() method.
So you need to tell,which thread you want to run and which is done by exposing your class as a thread either by implementing or extending.
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?...
As already pointed out you need to provide what the thread should do either by extending the Thread class or implementing Runnable interface. One difference is that if you extend the Thread class you will not able to extend anything else, so implementing Runnable is preferred in these cases. And you get to separate the code for the actual work from your Thread class itself.
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
Greenhorn
Joined: Jan 17, 2013
Posts: 21
posted
0
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"
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.
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.
What do you mean Steve ?
@shashi- I know Main thread is there. But Dont you think there are 2 Threads here. One is Main Thread and another is of Thread class. ??