It's not a secret anymore!
The moose likes Threads and Synchronization and the fly likes class calling thread without extending Thread class Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "class calling thread without extending Thread class" Watch "class calling thread without extending Thread class" New topic
Author

class calling thread without extending Thread class

neha sher
Greenhorn

Joined: Apr 07, 2011
Posts: 7
Hello friends,

This is just beginner's question.Please help me with this

How can any class call thread without extending Thread class ?

Thanks
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 3791
    
    1

Hi Neha. Welcome to The Ranch!

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.

See our ExtendingThreadVsImplementingRunnable FAQ for a bit more detail. The Java Concurrency Tutorial is also a good place to look.
isha krishnan
Ranch Hand

Joined: Nov 10, 2008
Posts: 50
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
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...
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 3791
    
    1

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
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
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.



Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3670

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.



SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Avin Sharma
Greenhorn

Joined: Jan 17, 2013
Posts: 5

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
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 Luke
Bartender

Joined: Jan 28, 2003
Posts: 3031
    
    4

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.


Steve
Avin Sharma
Greenhorn

Joined: Jan 17, 2013
Posts: 5

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.


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. ??
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35237
    
    7
This should be of interest: https://www.coderanch.com/how-to/java/ThreadLister


Android appsImageJ pluginsJava web charts
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: class calling thread without extending Thread class
 
Similar Threads
Extending Thread or implementing Runnable
Is this correct ?
Servlets class
Marcus Green #3 Question 44
best way to create a thread