I have been reading through many questions regarding this topic, but I couldn't find any direct answer that could answer my very simple question: Do handlers actually create new threads or just create a handler to the same thread where they were created from? In Android documentation, they mention:
"Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue."
And then they also mention on the same page:
"There are two main uses for a Handler: (1) to schedule messages and runnables to be executed as some point in the future; and (2) to enqueue an action to be performed on a different thread than your own.".
I am not sure whether they mean Handler actually creates new threads that can communicate with the thread from where they were created (for example, from UI thread) OR they allow different threads to communicate with the UI thread if the Handler was created from the UI thread?
For example, in my MainActivity and in onCreate() function I do this:
public class MainActivity extends ActionBarActivity {
private static final String TAG = "MyApp: " + MainActivity.class.getSimpleName();
private Handler mHandler;
private Runnable mRunnable = new Runnable() {
public void run() {
// do something
processData();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "onCreate");
mHandler = new Handler();
mHandler.post(mRunnable);
.....
Am I actually creating a new thread here, independent to the UI thread, that will execute function (processData)? If I am not creating a new thread, what would be the difference then between my code above and just calling function (processData) without needing to create a handler, as follow:
public class MainActivity extends ActionBarActivity {
private static final String TAG = "MyApp: " + MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "onCreate");
processData();
.....
Many thanks.
Anmar
the answer to the question
"Am I actually creating a new thread here, independent to the UI thread, that will execute function (processData)? If I am not creating a new thread, what would be the difference then between my code above and just calling function (processData) without needing to create a handler?"
is no, posting the runnable doesn't mean creating a thread and running the runnable in the background.
the difference between posting a runnable and calling a function: theoretically, posting a runnable adds it to the message queue to be processed when the message queue is ready to do so, while calling the function executes immediately, but overall they are both similar.
the message queue as its name suggests processes the runnables posted by a handler in a FIFO (first in, first out) fashion.
handlers can be used along with Threads to post updates from the background threads to the UI thread.
Here's an old post I wrote about using handlers: http://android-pro.blogspot.com.eg/2011/05/threading-in-android-part-1-handlers.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With