ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalEC-333Mobile Threading
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    EC-333

    Mobile Application Development

    Progress0 / 33 done
    Topics33
    1Mobiles Application Development Platform2HTML5 for Mobiles
    3Architecture, Framework and Application Development
    4Architecture, Framework5Application Development with Windows Mobile6Eclipse7Fragments8Calling Built-in Applications using Intents9Displaying Notifications10Components of a Screen11Adapting to Display Orientation12Managing Changes to Screen Orientation13Utilizing the Action Bar14Creating the User Interface15Listening for UI Notifications16Views17User Preferences18Persisting & Sharing Data19Sending SMS Messages20Getting Feedback21Sending E-mail22Displaying Maps23Consuming Web Services Using HTTP
    24Accessing and Creating25Threading26Publishing Android Applications27Deployment on App Stores28Mobile Programming Languages29Challenges with Mobility and Wireless Communication30Location-aware Applications31Performance/Power Tradeoffs32Mobile Platform Constraints33Emerging Technologies
    8 min read
    Topic 25 of 33
    Topic Study Note Illustration
    25

    Threading

    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...

    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 view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    8mremaining
    1.3kwords
    Intermediatelevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Mobile Application Development