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.
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.
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
posted 6 years ago
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
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
posted 6 years ago
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!
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?
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.
You may have just won ten million dollars! Or, maybe a tiny ad.
how do I do my own kindle-like thing - without amazon