Skip to content

Commit cdf12c1

Browse files
authored
Update README.md
1 parent e1977b4 commit cdf12c1

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

README.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -812,14 +812,23 @@ try {
812812
Tasks on a background thread using ```AsyncTask``` (for short or interruptable tasks) or ```AsyncTaskLoader``` (for tasks that are high-priority, or tasks that need to report back to the user or UI).
813813

814814
## AsyncTask
815+
- Help get work on/off the UI thread.
816+
- Basically, all AsyncTasks are created in a same thread. That means them will execute in a serial fashion from a single message queue.
815817
- run on UI thread: ```onPreExecute```, ```onProgressUpdate``` and ```onPostExecute```
816818
- update progress to UI via ```publishProgress```, handle data in ```onProgressUpdate```
817819
- ```WeakReference```
818-
- ```executeOnExecutor```
820+
- There is a way to force AsyncTask works in thread-pooled way: ```executeOnExecutor```
819821

820822
Code: [BackgroundActivity], [SleepTask]
821823

822-
## AsyncTaskLoader
824+
> Don't hold references to any type of UI specific objects in any
825+
> threading scenarios.
826+
> Don't declare your task as an inner class of an activity.
827+
828+
Looper class keeps the thread alive, holds a message queue and pop works off a queue and execute on.
829+
Handler class helps put work at the head, the tail or even set a time-based delay.
830+
831+
### AsyncTaskLoader
823832
When you want the data to be available even if the device configuration changes, use ```loaders```
824833
This ```getLoaderManager()``` or ```getSupportLoaderManager()``` is deprecated since Android P. Instead, we use ```ViewModels``` and ```LiveData```
825834

@@ -830,6 +839,28 @@ This ```getLoaderManager()``` or ```getSupportLoaderManager()``` is deprecated s
830839

831840
Code: [BackgroundActivity], [SleepTaskLoader]
832841

842+
## HandlerThread
843+
- Dedicate thread for API callbacks.
844+
- HandlerThread is a nifty solution for the work that not deal with UI updates.
845+
- Don't forget to assign the priority because CPU can only execute a few parallel threads.
846+
847+
## ThreadPool
848+
- Run a lot of parallel small works.
849+
- There are a couple of ThreadPools: FixedThreadPool, CachedThreadPool and ScheduledThreadPool.
850+
851+
1. FixedThreadPool: A thread pool with fixed number of threads.
852+
Instead of building a fixed thread pool with random number of threads, you should check how many processors your device have. E.g. ```val coreCount = Runtime.getRuntime().availableProcessors() // in my case, it's six.```.
853+
854+
2. CachedThreadPool: A thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available.
855+
856+
3. ScheduledThreadPool: A thread pool that can schedule commands to run after a ginven delay, or to execute periodically.
857+
858+
[Read more](https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executors.html)
859+
860+
## IntentService
861+
- It's ideal for background tasks. It also helps get intents off UI thread.
862+
- It's the easiest way to update UI by running on AsyncTask, and HandlerThread is also a excellent solution for the work that not deal with UI updates.
863+
833864
## ViewModels and LiveData
834865

835866
# Notification

0 commit comments

Comments
 (0)