You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+33-2Lines changed: 33 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -812,14 +812,23 @@ try {
812
812
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).
813
813
814
814
## 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.
815
817
- run on UI thread: ```onPreExecute```, ```onProgressUpdate``` and ```onPostExecute```
816
818
- update progress to UI via ```publishProgress```, handle data in ```onProgressUpdate```
817
819
-```WeakReference```
818
-
-```executeOnExecutor```
820
+
-There is a way to force AsyncTask works in thread-pooled way: ```executeOnExecutor```
819
821
820
822
Code: [BackgroundActivity], [SleepTask]
821
823
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
823
832
When you want the data to be available even if the device configuration changes, use ```loaders```
824
833
This ```getLoaderManager()``` or ```getSupportLoaderManager()``` is deprecated since Android P. Instead, we use ```ViewModels``` and ```LiveData```
825
834
@@ -830,6 +839,28 @@ This ```getLoaderManager()``` or ```getSupportLoaderManager()``` is deprecated s
830
839
831
840
Code: [BackgroundActivity], [SleepTaskLoader]
832
841
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.
- 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.
0 commit comments