Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why java has 2 ways to create thread ?

 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
  • 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
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://faq.javaranch.com/java/ExtendingThreadVsImplementingRunnable
 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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.
 
Santosh Kumar Nayak
Ranch Hand
Posts: 143
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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.
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic