Android timer workflow safe

I have a class that starts new threads, and they use a handler to write an array in the ui thread, they are thread safe.

What if I run these threads from a timer task, are they still thread safe with ui thred?

Thanks!

+4
source share
2 answers

If Handler objects are tied to a UI thread, it is safe to update the array in Handler (assuming the array is also associated with the UI thread).

Check out the Handler documentation , especially this part:

The handler allows you to send and process Message and Runnable objects associated with the MessageQueue stream. Each Handler instance is associated with one thread and this thread's message queue. When you create a new handler, it is attached to the thread / message queue of the thread that creates it - from this point it will deliver messages and executable files to the message queue and execute them as they exit the queue message.

For the handler, two main methods are used: (1) plan messages and executable files that should be executed as some points in the future; and (2) insert an action that must be performed on a thread other than your own.

Indicates that Handler code is executing on the thread to which it belongs.

+2
source

If the threads you start are the correct thread safe, it doesn't matter where you start them from, it's the user interface thread, AsyncTask or TimerTask .

0
source

All Articles