您的位置:首页 > 移动开发 > Android开发

Android task process thread 进程与线程

2014-07-10 08:01 489 查看
要了解Android的应用程式的开发,这是基础,也是一个观念

知道的表面的运作方式才可以深入

了解process&thread(进程与线程)的运作才可以去开发比较深入的程式

毕竟有时候可能会碰到多线程的程式运作

先简单了解task

转贴 : 小鳗的学习笔记

Task是使用者在使用Application时的User Experiences。如果今天我们的APK功能要开启Google map,也许我们程式会做连结直接开启MAP。但这个MAP却不是我们写的。但从我们的程式到展开MAP却感觉是一体的。那是因为Google想要照顾这部份的使用者经验。

Task写在Stack,也就是堆叠里。

Task里放的,就是我们在使用一只应用程式时,所有和这只应用程式相关的Activity的存放空间。

程式会依我们最先开启的Activity,在堆叠里,开出一个Task(任务)空间,然后呢,它被放在Task的最顶层。如果此时又开了一个新的Activity,就会把前一个Activity往下推,而成为你眼前看到的画面。之前那个Activity还是在堆叠里,所以如果此时你又按了[返回]键,目前的新画面,会"弹出"这个堆叠,然后把之前的Activity,利用onResume()的方式,呼叫回来你面前。

Task是一个Activity的堆叠,而非类别或任何manifest档里你看的见的物件。

原文 :

To the user, it will seem as if the map viewer is part of the same application as your activity, even though it's defined in another application
and runs in that application's process. Android maintains this user experience by

task. Simply put, a task is what the user experiences as an "application." It's a group of related activities, arranged in a stack.

keeping both activities in the same

The root activity in the stack is the one that began the task - typically, it's an activity the user selected in the application launcher.

- the one that is the focus for user actions. When one activity starts another, the new activity is pushed on the stack; it becomes the running
activity. The previous activity remains in the stack. When the user presses the BACK key, the current activity is popped from the stack, and the previous one resumes as the running activity.

The activity at the top of the stack is one that's currently running

A task is a stack of activities, not a class or an element in the manifest file.

==========================================================================

再来先简单介绍一下 process & thread

(原创,因为简单自己写好了,XD)

很简单的说法,当应用程式开启的时候,系统会给予一个process(进程),而process会在执行一个thread(线程)

一个process可以包含多个thread,且thread可以共享resource

当然第一次开启应用程式,Android将启动一个只有一个执行线程的进程,而这个线程就是mainthread,因为主要用来处理与使用者的沟通,也因此叫做UIthread

再来简单的整理TASK,PROCESS,THREAD

我们可以把TASK看成一个应用程式本身,当使用者点击应用程式以后,就启动了TASK,而TASK就可以看成一个或多个activity

当开启一个应用程式,task行成,然后产生第一个process,就是activity,activity又执行了第一个thread.

再补充 :

process是进程的意思,它代表程序的一次运行,而一个进程又是由一个以上的线程组成,thread是线程的意思。

线程是最小的调度单位,进程是最小的内存分配单位。

The major difference between threads and processes is

1.Threads share the address space of the process that created it; processes have their own address.

2.Threads have direct access to the data segment of its process; processes have their own copy of the data segment

of the parent process.

3.Threads can directly communicate with other threads of its process; processes must use interprocess communication to communicate with sibling
processes.

4.Threads have almost no overhead; processes have considerable overhead.

5.New threads are easily created; new processes require duplication of the parent process.

6.Threads can exercise considerable control over threads of the same process; processes can only exercise control over child processes.

7.Changes to the main thread (cancellation, priority change, etc.) may affect the behavior of the other threads of the process; changes to the parent
process does not affect child processes.

=================================================================

进程(Process)和线程(Thread)的关系和区别(Difference)

这里有满详细的讲解

转贴 : happybabydudu的专栏

Differences in process and thread

Definition定义

-------------

Process

进程是应用程序的一次运行活动;

从操作系统核心角度来说,进程是操作系统分配和调度系统内存资源、cpu时间片等资源的基本单位,为正在运行的应用程序提供

运行环境。

Thread

线程是程序内部有并发性的顺序代码流。是cpu调度资源的最小单元。

Units单位大小

------------

Process

进程是操作系统分配和调度系统内存资源、cpu时间片等资源的基本单位;一个进程至少包括一个线程。

进程是操作系统资源管理的实体。

Thread

线程是cpu调度资源的最小单元。

线程是进程的实体。

Resource系统资源分配上

-------------

Process

每个进程都有自己的内存地址空间。

Thread

线程没有自己独立的内存资源,它只有自己的执行堆栈和局部变量。但是在同属一个进程的多个线程中他们可以共享进程的内存

资源。

Running执行过程中

-------------

执行过程中,进程有内存单元的初始入口点,在存活阶段里拥有独立的地址空间。

A process has the initial entrance of Memory Units and room of address.

进程是应用程序的一次运行活动,独立地执行;所以某一个进程崩溃以后,在保护模式下不会影响其他的进程,

健壮性好。

A process is activity of application.

父进程与子进程 的关系待研究深入中……

每个已创建的进程都可以创建进程,创建进程的进程称为父进程,被创建的新进程为子进程,这样便形成一个进程树。父进程与子进程可并行执行;父进程等待子进程终止执行。父进程终止后,所有的子进程也都必须要终止。

Thread

而线程不能独立地执行,它必须依附在一个运行中的应用程序上。

但是,同一个进程中的多个线程可以并发地执行,并发性高,系统在数据交换上花费的资源少,运行效率高。

主线程与其他线程 的关系待研究深入中……

每个进程里都会有一个主线程,由它创建其他线程。

======================================================

Common ground 共同点

--------------

Process和Thread都有生命周期:

create创建,ready就绪,running运行,waitSleepJoin阻塞,suspend挂起,stoped死亡

新增补充(2011.5.07)

一个地方,当一个程式开启以后,产生task,task先产生第一个进程并且执行第一个线程

而后进程可以在产生子进程,相当于呼叫下一个activity或者service

而单一进程里面可以有很多线程,这也就是multithread的观念,而一般我们在activty中,因为task的堆叠功能

而process可以轻易的有callback的功能,但是在线程呢?

所以更进阶的就要去学习如何包装线程,毕竟这在开发应用程式上面会有很大帮助

虽然不至于走向multitask的开发,但是multithread应该会很容易碰到,尤其service,甚至网路处理就会碰到了

而包装就要了解 thread handler message queue looper
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: