Work in HR or Recruiting?
Vlingo
www.vlingo.com Cambridge, MA Unknown
Work in HR? Complete Your Profile

3 interview experiences Back to all Vlingo Interview Questions & Reviews

Interview Question for Senior Android Developer at Vlingo:
May 10, 2012

What are the various ways an Android developer can apply multi-threading to their app? Be specific, and give examples.


Tags: java, threads, mobile, multi-threading, thread, android
Add Tags [?]

See more for this Vlingo Senior Android Developer Interview

Helpful Question?  
Yes | No
Inappropriate?

Answers & Comments (1)

5 of 5 people found this helpful

May 10, 2012

by Interview Candidate:

since Android uses the Java language it provides all the normal threading mechanisms as all Java-based implementations.

You create a Thread by either (a) sub-classing the Thread class with your own and over-loading the run() method to perform your background task, or (b) you create an instance of the Thread class by giving it an instance of the Runnable interface, and overload the run() method of that interface to perform your background task. Option (b) is preferred by far in most circumstances.

Android, however, provides additional threading mechanisms over and above those provided by Java, and they tend to resolve some of the most interesting dilemmas in Java programming.

In Android you have the following additional mechanisms:

1) AsyncTask
2) Handler
3) runOnUiThread()
4) IntentService (for Service implementations)

So, depending upon what your needs are, you can greatly simplify your code by opting for the use of one of these mechanisms.

For instance, the AsyncTask class provides a very nice encapsulation of creating a Java Thread, providing thread-correct calls for pre/post processing, and automatically uses a thread pool behind the scenes to efficiently use thread resources. All this and provides a very simple 4 method sub-classing interface where you can write just your background code within a single method, and the AsyncTask class will handle cross-thread messaging for you. You have to know, however, that AsyncTask uses a Handler instance internally, so if you're launching this from a Thread that doesn't use a MessageQueue, then this may not work properly for you.

runOnUiThread() does just what it says, it will take a Runnable instance, and run it in the thread space of the main, or "UI", thread in Android. This is a handy feature when you just want a simple means of interacting with the user interface.

Although specific to Service implementations, which should always be threaded, the IntentService provides automatic single-threading for simple implementations, saving tons of code the Service developer.

Moral of the story? Read up on what's available in Android, and find the most elegant solution, rather than simply using the tired old Thread implementations we're so accustomed to.
Helpful Answer?  
Yes | No
Inappropriate?

To comment on this question, Sign In with Facebook or Sign Up

Tags are like keywords that help categorize interview questions that have something in common.