Process and Threads
Process and Threads
当一个Application启动时,如果该application此时没有其他Component在运行,Android System此时会为该application启动一个新的进程。 默认情况下,一个application中所有组件都运行在同一个进程和线程(主线程)中。 如果一个application启动时,该application已经有一个进程(已经有组件启动了),那么新启动的组件将会在已有进程中执行。你可以手动安排应用中不用组件运行在不同的进程中,也可以为任意进程增加线程。
Processes
默认情况下,一个application钟的所有Component都运行在同一个进程中,并且大多数application并没有必要去修改这点。但是,你也可以在manifest文件中定义哪些组件运行在哪个进程中。
android:process
manifest文件中,每种Component(activity,service,receiver,provider)中都支持 android:process 属性,该属性可以明确指出这个Component应该运行在哪个process中。利用这个属性,可以设置某个Component自己单独运行在一个进程中,也可以设置某些Component共享一个进程。还可以利用该属性设置不同Application的Components运行在同一个进程中(只要这两个应用使用相同的Linus user ID and are signed with the same certificates)
<application元素也可以通过 android:process 来为应用设置一个默认process。
Android might decide to shut down a process at some point, when memory is low and required by other processes that are more immediately serving the user. 运行在被杀掉进程中的Component都会被destroy。之后如果再次需要使用被回收进程中的组件,将会重新启动一个进程。
Android System根据进程与用户的关联重要程度来确定哪些进程将会被关闭。相对来说,包含用户不可见组件的进程相对更加不重要,更容易被回收。
Process lifecycle
在内存容许的情况下,android系统会尽可能的保持进程不销毁。根据进程中Component的运行状态,android system判断process的重要程度。
There are five levels int the importance hierarchy.
-
Foreground process : 前台进程 :
A process that is required for what the user is currently doing.- 包含用户正在交互的Activity的进程
- 包含与交互Activity绑定的服务的进程
- 包含正在执行lifecycle callback的Service的进程
- 包含正在执行onReceiver()广播组件的进程
-
Visible process: 可见进程 :
A process that doesn't have any foreground components, but still can affect what the user sees on screen.- 包含背景可见的Activity的进程
- 包含为可见Activity服务的service的进程
-
Service process:服务进程:
A process that is running a service that has been started with the startService() method and doesn't fall into either of the two higher categories. -
Background process
A process holding an activity that's not currently visible to the user.
一般会有很多背景进程,保存在一个LRU队列中 -
Empty process:
A process that doesn't hold any active application components.保存空进程是为了提高新进程的启动速度。
Process的重要性排名:
- 一个Process中有多个活动组件,按照最高等级组件进行排列
- A process's ranking might be increased because other processes are dependent on it - a process that is serving another process can never be ranked lower than the process it is serving.
- 因为一个在执行service的进程的重要性要高于process with background activities.所以如果有长时间任务需要执行,比较好的方式是启动一个Service,而不是在Activity中直接新启动一个线程。
Thread
When an application is launched, the system creates a thread of execution for the application, call "main".
一个process中所有Component均运行在一个UI线程中(一个Application可能有多个进程)。System callback(例如 onKeyDown, onCreate , onDestroy 均运行在UI线程中)。Android UI 线程不是线程安全的,所以,所有对UI的操作都应该在UI线程中完成(而不应该从另一个线程中去更改UI)
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 threads( background thread)
Android offers several ways to access the UI thread from other threads:
- Activity.runOnUiThread(Runnable)
- View.post(Runnable)
- View.postDelayed(Runnable, long)
- handler
- AsyncTask
AsyncTask: It performs the blocking operations in a worker thread and then publishes the results on the UI thread, without requiring you to handle threads or handlers yourself.
- doInBackground() : 在一个background thread线程池中执行
- onPreExecute(): UI线程
- onPostExecute() : UI线程:使用doInBackground中的结果来更新UI
- onProgressUpdate() : UI线程
Thread-safe methods
ContentProvider: 中的query(), insert(), delete(), update(), getType()可能被很多应用同时访问,因此它们必须是线程安全的方法,执行在ContentProvider所在进程的一个线程池中。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步