I was just wondering why the Handler has to take an instance of a Runnable ? After all a Handler purpose is place work from one thread into another Thread. What is the purpose of using a Third thread, a Runnable to accomplish this ?
Kind Regards,
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I think I understand your confusion: You’re thinking that a
Runnableobject is in itself an instance of a separateThread. It is not. In simple terms it is just an object that, by implementing theRunnableinterface, aThreadyou pass it to knows it can execute the code inside it by calling.run()on it.When you send a
Runnableobject to theHandler, thatRunnableis executed in theHandler‘sThread.In simple terms it’s probably sufficient to explain it this way: If you want to supply a
Handlerwith some work to do on its ownThread, then you have to supply that piece of code inside an object. Now, the object you supply to theHandlerhas to implement some kind ofinterfaceso that theHandlerknows what methods to call in that object to make it do the stuff it needs to do. In basic terms that’s what theRunnableinterface does: it is mandatory for an object that implementsRunnableto implement therun()method.