The thread is part of your program functionality that can run in parallel with the main application execution process (or thread). It runs withing a process. The process may contain only, and that’s rare one thread and mostly contains multiple threads. Threads share the same resources and memory. Creating new thread need very less work that creating a new process.
Use a Thread when
- The app is required to be visible when the operation occurs.
- background operation is relatively short running (less than a minute or two)
- the activity/screen/app is highly coupled with the background operation, the user usually 'waits' for this operation to finish before doing anything else in the app. Using a thread in these cases leads to cleaner, more readable & maintainable code. That being said its possible to use a Service( or IntentService).
Use a Service when
- The app could be invisible when the operation occurs (Features like Foreground service could help with operations being interrupted)
- The user is not required to 'wait' for the operation to finish to do other things in the app.
- The app is visible and the operation is independent of the app/screen context.
Comments
Post a Comment