Processes and Threads--进程与线程

When the first of an application's components needs to be run, Android starts a Linux process for it with a single thread of execution. By default, all components of the application run in that process and thread.

翻译:一旦应用中的某个组件需要执行,那么Android系统将启动一个Linux进程,在该进程中为该组件的运行分配一个线程。默认场合下,一个应用的所有组件都在该进程下的一个主线程中运行。

However, you can arrange for components to run in other processes, and you can spawn additional threads for any process.

翻译:然而作为开发者,是可以修订系统默认的进程、线程启动与管理策略的,达到多线程方式管理应用的目的。

Processes--进程

The process where a component runs is controlled by the manifest file. The component elements — <activity>, <service>, <receiver>, and <provider> — each have a process attribute that can specify a process where that component should run. These attributes can be set so that each component runs in its own process, or so that some components share a process while others do not. They can also be set so that components of different applications run in the same process — provided that the applications share the same Linux user ID and are signed by the same authorities. The <application> element also has a process attribute, for setting a default value that applies to all components.

翻译:每个组件运行的进程是由manifest文件控制的,Activity、service、provider、receiver组件元素都有一个process属性可以用来设定该组件运行的进程。如果设定了不同组件的不同进程,那么各个组件分别在自身的进程中运行。也可以为一些组件设定同样的进程属性值,这样一来,这些组件将运行在一个进程中。而且我们还可以通过这个属性的设置使得不同应用中的组件运行在同一个进程中,一旦不同应用中的组件运行在同一个进程中,那么不同应用将共享同一个Linux user ID,而且不同应用享有同样的执行权限。 <application> 元素本身也有一个process属性,如果我们设定了该元素的process属性,那么就意味着当前应用涉及的所有组件元素将使用该属性值作为自身的process属性值。

All components are instantiated in the main thread of the specified process, and system calls to the component are dispatched from that thread. Separate threads are not created for each instance. Consequently, methods that respond to those calls — methods like View.onKeyDown() that report user actions and the lifecycle notifications discussed later in the Component Lifecycles section — always run in the main thread of the process. This means that no component should perform long or blocking operations (such as networking operations or computation loops) when called by the system, since this will block any other components also in the process. You can spawn separate threads for long operations, as discussed under Threads, next.

翻译:所有的组件都是在指定(manifest文件所指定的进程或默认的)的主线程中被实例化的,Android系统对某个组件方法的调用是基于这个主线程派发的。这时候,Android系统是不会再为这些组件实例们分别创建各自独立的线程的。因此,所有基于某个组件的方法如View.onKeyDown()或是后面将讲述到的组件生命周期方法的调用与执行都是在这个主线程中运行的。这就意味着组件方法被系统调用执行过程中是不能出现长时间等待或是阻塞现象(网络操作或循环等待),一旦出现这些现象,将导致主线程中的所有其他组件方法执行出现阻塞现象。实际开发过程中,总有需要执行长等待或可能导致阻塞现象操作的可能,针对这种需要,开发者可以使用独立线程的方式达到自己的目的,为一些特殊组件提供独立线程支持,以避免阻塞主线程的现象的发生。后面我们将会在 Threads章节中讲述。

Android may 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. Application components running in the process are consequently destroyed. A process is restarted for those components when there's again work for them to do.

翻译:Android系统可以在某个时间点上关闭进程,比如内存不足或其他更紧急的进程需要更多内存的时候。进程被关闭的同时,运行在被关闭进程上的组件也将被销毁。

When deciding which processes to terminate, Android weighs their relative importance to the user. For example, it more readily shuts down a process with activities that are no longer visible on screen than a process with visible activities. The decision whether to terminate a process, therefore, depends on the state of the components running in that process. Those states are the subject of a later section, Component Lifecycles.

在决定哪个进程被终止的时候,Android系统需要衡量进程对用户的重要性,比如,系统更倾向终止当前屏幕上没有可见的Activity的进程。所以是否关闭一个进程和这个进程中运行的Activity状态有关系的,这里说的状态和后面我们将要讲到的Component Lifecycles有密切关系。

Threads--线程

Even though you may confine your application to a single process, there will likely be times when you will need to spawn a thread to do some background work. Since the user interface must always be quick to respond to user actions, the thread that hosts an activity should not also host time-consuming operations like network downloads. Anything that may not be completed quickly should be assigned to a different thread.

翻译:尽管你可以把你的应用约束在一个进程中运行(默认情况下该进程中只有一个主线程),实际上仍然有很多时候是需要多个线程以支持一些后台操作,由于用户界面总是需要能够快速响应客户交互动作的,所以,一个基本原则是:作为某个Activity的宿主线程不应该包含耗时操作,比如下载这样的操作,这样的操作应该在另外一个线程中执行。否则主线程将无法对用户基于界面的操作做出快速响应。

Threads are created in code using standard Java Thread objects. Android provides a number of convenience classes for managing threads — Looper for running a message loop within a thread, Handler for processing messages, and HandlerThread for setting up a thread with a message loop.

翻译:线程的创建可以使用标准的Java类Thread,android提供几个类可以很便捷管理线程, Looper 可以在一个线程中作消息循环, Handler 可以用来处理消息, HandlerThread 可以创建一个可以做消息循环的线程。

更多相关文章

  1. 一款霸榜 GitHub 的开源 Linux 资源监视器!
  2. Android应用程序线程消息循环模型分析(5)
  3. 搭建windows下的android开发环境
  4. Android(安卓)面试复习资料
  5. Android知识点总结(二十)Android中的ANR
  6. android之handler和asynctask
  7. EventBus3.0源码解析(二):post()与postSticky()
  8. Android(安卓)camera系统开发之IPC (二)
  9. Android动画之一:Drawable Animation

随机推荐

  1. Android获取手机的型号和系统版本
  2. Android.StructureOfAndroidSourceCodeRo
  3. Android(安卓)创建与解析XML(三)—— Sax方
  4. Timer和TimerTask的知识点
  5. Android获取sdcard信息
  6. Android常用颜色表
  7. Android软键盘的弹出与隐藏
  8. android 监听网络连接状态的改变
  9. build.gradle(module)
  10. Android实现简易计算器(页面跳转和数据传