wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Runnable Interface question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Runnable Interface question" Watch "Runnable Interface question" New topic
Author

Runnable Interface question

John Paul Hoey
Greenhorn

Joined: Apr 15, 2012
Posts: 25
Hi All,

I was wondering if someone could help answer a question for me please in relation to interfaces - in particular the Runnable interface, as there is something about interfaces that im just not grasping.

Ive not long started learning Java so please bear this in mind when answering the question

Its in relation to the following code:

Basically, my question is, how can the Thread class be available for instantiation by only implementing the Runnable interface? How does this relationship work?

The API for the class Thread states the following:

The other way to create a thread is to declare a class that implements the Runnable interface. That class then implements the run method. An instance of the class can then be allocated, passed as an argument when creating Thread, and started.

However, when the Runnable interface isnt implemented, Thread cannot be created and isnt available for instantiation - so where is the relationship held?

Im just not grasping the relationship between the Runnable interface and the creation of Thread objects i.e. By implementing the Runnable interface, this somehow allows me to create Thread objects - why is this?

I understand that when a class implements an interface, that class must cater for those abstract methods defined within the interface, in this instance its the run() method - hence why the run() method has been added to the WhereAmI class, but, by doing this, how does this then equate to being able to create Thread objects within the main class??

I understand how to use Thread objects if i extend the Thread class but not by using the Runnable interface.

Hopefully my question makes sense

Thanks in advance

John Paul
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Hi John,

Take a gander at this topic: which discusses Thread and Runnable. It isn't exactly the question you are asking but is real close.

The basic answer is that the Thread holds a reference to the Runnable Object. The Thread creates a new OS thread and, if the reference to the Runnable is not null, runs the Runnable#run() method. If the reference to the Runnable is null then it does nothing and immediately ends.

The link I provided gives a bit more detail.


Steve
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

John Paul Hoey wrote:However, when the Runnable interface isnt implemented, Thread cannot be created and isnt available for instantiation - so where is the relationship held?


But I am not exactly sure what this part of your post is asking. The relationship is a contract defined as the Thread constructor's signature - the constructor says 'give me something that implements Runnable.' So to use that constructor you need something that IS-A Runnable. So the relationship is 'held' in the Thread Constructor's signature:

John Paul Hoey
Greenhorn

Joined: Apr 15, 2012
Posts: 25
Hi Steve,

Thanks very much for your quick response!

Ill take some time out to read and digest your feedback and the link you provided and if i have any more questions ill certainly come back

Thanks again

John Paul
harshvardhan ojha
Ranch Hand

Joined: Jul 26, 2007
Posts: 157
    
    1

how can the Thread class be available for instantiation by only implementing the Runnable interface


Thread class came in pic when you did



and provided you Runnable target. Hope that makes sense.
John Paul Hoey
Greenhorn

Joined: Apr 15, 2012
Posts: 25
Hi Steve / harshvardhan ojha,

Thanks very much for you feedback and yes, after taking some time out to understand this i think i have grasped the connection!

@Steve, your mention of the public Thread(Runnable target) constructor was the missing piece to my understanding, because basically, the reason why i can create Thread objects just by implementing the Runnable interface on my WhereAmI class is all down to that constructor; So by using that constructor, it needs something that IS-A Runnable, which in this case, is the place1 object!

Finally!

Thanks Guys, ive been trying to figure this out since Friday (should have came here first but i do like to try and figure things out myself )

Ill assume this same approach can be applied to other implementations of interfaces?

Thanks again

John Paul
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2341
    
  28

IMO, Java muddied the waters by having a Thread constructor that doesn't take Runnable parameter. Internally, the Thread calls it's own run method, which delegates the call to the RUnnable. If you don't provide a Runnable, it does nothing. You can extend Thread and override the run method, and then your Thread's run method will be called. However, this last method is not reccomended, but many newbies end up doing this.

IMO, a Thread should always have a Runnable, hence, all of it's constructors should take Runnable. Not doing so makes it hell of a lot confusing.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Runnable Interface question