wood burning stoves 2.0*
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 | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
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: 4375
    
    8

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: 4375
    
    8

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: 3827

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: 27
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: 4181
    
  21

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: 41621
    
  55
This should be of interest: https://www.coderanch.com/how-to/java/ThreadLister


Ping & DNS - my free Android networking tools app
Carlos Borrero
Greenhorn

Joined: Jul 24, 2013
Posts: 11

Hi Neha,

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. That's why you don't get an compilation error by creating a Thread object without extending Thread, because your current class already knows this functionality implicitly.
Hope this helps...

java.langĀ 
Class Thread
java.lang.Object


java.lang.Thread
All Implemented Interfaces:
Runnable

From <http://docs.oracle.com/javase/6/docs/api/java/lang/Thread.html>
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.
Aslam anwer
Greenhorn

Joined: Aug 28, 2013
Posts: 1
Thread is class belongs to Java.lang package

And static method can be called without the instance of the class

So you can call Thread.anystaticmethod().
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: class calling thread without extending Thread class