What are asynchronous processing and synchronous processing?

Asked

Viewed 3,385 times

7

I’m having this doubt while I’m studying about the class Handler.

In the book it says that when a Thread is being used to perform some asynchronous processing and need to update the graphical interface of the screen, is required to use a Handler.

3 answers

12


Asynchronous processing refers to processes that do not depend on the outcome of others, and can therefore occur simultaneously/separately. They run into Threads different.

In contrast, synchronous processing runs one after the other, the next starts only when the previous one ends. They run in the same Thread.

The Handler allows to place Messages and Runnables in Messagequeue of Thread where it was created, allowing code from a Thread be executed in another.

Android, as well as most systems, requires code that uses "UI objects" to run on Uithread(Mainthread).
When the code that runs on Thread different needs to update the UI, uses a Handler associated with Uithred to put it on your Messagequeue to be executed.

Note: The subject was approached here in a simplistic way, in an attempt to facilitate their understanding, if you want to deepen follow the links.

6

Asynchronous processing in the context of your question is a processing that takes a certain time to execute and (typically) at the end of this processing the graphical interface needs to be updated.

You could simply encapsulate this processing and updating the screen in a method, call this method (from the thread main, otherwise the screen update does not work) and wait for it to run. That would be synchronous processing. But because the processing is time consuming the method will take time to execute and the screen will lose responsiveness (will not respond to touch commands or update the graphical interface) because the thread will be busy running it. Ideally the thread should be restricted to update the screen and treat input (in the case of said touch commands).

For this reason you do the processing asynchronously: ask a thread separate (either by creating one directly, or indirectly through a AsyncTask or other means) to perform this processing and continue its normal way by executing other commands.

When processing is completed thread secondary will place in the message queue of thread the main command that updates the graphical interface (whether via Handler, or runOnUiThread(), or a method callback of AsyncTask like the onPostExecute(), for example).

2

It is not necessary to implement an Handler, but it is necessary to create a separate asynchronous processing thread to update the graphical interface. You can do this in a very simplified way using runOnUiThread. For example, if you have a progressiDialog that needs to be displayed and hidden at the beginning and end of a processing, you would do something like:

//Mostrar dialog de progresso:
runOnUiThread(new Runnable() {
        @Override
        public void run() {
            progressDialog.show();
        }
});
//Esconder dialog de progesso:
runOnUiThread(new Runnable() {
        @Override
        public void run() {
            progressDialog.dismiss();
        }
});

runOnUiThread must be called by an Activity.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.