• 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

Why java has 2 ways to create thread ?

 
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Why java has 2 ways to create Threads.
1) Thread Class
2) Runnable INTERFACE

i) Thread class won't allow you to extend more class while interface allows you
ii) Runnable has only one method need to implement mandatory while Thread has methods other than Run also.

What else benefits we have ?


Regards,
Prabhat
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java has only 1 way to create a thread. With the thread class (or a child of it). However it can be used in combination with the runnable interface.
It's just an abstraction of the code to be executed. It's generally discouraged to extend the Thread class. The Thread class has a lot of overhead
and the Runnable interface doesn't.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://faq.javaranch.com/java/ExtendingThreadVsImplementingRunnable
 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I have few comments over this point:

1) Interface Runnable have only 1 method which you mandatory to implement.
2) If you need other methods like suspend() resume() sleep() join() yield() and stop() then
go for extending class Thread
3) Extending the Thread class will make your class unable to extend other classes, because of the single inheritence feature in JAVA.
4) If you want to execute run() method for multiple no of times then its better to you Runnable.

public class testing implements Runnable {

public void run() {
System.out.println("Hello Run --->");
}
public static void main(String args[]){

testing testing = new testing();
Thread thd = new Thread(testing);

thd.run();
thd.run();
}
}

While Thread class doesn't allow yo to call the start() methods more than once.
will throw IllegalThreadStateException.

5) Thread Class actually implements Runnable interface internally.
 
Rob Spoor
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prabhat Ranjan wrote:2) If you need other methods like suspend() resume() sleep() join() yield() and stop() then go for extending class Thread


Please don't. suspend(), resume() and stop() are deprecated and should not be used. sleep() and yield() are static and cannot be overridden. join() is final and cannot be overridden.

By just instantiating a new Thread object you can make use of join() without ever needing to extend it. You don't need to extend a class to be able to call its public methods.

5) Thread Class actually implements Runnable interface internally.


Not just internally, also externally
The implements clause is not an implementation detail but part of its API. But yes, it does implement Runnable.
 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for your answer !

others points are fine point no 1,3 ,4

About point 2 , i have clarification that if methods are deprecated means we cann't use in real world programming.

What else benefits on using Runnable over Thread ?


please let me know
 
Rob Spoor
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prabhat Ranjan wrote:About point 2 , i have clarification that if methods are deprecated means we cann't use in real world programming.


Well, you can. You just shouldn't. They are deprecated for a reason; either they can cause a lot of problems (like Thread.resume(), Thread.suspend(), Thread.stop()) or there are better alternatives.

What else benefits on using Runnable over Thread ?


Aren't points 3 and 4 enough? No? Then how about sharing one Runnable instance among multiple Threads at the same time?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this point has already been made, but to reiterate: According to Cay S. Horstmann and Gary Cornell in Core Java 2: Volume II - Advanced Features...

...forming a subclass of the Thread class ... is no longer recommended. You should decouple the task that is to be run in parallel from the mechanism of running it.

 
Ranch Hand
Posts: 230
IntelliJ IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you extend Thread, your thread creates unique object and associate with it.
When you implement Runnable, it shares the same object to multiple threads.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd look at it this way. What is a Thread? It represents a process running on the machine, that can execute some actions. What is a Runnable? It represents an action to execute. These are really two completely different concepts - so general object-oriented principles suggest that using two different objects is likely to be preferable.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By reading the above explanation I understood that what are the benefits of either going for extends Thread or implements Runnable and its better to use Runnable than Why Java has still provide two ways to create the Thread ?
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kishan Jaiswal wrote:By reading the above explanation I understood that what are the benefits of either going for extends Thread or implements Runnable and its better to use Runnable than Why Java has still provide two ways to create the Thread ?



Apparently you missed reading the very first response to the original post, then. Have a look... it's the one which starts "Java has only 1 way to create a thread."
reply
    Bookmark Topic Watch Topic
  • New Topic