What are asynchronous processing and synchronous processing?

7

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

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

asked by anonymous 05.09.2016 / 21:52

3 answers

11

Asynchronous processing refers to processing that does not depend on the output of others, and can therefore occur simultaneously / separately. They run in different Threads .

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

Handler lets you place Messages and Runnables in the MessageQueue of the Thread where it was created, allowing code of a Thread runs in another.

Android, as well as most systems, forces code that uses "UI objects" to run in the UIThread ( MainThread ). When code that runs on a different Thread needs to update the UI a>, uses a Handler associated with UIThred to put it in its MessageQueue to run.

    
05.09.2016 / 22:32
6

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

You could simply encapsulate this processing and refresh the screen in a method, call this method (from the main thread , 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 a long time to execute and the screen will lose responsiveness (it will not respond to touch commands or update the graphical interface) because the main thread will be busy executing it. Ideally, the main thread should be restricted to updating the screen and handling input (in this case the touch commands).

For this reason, you do the processing asynchronously: it requests a separate thread (either directly or indirectly through a AsyncTask or other means) to perform this processing and continues its path normally by running other commands.

When processing terminate the thread , it will put the command that updates the graphical interface (either via Handler , or runOnUiThread() , or a callback method of AsyncTask such as onPostExecute() , for example).

    
05.09.2016 / 22:07
2

It is not necessary to implement a handler, but rather it is necessary to create a thread separate from asynchronous processing to update the graphical interface. You can do this in a very simplified way using runOnUiThread. For example, if you have a progressDialog that needs to be displayed and hidden at the beginning and end of a render, 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();
        }
});

The runOnUiThread should be called by an activity.

    
05.09.2016 / 22:10