ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Mobile Application Development
    EC-333
    Progress0 / 33 topics
    Topics
    1. Mobiles Application Development Platform2. HTML5 for Mobiles3. Android OS: Architecture, Framework and Application Development4. iOS: Architecture, Framework5. Application Development with Windows Mobile6. Eclipse7. Fragments8. Calling Built-in Applications using Intents9. Displaying Notifications10. Components of a Screen11. Adapting to Display Orientation12. Managing Changes to Screen Orientation13. Utilizing the Action Bar14. Creating the User Interface15. Listening for UI Notifications16. Views17. User Preferences18. Persisting & Sharing Data19. Sending SMS Messages20. Getting Feedback21. Sending E-mail22. Displaying Maps23. Consuming Web Services Using HTTP24. Web Services: Accessing and Creating25. Threading26. Publishing Android Applications27. Deployment on App Stores28. Mobile Programming Languages29. Challenges with Mobility and Wireless Communication30. Location-aware Applications31. Performance/Power Tradeoffs32. Mobile Platform Constraints33. Emerging Technologies
    EC-333›Threading
    Mobile Application DevelopmentTopic 25 of 33

    Threading

    8 minread
    1,325words
    Intermediatelevel

    Threading in Mobile Application Development

    Threading refers to the ability of an application to perform multiple tasks concurrently. This is essential for creating smooth and responsive applications, as it allows for multitasking without freezing or blocking the main user interface (UI). In mobile application development, handling threading properly ensures that tasks like network requests, database operations, or background computations don't interfere with the user experience.

    In this guide, we’ll discuss what threading is, how it works, and how to implement threading in Android development.


    1. What is Threading?

    In programming, a thread is a sequence of instructions that can be executed independently of other threads. Modern operating systems and mobile platforms like Android support multithreading, which allows applications to perform several tasks simultaneously.

    Types of Threads in Android:

    1. Main Thread (UI Thread): This is the thread that controls the user interface. All UI updates must happen on this thread to avoid exceptions. If you perform long-running tasks on this thread, the UI can freeze and become unresponsive.

    2. Worker Threads: These are background threads used to handle long-running tasks such as network calls, file I/O, or intensive calculations. These threads run independently of the UI thread, so they don’t block the user interface.


    2. Threading in Android: Handling the UI Thread and Worker Threads

    A. The Main (UI) Thread

    The main thread, also known as the UI thread, is responsible for interacting with the user interface. In Android, all UI updates (such as displaying a button click or showing a progress bar) must be done on the main thread.

    Key points:

    • UI updates: All actions that modify the UI must be done on the main thread.
    • UI Blocking: Any long-running task on the main thread will block the UI, causing the app to freeze. For example, making a network request or reading a large file on the main thread will make the app unresponsive.

    B. Worker Threads

    To prevent the UI from freezing, you can offload long-running operations to worker threads. Worker threads are used for tasks that take time and should not block the UI, such as:

    • Network operations
    • Database queries
    • File operations
    • Image processing
    Creating a Worker Thread in Android

    In Android, you can create a worker thread using the Thread class or AsyncTask (deprecated in API level 30) for simpler tasks. More advanced tasks should use ExecutorService or HandlerThread.


    3. Working with Threads in Android

    A. Using Thread Class

    You can create a new thread in Android by extending the Thread class or implementing the Runnable interface. Here’s a basic example of how to use the Thread class:

    public class MyActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // Create a new thread
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    // Background task (e.g., network operation)
                    performBackgroundTask();
                }
            });
            thread.start(); // Start the thread
        }
    
        private void performBackgroundTask() {
            // Simulate a long-running task
            try {
                Thread.sleep(5000);  // Simulating a task that takes time
                // Update UI after task completion (must run on UI thread)
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        // UI update code (e.g., change text, hide progress bar)
                        Toast.makeText(MyActivity.this, "Task Completed", Toast.LENGTH_SHORT).show();
                    }
                });
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    

    Explanation:

    • Creating the thread: new Thread(new Runnable()) creates a new thread that runs a background task.
    • UI thread updates: runOnUiThread() is used to update the UI from the background thread.

    B. Using AsyncTask (Deprecated)

    The AsyncTask class was designed for performing background operations and updating the UI thread, but it has been deprecated in Android API level 30. For smaller tasks, consider using ExecutorService or Kotlin coroutines (recommended).

    Here’s how AsyncTask was used:

    private class MyAsyncTask extends AsyncTask<Void, Void, String> {
    
        @Override
        protected String doInBackground(Void... params) {
            // Perform long-running task in background (e.g., network call)
            try {
                Thread.sleep(5000); // Simulating background task
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "Task Completed";
        }
    
        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            // Update UI with the result
            Toast.makeText(MyActivity.this, result, Toast.LENGTH_SHORT).show();
        }
    }
    
    // Call the AsyncTask in your activity
    new MyAsyncTask().execute();
    

    Explanation:

    • doInBackground() runs in the background thread and performs the heavy task.
    • onPostExecute() runs on the UI thread after the task is complete, allowing UI updates.

    Note: AsyncTask is now deprecated in Android, and you are encouraged to use alternatives like Kotlin coroutines, ExecutorService, or Handler.


    C. Using ExecutorService

    ExecutorService is a more flexible and modern way to handle background tasks in Android. It provides a pool of worker threads to execute tasks concurrently.

    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    public class MyActivity extends AppCompatActivity {
    
        private ExecutorService executorService;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            executorService = Executors.newFixedThreadPool(2); // Creates a pool with 2 threads
    
            // Submit task to execute in the background
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    performBackgroundTask();
                }
            });
        }
    
        private void performBackgroundTask() {
            // Simulating a long-running task
            try {
                Thread.sleep(5000);
                // Update UI after task completion (must run on UI thread)
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(MyActivity.this, "Task Completed", Toast.LENGTH_SHORT).show();
                    }
                });
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    

    Explanation:

    • ExecutorService manages a pool of threads, allowing you to submit multiple tasks efficiently.
    • The submit() method adds tasks to the thread pool for execution.

    D. Using HandlerThread

    A HandlerThread is useful for background tasks that require message handling between threads. It’s an easier approach for tasks that require interaction with the Handler (a utility that allows communication between threads).

    import android.os.Handler;
    import android.os.HandlerThread;
    
    public class MyActivity extends AppCompatActivity {
    
        private HandlerThread handlerThread;
        private Handler handler;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            handlerThread = new HandlerThread("BackgroundThread");
            handlerThread.start();  // Start the background thread
            handler = new Handler(handlerThread.getLooper());
    
            // Post a task to run on the background thread
            handler.post(new Runnable() {
                @Override
                public void run() {
                    performBackgroundTask();
                }
            });
        }
    
        private void performBackgroundTask() {
            // Simulate background work
            try {
                Thread.sleep(5000);
                // Update UI after task completion (must run on UI thread)
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(MyActivity.this, "Task Completed", Toast.LENGTH_SHORT).show();
                    }
                });
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            handlerThread.quit(); // Clean up when activity is destroyed
        }
    }
    

    Explanation:

    • HandlerThread allows you to manage background tasks and ensures you can communicate with the thread’s Handler using its Looper.

    4. Best Practices for Threading in Android

    • Never update UI on background threads: Always use runOnUiThread() or Handler to update the UI from a background thread.
    • Handle exceptions: Make sure to handle exceptions, especially when working with network requests or other tasks that may fail.
    • Use proper thread management: Use ExecutorService for more complex background tasks, as it can manage multiple threads efficiently.
    • Avoid ANR (Application Not Responding): If your app is performing long-running operations, ensure they are done on a background thread to avoid UI freezing and ANR errors.
    • Clean up threads: Always ensure threads are properly terminated or cleaned up when no longer needed (e.g., using handlerThread.quit()).

    Conclusion

    Threading is a crucial concept in Android development to ensure that long-running tasks (like network calls or database operations) do not block the main UI thread and make the application unresponsive. By using tools like Thread, ExecutorService, or HandlerThread, you can efficiently handle background operations in your Android apps. Just remember that UI updates should always happen on the main thread, and background tasks should run in separate threads to keep the app smooth and responsive.

    Previous topic 24
    Web Services: Accessing and Creating
    Next topic 26
    Publishing Android Applications

    Past Papers

    Open this section to load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time8 min
      Word count1,325
      Code examples0
      DifficultyIntermediate