Processes and Threads 进程和线程,android移动开发基础案例代码

(because another component from the application exists),

then the component is started within that process and uses the same thread of execution.

如果应用组件开始时,并且该应用存在一个进程(因为该应用的其他组件在运行),那么该组件在此进程中启动,并使用同一个执行线程。

However, you can arrange for different components in your application to run in separate processes,

and you can create additional threads for any process.

但是,你可以把你的应用中的不同组件按排运行在不同的进程中,并且可以为任意进程创建附加的线程。

This document discusses how processes and threads work in an Android application

本文,讨论Android应用中进程和线程是如何工作的。

Processes进程

By default, all components of the same application run in the same process and most applications should not change this.

一般,同一个应用的所有组件运行在同一个进程中,并且大多数应用不需要修改这一点

However, if you find that you need to control which process a certain component belongs to, you can do so in the manifest file.

然而,如果你发现,需要控制某个组件属于某个指定的进程,你可以在manifest文件中控制它。

The manifest entry for each type of component element—, , ,

and —supports an android:process attribute that can specify a process in which that component should run.

每种组件元素的 manifest入点,, , ,和—支持一个属性android:process,该属性指定组件应该运行在那个进程.

You can set this attribute so that each component runs in its own process or so that some components share a process while others do not.

你可以设置这个属性,来控制每个组件运行在它自己的进程中,或者,某些组件共享同一个进程而其他的则不共享此进程。

You can also set android:process so that components of different applications run in the same process—provided that the applications share the same Linux user ID and are signed with the same certificates.

你还可以设置android:process属性,让不同应用的组件运行在同一个进程中—提供应用共享相同的linux用户ID,及相同的认证签名。

The element also supports an android:process attribute, to set a default value that applies to all components.

元素,也支持android: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.

当其他的更需要立即服务于用户的进程请求的内存不够时,Android可以会在某个点决定关闭一个进程。

Application components running in the process that’s killed are consequently destroyed. A process is started again for those components when there’s again work for them to do.

在此进程中运行的应用组件也因此被杀掉,当组件再次需要工作时,将会为这些组件开启一个进程。

When deciding which processes to kill, the Android system weighs their relative importance to the user.

什么时候,决定杀掉那一个进程,Android系统会衡量它们对用户的重要性。

For example, it more readily shuts down a process hosting activities that are no longer visible on screen, compared to a process hosting visible activities.

比如,捅用不在屏上显示的活动的进程,比捅有在屏幕显示活动的进程更容易被杀掉。

The decision whether to terminate a process, therefore, depends on the state of the components running in that process. The rules used to decide which processes to terminate is discussed below.

因此,决定是否终结一个进程,取决于运行在该进程中的组件的状态。决定终结那个进程的规则在下面讨论。

Process lifecycle进程生命周期

The Android system tries to maintain an application process for as long as possible, but eventually needs to remove old processes to reclaim memory for new or more important processes.

Android系统试着尽可能久的去维护一个应用进程,但最终需要移除旧的进程来回收内存,以运行新的或更重要的进程。

To determine which processes to keep and which to kill, the system places each process into an “importance hierarchy” based on the components running in the process and the state of those components.

为了检测那个进程需要保持及那个需要杀掉,系统在每个进程中,根据运行在进程中的组件和组件的状态,加入一个"重要性等级".

Processes with the lowest importance are eliminated first, then those with the next lowest importance, and so on, as necessary to recover system resources.

当必须要回收系统资源时,重要性最低的被首先消除,然后是下一个最低的,依此类推。

There are five levels in the importance hierarchy. The following list presents the different types of processes in order of importance (the first process is most

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值