wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Why java has 2 ways to create thread ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why java has 2 ways to create thread ?" Watch "Why java has 2 ways to create thread ?" New topic
Author

Why java has 2 ways to create thread ?

Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391

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

Joined: Oct 25, 2008
Posts: 2700

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.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19757
    
  20

http://faq.javaranch.com/java/ExtendingThreadVsImplementingRunnable


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391

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

Joined: Oct 27, 2005
Posts: 19757
    
  20

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

Joined: Oct 04, 2006
Posts: 391
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

Joined: Oct 27, 2005
Posts: 19757
    
  20

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

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Santosh Kumar Nayak
Ranch Hand

Joined: Aug 02, 2011
Posts: 107
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

Joined: Apr 06, 2010
Posts: 4457
    
    8

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why java has 2 ways to create thread ?