• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

class calling thread without extending Thread class

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello friends,

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

How can any class call thread without extending Thread class ?

Thanks
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.



 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.


 
Greenhorn
Posts: 5
Android PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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"
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Avin Sharma
Greenhorn
Posts: 5
Android PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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. ??
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This should be of interest: https://coderanch.com/how-to/java/ThreadLister
 
Greenhorn
Posts: 22
Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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().
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



@ steve luke, here i have the run method overridden, and it still works without extending THREAD or implementing Runnable

Also, I have an explanation though(which can be wrong), when we use toString() function ,which is an Object class method, but we do not import Object class cause java.lang Package is imported implicitly.So Thread class also belongs to the java.lang package, the same thing might be happening here.

I am a beginner, hence curious to know the right reason.
Thanks, if you reply.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raj Shekhars wrote:
@ steve luke, here i have the run method overridden, and it still works without extending THREAD or implementing Runnable



Anonymous class *does* extend -- it just doesn't use the extends keyword. In your example, you should have an anonymous class that extends the Thread class. Its class file should have a name like Test2$1.class in your file system.

Henry
reply
    Bookmark Topic Watch Topic
  • New Topic