When it comes to keeping user interfaces “lively,” threads are our friends. But Threads are a real pain in the neck. Such is the life of a programmer.

It’s important that we design applications so that the user interface stays alive. Not only does this improve our user’s experience with our apps, but it keeps operating systems from stepping in and punishing us. Performing long operations on the UI thread is a no-no, and both iOS and Android will terminate an app if it violates this. Thus, we want to push such things off to a background thread and provide the user progress information on the UI thread.

Unfortunately, as soon as we have two threads, there are all kinds of “gotcha’s” we have to worry about. Chief among them is synchronization – we have to worry about what happens if both threads are trying to access the same information at the same time. You’ll find lots of references to this out on the Internet if you fire up your favorite search engine.

Fortunately, in an event-driven system such as Android, there are things that we can do to make life easier. For example, any operations that are performed solely on the UI thread can’t run into synchronization issues, because they are automatically serialized by the operating system – only one such operation is going on at any time. Thus is part of the secret behind what makes AsyncTasks so much easier to use than raw threads.