2011年5月5日 星期四

Android task process thread 進程與線程

要了解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
這個這兩天我會找時間來好好的寫一下
希望大家喜歡
內容滿意的話,可以順便看看廣告喔,謝謝

沒有留言:

張貼留言