A thread is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.
- How many types of thread?
- How to specify the code to run on a thread
- How to manage multi-thread?
- How threads communicate?
- What matter if multithread access an instance?
1. How many types of thread?
There’re 3 types of thread: Main thread, UI thread and Worker thread.
Main thread: when an application is launched, the system creates a thread of execution for the application, called main. This thread is very important because it is in charge of dispatching events to the appropriate user interface widgets, including drawing events. The main thread is also sometimes called the UI Thread. However, under special circumstances, an app’s main thread might not be its UI Thread.
UI Thread: This is the main thread of execution for your application. This is where most of your application code is run. All of your application components (Activities, Services, ContentProviders, BroadcastReceivers) are created in this thread, and any system calls to those components are performed in this thread.
Additionally, the Android UI toolkit is not thread-safe. So, you must not manipulate your UI from a worker thread — you must do all manipulation to your user interface from the UI thread. Thus, there are simply two rules to Android’s single thread model:
— Do not block the UI thread
— Do not access the Android UI toolkit from outside the UI thread
Worker Thread: Because of the single-threaded model described above, it’s vital to the responsiveness of your application’s UI that you do not block the UI thread. If you have operations to perform that are not instantaneous, you should make sure to do them in separate threads (“background” or “worker” threads).
However, as the complexity of the operation grows, this kind of code can get complicated and difficult to maintain. To handle more complex interactions with a worker thread, you might consider using a Handler in your worker thread, to process messages delivered from the UI thread.
2. Specify the code to run on a thread
Implement the run() method of Runnable.
Remember, though, that the Runnable won’t be running on the UI thread, so it can’t directly modify UI objects such as View objects.
At the beginning of the run() method, set the thread to use background priority by calling Process.setThreadPriority()
with THREAD_PRIORITY_BACKGROUND
. This approach reduces resource competition between the Runnable
object’s thread and the UI thread.
class PhotoDecodeRunnable implements Runnable {
...
/*
* Defines the code to run for this task.
*/
@Override
public void run() {
// Moves the current Thread into the background
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
...
}
...
}
3. How to manage multi-thread?
If you only want to run the task once, the above section may be all you need.
If you want to run a task repeatedly on different sets of data, but you only need one execution running at a time, an IntentService
suits your needs.
To automatically run tasks as resources become available, or to allow multiple tasks to run at the same time (or both), you need to provide a managed collection of threads. To do this, use an instance of ThreadPoolExecutor
, which runs a task from a queue when a thread in its pool becomes free. To run a task, all you have to do is add it to the queue.
Next step
4. How threads communicate?
5. What matter if multithread access an instance?
https://medium.com/culi-tech-viet/thread-handling-android-p2-bd34da043afc
Reference
https://developer.android.com/guide/components/processes-and-threads
https://developer.android.com/training/multiple-threads/create-threadpool
https://medium.com/@ojiofong/introduction-to-android-thread-pool-and-threadpoolexecutor-32b73a2f5d60
No responses yet