Is the android.os.Handler class eliminating the need to declare certain methods as synchronized?

When compiling a simple Clock application, I found that Android requires that you use the android.os.Handler, which lives in Thread A, to update the View objects in Thread A with the results of the data that comes from Thread B.

I'm still relatively new to Java, and this is the first time I've explicitly discussed Threads before, but I know that you usually declare methods and / or operations as synchronized if two different threads want to access the same data. Android.os.Handler seems to be an Android-specific way to synchronize data between threads, so you avoid the classic concurrency errors described in detail in the Oracle documentation I just contacted. It's true?

If I were in such a situation that I had to use the Android OS to use android.os.Handler to transfer data from one stream to another, does this mean that I do not need to declare the methods used to output this data as synchronized?

+4
source share
3 answers

My understanding:

A Handler is just a mechanism for delivering information between two streams. This is not the only mechanism, but it was the mechanism Google decided to use to add easy-to-use methods to actions to perform common tasks.

From document

When a process is created for your application, its main thread is designed to start a message queue that takes care of managing top-level application objects (events, broadcast receivers, etc.) and any windows that they create. You can create your own threads and communicate with the main application stream through the handler. This is done by calling the same record or sendMessage as before, but from your new topic. The given Runnable or Message will then be scheduled in the queue handler message and processed when necessary.

The main thread runs the handler. The handler is responsible for performing your actions and managing the application environment. A handler is simply a message loop that sends messages from a MessageQueue. This is why your thread must run Looper to create a handler. To synchronize the other things you want to do with these efforts, you need to insert your requests into this message queue so that the main application flow does your work. Ui is written so that only one thread accesses the objects; it functions.

I guess the point was to indicate that a handler is one of the mechanisms for synchronization. Of course, the internal elements of MessageQueue are written to account for synchronization, although its gross simplification means "if multiple threads need to synchronize it." Although the user interface is the most common example, using Handler and Looper is just a mechanism for developing a multi-threaded application that synchronizes a single thread for event processing.

Regardless of whether you need to synchronize what you send to the handler, it greatly depends on what you send. If you are talking about synchronizing calls with post , then no, the handler will take care of this. If you are talking about code inside the Runnable that you send to the handler, you realize that the only guarantee for Handler is that your runnable will be executed using the thread that created the handler. You will need to continue synchronizing with other resources.

+11
source

You will need synchronized synchronized if you have a shared resource like ArrayList or something that can be read from two threads at the same time.

The handler itself does not prevent any concurrency, it simply simplifies the execution of actions that should occur in the user interface thread, although the worker thread tries to execute them.

To answer your question: if you use a handler, this usually means that you are doing some important things in the user interface thread. As an example, you have ArrayList , you initialize in onCreate , and then, perhaps, update handler clicks or something like that. Now, if you use a handler to modify it, then ALL access to the ArrayList will occur in the user interface thread, so there is no need for synchronized .

However, as soon as you access this ArrayList from the workflow, you need to synchronize every access to it.

+3
source

Synchronized expectations and notifications are low-level concurrency constructs on top of which high-level concurrency constructors are built, such as semaphores, blocking queues, barriers and Looper / Handler.

Most of what you see in java.util.concurrent (which implements high-level concurrency templates / constructs) runs on top of synchronized, wait, and notification (if you ignore blocking algorithms)

The Looper / Handler mechanism basically facilitates the implementation of the producer-consumer model, where you have one consumer, but several manufacturers. It was created to easily exchange messages between a user interface thread and user interface threads. The user interface threads in android run as a single thread (for example, one consumer), but can receive updates from several non-interface threads (for example, from several manufacturers through handlers).

0
source

Source: https://habr.com/ru/post/1342340/


All Articles