This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Relationship between Thread class and Runnable interface? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Relationship between Thread class and Runnable interface?" Watch "Relationship between Thread class and Runnable interface?" New topic
Author

Relationship between Thread class and Runnable interface?

Anto Telvin
Ranch Hand

Joined: Aug 12, 2008
Posts: 113
hi all

what is the relationship between Thread class and Runnable Interface ?Why it is not necessary to create a Thread object for creating Threads if we are extending it.But if we are implementing it we have to explicitly create an Object of Thread.so what make this difference ?


Anto Telvin Mathew<br />Many of the life failures are people who did not realize how close they were to success when they give up. EDISON
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41068
    
  43
You always have to create an object, whether it extends Thread or implements Runnable. See ExtendingThreadVsImplementingRunnable for some discussion which method is appropriate in which case.


Ping & DNS - my free Android networking tools app
ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146
Originally posted by Anto telvin Mathew:
hi all

what is the relationship between Thread class and Runnable Interface ?Why it is not necessary to create a Thread object for creating Threads if we are extending it.But if we are implementing it we have to explicitly create an Object of Thread.so what make this difference ?


The answer is simple watch the API Specification :

public class Thread extends Object implements Runnable

To be more precise :
Remember :

To make a thread object you have to say

Thread objThread = new Thread(anyobject of type class that implements runnable);

To execute that object as a thread say

Thread.start();

will internally call run() method;

This is the way to create and start a thread !


The Class Thread has a constructor of version

Thread(Runnable target)
Allocates a new Thread object.

The interface Runnable has a method run()

The part of code inside run() will be considered as a single unit of execution.



Why it is not necessary to create a Thread object for creating Threads if we are extending it.

If you extend it implicitly extends thread and implements Runnable

Where the run() method is implicitly available.

Just say Object.start(); //will call run internally

Thats why creating a class that extends thread need not explicitly need to create a thread object .

You are making the object to be a thread - there itself !


if we are implementing it we have to explicitly create an Object of Thread.so what make this difference ?

If you implement Runnable, you say that the object capable to run as a Thread.

So you have to pass it to the Thread Constructor - Thread(Runnable)

Then, the Thread would consider and execute this object as a Thread.

Invoiking the run() method internally !


so what make this difference ?

  • In both cases you have to Implement Run() method(Mandatory)
  • Without calling .start on an object - Thread will not start !
  • I can give an example, but try yourself with the information.
  • else, you will not know the difference by yourself !
  • Implementing through Runnable is the best way.
  • See tutorial pages of sun


  • Hope this helps !
    [ October 15, 2008: Message edited by: ram kumar ]

    Discussion - the powerfull way to excellence!
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 37937
        
      22
    [Pedantic mode]It's not Object, but object, and it's not Run() but run(). You can get into no end of confusion if you forget that Java is case-sensitive.[/pedantic mode]
    ram kumar
    Ranch Hand

    Joined: May 22, 2008
    Posts: 146
    Originally posted by Campbell Ritchie:
    [Pedantic mode]It's not Object, but object, and it's not Run() but run(). You can get into no end of confusion if you forget that Java is case-sensitive.[/pedantic mode]


    Agreed ! Campbell !

    But that's why i did not put them in the Code tag !

    If i had used code tag - I would have said Object as object and Run as run()

    I just wanted to give some importance to the explanation than on code level work !

    Will follow as you said !

    what is pedantic mode !
    Anto Telvin
    Ranch Hand

    Joined: Aug 12, 2008
    Posts: 113
    thank you
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 37937
        
      22
    Originally posted by ram kumar:
    what is pedantic mode !


    _____
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Relationship between Thread class and Runnable interface?
     
    Similar Threads
    Diff between Thread and Runnable
    Runnable vs Thread
    Doubt in Threads
    Thread concepts
    has-a and is-a relationships question