user1730789
user1730789

Reputation: 5177

Android purpose of Handler Thread

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,

Upvotes: 0

Views: 566

Answers (1)

Trevor
Trevor

Reputation: 10993

I think I understand your confusion: You're thinking that a Runnable object is in itself an instance of a separate Thread. It is not. In simple terms it is just an object that, by implementing the Runnable interface, a Thread you pass it to knows it can execute the code inside it by calling .run() on it.

When you send a Runnable object to the Handler, that Runnable is executed in the Handler's Thread.

In simple terms it's probably sufficient to explain it this way: If you want to supply a Handler with some work to do on its own Thread, then you have to supply that piece of code inside an object. Now, the object you supply to the Handler has to implement some kind of interface so that the Handler knows what methods to call in that object to make it do the stuff it needs to do. In basic terms that's what the Runnable interface does: it is mandatory for an object that implements Runnable to implement the run() method.

Upvotes: 2

Related Questions