1. 程式人生 > >《Efficient Android Threading》Chapter 3---Threads on Android (Android中的執行緒)

《Efficient Android Threading》Chapter 3---Threads on Android (Android中的執行緒)

Android中的執行緒

序: 
      閱讀英文原版,受益匪淺。修飾詞形容惟妙惟肖。 
      其中舉例之圖,經典,且秒不可言。亦當細細品味之。 
      翻譯為谷歌翻譯。 重點單詞日後補上,可用有道詞典積累,認識的單詞會越來越多
      不認識的單詞亦會越來越少,而翻譯並不能完全體現作者本意,亦可備註評論之上。
      佛祖拈花 迦葉一笑。觀者千人,體會亦千面。 所思所想亦可評論,互相交流。 
      亦可以鍛鍊英文文件閱讀能力。

整理如下。

Every Android application is started with numerous threads that are bundled with the 

Linux process and the Dalvik VM to manage its internal execution. But the application is exposed to system threads, like the UI and binder threads, and creates background threads of its own. In this chapter, we’ll get under the hood of threading on the Android platform, examining the following: 
• Differences and similarities between UI, binder, and background threads 
• Linux thread coupling 
• How thread scheduling is affected by the application process rank 
• Running Linux threads

每個Android應用程式都啟動了許多與Linux程序和Dalvik VM捆綁在一起的執行緒來管理其內部執行。 但應用程式暴露於系統執行緒,如UI和繫結執行緒,並建立自己的後臺執行緒。 在本章中,我們將介紹Android平臺上的執行緒,並檢查以下內容: 
•UI,binder和後臺執行緒之間的差異和相似之處 
•Linux執行緒耦合 
•執行緒排程如何受到應用程式程序排名的影響 
•執行Linux執行緒

Android Application Threads 
All application threads are based on the native pthreads in Linux with a Thread representation in 

Java, but the platform still assigns special properties to threads that make them differ. From an application perspective, the thread types are UI, binder, and background threads.

Android應用主題 
所有應用程式執行緒都是基於Java中的執行緒表示形式的Linux中的本機pthread,但該平臺仍然為使其不同的執行緒分配特殊屬性。 從應用程式的角度來看,執行緒型別是UI,binder和後臺執行緒。

UI Thread 
The UI thread is started on application start and stays alive during the lifetime of the Linux process. The UI thread is the main thread of the application, used for executing Android components and updating the UI elements on the screen. If the platform detects that UI updates are attempted from any other thread, it will promptly notify the application by throwing a CalledFromWrongThreadException. This harsh platform 
behavior is required because the Android UI Toolkit is not thread safe, so the runtime allows access to the UI elements from one thread only.

UI執行緒 
UI執行緒在應用程式啟動時啟動,並在Linux程序的生命週期內保持活動狀態。 UI執行緒是應用程式的主執行緒,用於執行Android元件並更新螢幕上的UI元素。 如果平臺檢測到從任何其他執行緒嘗試UI更新,它將立即通過丟擲CalledFromWrongThreadException來通知應用程式。 這是一個苛刻的平臺行為,因為Android UI Toolkit不是執行緒安全的,所以執行時允許只從一個執行緒訪問UI元素。

UI elements in Android are often defined as instance fields of activ‐ ities, so they constitute a part of the object’s state. However, access to those elements doesn’t require synchronization because UI elements can be accessed only from the UI thread. In other words, the run- time enforces a single-threaded environment for UI elements, so they are not susceptible to concurrency problems.

Android中的UI元素通常定義為活動的例項欄位,因此它們構成物件狀態的一部分。 然而,訪問這些元素不需要同步,因為只能從UI執行緒訪問UI元素。 換句話說,執行時為UI元素強制執行單執行緒環境,因此它們不會受到併發問題的影響。

The UI thread is a sequential event handler thread that can execute events sent from any other thread in the platform. The events are handled serially and are queued if the UI thread is occupied with processing a previous event. Any event can be posted to the UI thread, but if events are sent that do not explicitly require the UI thread for execution, the UI-critical events may have to wait in the queue before being processed and before responsiveness is decreased.

UI執行緒是可以執行從平臺中任何其他執行緒傳送的事件的順序事件處理程式執行緒。 事件被序列處理,並且如果UI執行緒被處理前一個事件佔用,則排隊。 任何事件都可以釋出到UI執行緒,但是如果傳送的事件不明確地要求UI執行緒執行,則UI關鍵事件可能必須在被處理之前等待佇列,並且響應性降低之前。 
“Android Message Passing” on page 47 describes event handling in detail. 
第47頁上的“Android訊息傳遞”詳細描述了事件處理。

Binder Threads 
Binder threads are used for communicating between threads in different processes. Each process maintains a set of threads, called a thread pool, that is never terminated or recreated, but can run tasks at the request of another thread in the process. These threads handle incoming requests from other processes, including system services, intents, content providers, and services. When needed, a new binder thread will be created to handle the incoming request. In most cases, an application does not have to be con‐ cerned about binder threads because the platform normally transforms the requests to use the UI thread first. The exception is when the application offers a Service that can be bound from other processes via an AIDL interface. Binder threads are discussed more thoroughly in Chapter 5.

粘合執行緒 
執行緒執行緒用於在不同程序中的執行緒之間進行通訊。 每個程序維護一組執行緒,稱為執行緒池,從未終止或重新建立,但可以根據程序中另一個執行緒的請求執行任務。 這些執行緒處理來自其他程序的傳入請求,包括系統服務,意圖,內容提供者和服務。 當需要時,將建立一個新的繫結執行緒來處理傳入的請求。 在大多數情況下,應用程式不必關心繫結執行緒,因為平臺通常會先轉換使用UI執行緒的請求。 例外是當應用程式提供可以通過AIDL介面從其他程序繫結的服務時。 第5章中更詳細地討論了粘結螺紋。

Background Threads 
All the threads that an application explicitly creates are background threads. This means that they have no predefined purpose, but are empty execution environments waiting to execute any task. The background threads are descendants of the UI thread, so they inherit the UI thread properties, such as its priority. By default, a newly created process doesn’t contain any background threads. It is always up to the application itself to create them when needed.

後臺執行緒 
應用程式顯式建立的所有執行緒都是後臺執行緒。 這意味著它們沒有預定義的目的,而是等待執行任何任務的空執行環境。 後臺執行緒是UI執行緒的後代,因此它們繼承UI執行緒屬性,例如其優先順序。 預設情況下,新建立的程序不包含任何後臺執行緒。 應用程式本身在需要時建立它們是始終如一的。 
The second part of this book, Part II, is all about creating back‐ ground threads. 
本書第二部分第二部分是關於建立背景執行緒的。

A background thread created here in the application would look like this in the ps -t output. The last field is the name. The thread name, by default, ends with the number assigned by the runtime to the thread as its ID: 
u0_a72 4283 4257 320304 34540 ffffffff 00000000 S Thread-12412 
In the application, the use cases for the UI thread and worker threads are quite different, but in Linux they are both plain native threads and are handled equally. The constraints on the UI thread—that it should handle all UI updates—are enforced by the Window Manager in the Application Framework and not by Linux.

在應用程式中建立的後臺執行緒將在ps -t輸出中看起來像這樣。 最後一個欄位是名稱。 預設情況下,執行緒名稱以執行時分配給執行緒的號碼作為其ID: 
u0_a72 4283 4257 320304 34540 ffffffff 00000000 S Thread-12412 
在應用程式中,UI執行緒和工作執行緒的用例是完全不同的,但是在Linux中它們都是純本地執行緒,並且被平等地處理。 UI執行緒的限制 - 它應該處理所有UI更新 - 由應用程式框架中的Window Manager執行,而不是由Linux執行。

The Linux Process and Threads 
The execution of long operations on background threads on Android can be handled in many ways, but no matter how the application implements the execution mechanism, the threads, in the end, are always the same on the operating system level. The Android platform is a Linux-based OS, and every application is executed as a Linux application in the OS. Both the Android application and its threads adhere to the Linux execution environment. As we will see, knowledge of the Linux environment helps us not only to grasp and investigate the application execution, but also to improve our applications’ performance.

Linux程序和執行緒 
在Android上的後臺執行緒上執行長操作可以通過多種方式進行處理,但無論應用程式如何實現執行機制,執行緒在作業系統級別上總是相同的。 Android平臺是基於Linux的作業系統,每個應用程式都是作為作業系統中的Linux應用程式執行的。 Android應用程式及其執行緒都遵守Linux執行環境。 我們將看到,Linux環境的知識有助於我們不僅要掌握和調查應用程式的執行情況,還可以提高應用程式的效能。

Each running application has an underlying Linux process, forked from the prestarted Zygote process, which has the following properties: 
User ID (UID) 
A process has a unique user identifier that represents a user on a Linux system. Linux is a multiuser system, and on Android, each application represents a user in this system. When the application is installed, it is assigned a user ID. 
Process identifier (PID) 
A unique identifier for the process. 
Parent process identifier (PPID) 
After system startup, each process is created from another process. The running system forms a tree hierarchy of the running processes. Hence, each application process has a parent process. For Android, the parent of all processes is the Zygote. 
Stack 
Local function pointers and variables. 
Heap 
The address space allocated to a process. The address space is kept private to a process and can’t be accessed by other processes.

每個正在執行的應用程式都有一個底層的Linux程序,從預先分配的Zygote程序分支,它具有以下屬性: 
使用者ID(UID) 
一個程序有一個唯一的使用者識別符號,表示Linux系統上的一個使用者。 Linux是一個多使用者系統,在Android上,每個應用程式都代表了該系統中的使用者。 安裝應用程式後,會分配一個使用者ID。 
程序識別符號(PID) 
程序的唯一識別符號。 
父程序識別符號(PPID) 
系統啟動後,每個程序都是從另一個程序建立的。 執行的系統形成執行程序的樹狀層次結構。 因此,每個應用程序都有一個父程序。 對於Android,所有程序的父級都是Zygote。 
堆 
區域性函式指標和變數。 
堆 
分配給程序的地址空間。 地址空間對程序保持私有,不能被其他程序訪問。

Finding Application Process Information 
The process information of a running application is retrieved by the ps (process status) command, which you can call from the ADB shell. The Android ps command retrieves process information just as it would on any Linux distribution. However, the set of options is different than the traditional Linux version of ps: 
-t 
Shows thread information in the processes 
-x 
Shows time spent in user code (utime) and system code (stime) in “jiffies,” which typically is units of 10 ms. 
-p 
Shows priorities. 
-P 
Shows scheduling policy, normally indicating whether the application is executing in the foreground or background. 
-c 
Shows which CPU is executing the process.

name | pid 
Filter on the application’s name or process ID. Only the last defined value is used. 
You can also filter through the grep command. For instance, executing the ps command for a com.eat application1 process would look like this: 
$ adb shell ps | grep com.eat 
USER PID PPID VSIZE RSS WCHAN PS NAME 
u0_a72 4257 144 320304 34540 ffffffff 00000000 S com.eat 
From this output, we can extract the following interesting properties of the com.eat application: 
• UID: u0_a72 
• PID: 4257 
• PPID: 144 (process number of the parent, which in the case of an Android appli‐ cation is always the Zygote) 
Another way of retrieving process and thread information is with DDMS2 in the An‐ droid tools.

  1. I have used the string EAT to create a namespace for applications in this book. The string is an acronym of the book’s title.

查詢申請流程資訊 
正在執行的應用程式的程序資訊可以通過ps(程序狀態)命令檢索,您可以從ADB shell呼叫它。 Android ps命令與任何Linux發行版一樣檢索程序資訊。但是,這套選項與傳統的Linux版本的ps不同: 
-t 
顯示程序中的執行緒資訊 
-X 
顯示在“jiffies”中的使用者程式碼(utime)和系統程式碼(stime)中花費的時間,通常為10 ms的單位。 
-p 
顯示優先順序 
-P 
顯示排程策略,通常指示應用程式是在前臺還是後臺執行。 
-C 
顯示哪個CPU正在執行該程序。

名稱| PID 
過濾應用程式的名稱或程序ID。只使用最後定義的值。 
您還可以通過grep命令進行過濾。例如,執行com.eat application1程序的ps命令將如下所示: 
$ adb shell ps | grep com.eat 
USER PID PPID VSIZE RSS WCHAN PS NAME 
u0_a72 4257 144 320304 34540 ffffffff 00000000 S com.eat 
從這個輸出中,我們可以提取com.eat應用程式的以下有趣的屬性: 
•UID:u0_a72 
•PID:4257 
•PPID:144(父應用程式的程序號,在Android應用程式的情況下,始終是Zygote) 
檢索程序和執行緒資訊的另一種方法是使用DDMS2進行安裝。 
我使用字串EAT為本書中的應用程式建立一個名稱空間。該字串是本書標題的縮寫。

All the threads that an application creates and starts are native Linux threads, a.k.a. pthreads, because they were defined in a POSIX standard. The threads belong to the process where they were created, and the parent of each thread is the process. Threads and processes are very much alike, with the difference between them coming in the sharing of resources. The process is an isolated execution of a program in a sandboxed environment compared to other processes, whereas the threads share the resources within a process. An important distinction between processes and threads is that pro‐ cesses don’t share address space with each other, but threads share the address space within a process. This memory sharing makes it a lot faster to communicate between threads than between processes, which require remote procedure calls that take up more overhead. Thread communication is covered in Chapter 4 and process communication in Chapter 5. 
When a process starts, a single thread is automatically created for that process. A process always contains at least one thread to handle its execution. In Android, the thread cre‐ ated automatically in a process is the one we’ve already seen as the UI thread. 
Let’s take a look at the threads created in a process for an Android application with the package name com.eat: 
$ adb shell ps -t | grep u0_a72 
USER PID PPID VSIZE RSS WCHAN PS NAME 
u0_a72 4257 144 320304 34540 ffffffff 00000000 S com.eat 
u0_a72 4259 4257 320304 34540 ffffffff 00000000 S GC 
u0_a72 4262 4257 320304 34540 ffffffff 00000000 S Signal Catcher 
u0_a72 4263 4257 320304 34540 ffffffff 00000000 S JDWP 
u0_a72 4264 4257 320304 34540 ffffffff 00000000 S Compiler 
u0_a72 4265 4257 320304 34540 ffffffff 00000000 S ReferenceQueueDemon 
u0_a72 4266 4257 320304 34540 ffffffff 00000000 S FinalizerDaemon 
u0_a72 4267 4257 320304 34540 ffffffff 00000000 S FinalizerWatchdogDaemon 
u0_a72 4268 4257 320304 34540 ffffffff 00000000 S Binder_1 
u0_a72 4269 4257 320304 34540 ffffffff 00000000 S Binder_2

應用程式建立和啟動的所有執行緒都是本機的Linux執行緒又名並行執行緒,因為它們是在POSIX標準中定義的。執行緒屬於建立它們的程序,每個執行緒的父程序都是程序。執行緒和程序非常相似,它們之間的區別在於共享資源。該過程是與其他程序相比,沙盒環境中的程式的獨立執行,而執行緒共享程序中的資源。程序和執行緒之間的一個重要區別是程序之間不會共享地址空間,而是執行緒共享程序內的地址空間。這種記憶體共享使執行緒之間的通訊速度比程序之間的速度要快得多,這些程序需要佔用更多開銷的遠端過程呼叫。執行緒通訊在第4章和第5章的過程通訊中有所描述。 
當一個程序開始時,為該程序自動建立一個執行緒。一個程序總是包含至少一個執行緒來處理它的執行。在安卓中,在一個程序中自動建立的執行緒是我們已經看到的UI執行緒。 
讓我們來看看一個Android的應用程式的程序中建立的執行緒,其中包含名為com.eat的包: 
$ adb shell ps -t | grep u0_a72 
USER PID PPID VSIZE RSS WCHAN PS NAME 
u0_a72 4257 144 320304 34540 ffffffff 00000000 S com.eat 
u0_a72 4259 4257 320304 34540 ffffffff 00000000 S GC 
u0_a72 4262 4257 320304 34540 ffffffff 00000000 S訊號捕獲器 
u0_a72 4263 4257 320304 34540 ffffffff 00000000 S JDWP 
u0_a72 4264 4257 320304 34540 ffffffff 00000000 S編譯器 
u0_a72 4265 4257 320304 34540 ffffffff 00000000 S ReferenceQueueDemon 
u0_a72 4266 4257 320304 34540 ffffffff 00000000 S FinalizerDaemon 
u0_a72 4267 4257 320304 34540 ffffffff 00000000 S FinalizerWatchdogDaemon 
u0_a72 4268 4257 320304 34540 ffffffff 00000000 S Binder_1 
u0_a72 4269 4257 320304 34540 ffffffff 00000000 S Binder_2

On application start, no fewer than 10 threads are started in our process. The first thread —named com.eat—is started by default when the application launches. Hence, that is the UI thread of the application. All the other threads are spawned from the UI thread, which is seen on the parent process ID (PPID) of the other threads. Their PPID corre‐ sponds to the process ID (PID) of the UI thread.

在應用程式啟動時,在我們的程序中啟動不少於10個執行緒。 第一個執行緒名為com.eat - 在應用程式啟動時預設啟動。 因此,這是應用程式的UI執行緒。 所有其他執行緒都是從其他執行緒的父程序標識(PPID)中看到的UI執行緒生成的。 它們的PPID對應於UI執行緒的程序ID(PID)。 
Most of the threads are Dalvik internal threads, and we don’t have to worry about them from an application perspective. They handle garbage collection, debug connections, finalizers, etc. Let’s focus on the threads we need to pay attention to: 
u0_a72 4257 144 320304 34540 ffffffff 00000000 S com.eat 
u0_a72 4268 4257 320304 34540 ffffffff 00000000 S Binder_1 
u0_a72 4269 4257 320304 34540 ffffffff 00000000 S Binder_2

大多數執行緒是Dalvik內部執行緒,我們不必從應用程式角度來擔心它們。 他們處理垃圾收集,除錯連線,終結器等。讓我們關注我們需要注意的執行緒: 
u0_a72 4257 144 320304 34540 ffffffff 00000000 S com.eat 
u0_a72 4268 4257 320304 34540 ffffffff 00000000 S Binder_1 
u0_a72 4269 4257 320304 34540 ffffffff 00000000 S Binder_2

Scheduling 
Linux treats threads and not processes as the fundamental unit for execution. Hence, scheduling on Android concerns threads and not processes. Scheduling allocates exe‐ cution time for threads on a processor. Each thread that is executing in an application is competing with all of the other threads in the application for execution time. The scheduler decides which thread should execute and for how long it should be allowed to execute before it picks a new thread to execute and a context switch occurs. A sched‐ uler picks the next thread to execute depending on some thread properties, which are different for each scheduler type, although the thread priority is the most important one. In Android, the application threads are scheduled by the standard scheduler in the Linux kernel and not by the Dalvik virtual machine. In practice, this means that the threads in our application are competing not only directly with each other for execution time, but also against all threads in all the other applications.

排程 
Linux將執行緒而不是程序作為執行的基本單位。因此,Android上的安排涉及到執行緒而不是程序。計劃為處理器上的執行緒分配執行時間。在應用程式中執行的每個執行緒與應用程式中的所有其他執行緒競爭執行時間。排程程式決定哪個執行緒應該執行,以及在選擇要執行的新執行緒之前應該允許執行多長時間,併發生上下文切換。執行緒優先順序是最重要的一個執行緒,根據某些執行緒屬性選擇要執行的下一個執行緒,每個執行緒屬性對於每個排程器型別是不同的。在Android中,應用程式執行緒由Linux核心中的標準排程程式排程,而不是由Dalvik虛擬機器安排。實際上,這意味著我們的應用程式中的執行緒不僅直接競爭執行時間,而且還針對所有其他應用程式中的所有執行緒進行競爭。

The Linux kernel scheduler is known as a completely fair scheduler (CFS). It is “fair” in the sense that it tries to balance the execution of tasks not only based on the priority of the thread but also by tracking the amount of execution time3 that has been given to a thread. If a thread has previously had low access to the processor, it will be allowed to execute before higher-prioritized threads. If a thread doesn’t use the allocated time to execute, the CFS will ensure that the priority is lowered so that it will get less execution time in the future. 
The platform mainly has two ways of affecting the thread scheduling: 
Priority 
Change the Linux thread priority. 
Control group 
Change the Android-specific control group.

Linux核心排程程式被稱為完全公平的排程程式(CFS)。 在這種意義上,它是“公平的”,它試圖平衡任務的執行,不僅基於執行緒的優先順序,而且還通過跟蹤已經給執行緒的執行時間3。 如果執行緒以前對處理器的訪問許可權較低,則允許在更高優先順序的執行緒之前執行。 如果執行緒不使用分配的時間執行,CFS將確保優先順序降低,以便將來可以減少執行時間。 
該平臺主要有兩種影響執行緒排程的方式: 
優先 
更改Linux執行緒的優先順序。 
控制組 
更改Android特定的控制組。

Priority 
All threads in an application are associated with a priority that indicates to the scheduler which thread it should allocate execution time to on every context switch. On Linux, the thread priority is called niceness or nice value, which basically is an indication of how nice a certain thread should behave toward other threads. Hence, a low niceness corresponds to a high priority. In Android, a Linux thread has niceness values in the range of -20 (most prioritized) to 19 (least prioritized), with a default niceness of 0. A thread inherits its priority from the thread where it is started and keeps it unless it’s explicitly changed by the application. 
An application can change priority of threads from two classes:

An application can change priority of threads from two classes: 
java.lang.Thread 
setPriority(int priority); 
Sets the new priority based on the Java priority values from 0 (least prioritized) to 
10 (most prioritized). 
android.os.Process 
Process.setThreadPriority(int priority); // Calling thread. Process.setThreadPriority(int threadId, int priority); // Thread with 
// specific id. 
Sets the new priority using Linux niceness, i.e. -20 to 19.

優先 
應用程式中的所有執行緒與優先順序相關聯,該優先順序指示排程程式哪個執行緒應該在每個上下文切換上分配執行時間。在Linux上,執行緒優先順序被稱為優點或漂亮的值,這基本上表明某個執行緒應該對其他執行緒行為有多好。因此,低的優點對應於高優先順序。在Android中,Linux執行緒具有-20(最優先順序)到19(最優先順序)的精確度值,預設值為0.執行緒從啟動的執行緒繼承其優先順序,並保留它,除非它是由應用程式明確更改。 
應用程式可以從兩個類更改執行緒的優先順序:

應用程式可以從兩個類更改執行緒的優先順序: 
java.lang.Thread中 
setPriority(int priority); 
基於Java優先順序值(從0(最小優先順序)到)設定新的優先順序 
10(最優先)。 
android.os.Process 
Process.setThreadPriority(int priority); //呼叫執行緒Process.setThreadPriority(int threadId,int priority); //執行緒與 
//具體ID。 
使用Linux的好處設定新的優先順序,即-20到19。

Java Priority Versus Linux Niceness 
Thread.setPriority() is platform independent. It represents an abstraction of the underlying platform-specific thread priorities. The abstract priority values correspond to Linux niceness values according to the following table:

Java優先順序與Linux的精確性 
Thread.setPriority()與平臺無關。 它代表了基於平臺的執行緒優先順序的抽象。 抽象優先順序值對應於Linux niceness值,如下表所示:

這裡寫圖片描述
The mapping of Java priorities is an implementation detail and may vary depending on platform version. The niceness mapping values in the table are from Jelly Bean. 
Java優先順序的對映是一個實現細節,可能會根據平臺版本而有所不同。 表中的精美度對映值來自Jelly Bean。

Control groups 
Android not only relies on the regular Linux CFS for thread scheduling, but also imposes thread control groups4 on all threads. The thread control groups are Linux containers that are used to manage the allocation of processor time for all threads in one Container. All threads created in an application belong to one of the thread control groups. 
Android defines multiple control groups, but the most important ones for applications are the Foreground Group and Background Group. The Android platform defines ex‐ ecution constraints so that the threads in the different control groups are allocated different amounts of execution time on the processor. Threads in the Foreground Group are allocated a lot more execution time than threads in the Background Group,5 and Android utilizes this to ensure that visible applications on the screen get more processor allocation than applications that are not visible on the screen. The visibility on the screen relates to the process levels (see “The Linux Process and Threads” on page 31), as illus‐ trated in Figure 3-1. 
對照組 
Android不僅依賴於常規的Linux CFS進行執行緒排程,而且還對所有執行緒強加執行緒控制組4。執行緒控制組是Linux容器,用於管理一個容器中所有執行緒處理器時間的分配。在應用程式中建立的所有執行緒都屬於其中一個執行緒控制組。 
Android定義了多個控制組,但最重要的應用程式是前臺組和後臺組。 Android平臺定義了執行約束,以便在處理器上為不同控制組中的執行緒分配不同的執行時間。前臺組中的執行緒比後臺組中的執行緒分配了更多的執行時間,而且Android利用此功能可確保螢幕上的可見應用程式獲得比在螢幕上不可見的應用程式更多的處理器分配。螢幕上的可見性與過程級別有關(請參見第31頁的“Linux程序和執行緒”),如圖3-1所示。

這裡寫圖片描述

If an application runs at the Foreground or Visible process level, the threads created by that application will belong to the Foreground Group and receive most of the total processing time, while the remaining time will be divided among the threads in the other applications. A ps command issued on a foreground thread shows something like this (note the appearance of the fg group): 
adbshellpsP|grepu0a72u0a72425714432030434504fgffffffff00000000Scom.eatIftheusermovesanapplicationtothebackground,suchasbypressingtheHomebu

相關推薦

weex 載入三端(android ios web) 本地圖片 解決專案已實踐

  本文講解內容為weex載入三端本地圖片,所寫解決方案均已驗證,使用sdk版本為0.18.0,其他版本不能保證可行。   weex載入圖片方式有三種,1.src直接引用base64編碼,2.載入網路圖片,3.載入本地圖片(三端分別放在專案目錄)。前兩種都是比較簡單直接,第三種相對初

python 學習第二十二天程序和執行

程序 程序就是一個程式在一個數據集上的一次動態執行過程。 程序一般由程式、資料集、程序控制塊三部分組成。 我們編寫的程式用來描述程序要完成哪些功能以及如何完成; 資料集則是程式在執行過程中所需要使用的資源; 程序控制塊用來記錄程序的外部特徵,描述程序的執行變化過程,系統可以利

QT學習記錄2QT多執行

對QT多執行緒的理解 qt多執行緒基於QThread類,在使用的時候,可以選擇新建類,然後繼承QThread類,然後重寫run函式,從而實現QT多執行緒   QT多執行緒小例程 首先,建立一個QT工程,名字叫做mythread,視窗選擇widget,然後一路下一步即可,生成

小白帶你認識netty之NioEventLoop的執行或者reactor執行啟動

在上一章中,我們看了處理IO事件的過程,今天,我們瞅瞅處理非同步任務佇列。 3、處理非同步任務佇列 在執行完processSelectedKeys方法後,netty會繼續執行runAllTasks方法,在觀摩這個方法之前,我們瞭解下netty的task。 在初始化NioEventLoop的時候,會例

Java執行簡介什麼是執行

一、執行緒概述 執行緒是程式執行的基本執行單元。當作業系統(不包括單執行緒的作業系統,如微軟早期的DOS)在執行一個程式時,會在系統中建立一個程序,而在這個程序中,必須至少建立一個執行緒(這個執行緒被稱為主執行緒)來作為這個程式執行的入口點。因此,在作業系統中執行的任何程式

winform執行時可以拖動視窗C#多執行

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; u

併發通訊程序與執行

程序與執行緒在通訊中都會遇到各自的問題,這是因他們各自的而產生的。每個程序有自己的地址空間。兩個程序中的地址即使值相同,實際指向的位置也不同,故程序間無法直接就能通訊。雖然同一程序的執行緒共享全域性變數和記憶體,使得執行緒之間共享資料很容易也很方便,但會帶來某些共享資料的互斥

java學習筆記20-多執行

1. Thread類 1.1 繼承實現 package com.daigua20; public class ThreadDemo { public static void main(String[] args) { MyThread t1 = new MyThread();

《計算機作業系統》總結二程序與執行

作業系統(計算機)程序和執行緒管理 主要內容: 程序與執行緒 程序概念;程序的狀態與轉換程序控制;程序組織程序通訊;執行緒概念與多執行緒模型處理器排程 排程的基本概念;排程時機、切換與過程排程的基本準

(8)什麼是執行如何開啟執行

為什麼會出現執行緒 程序的缺點: 1、非常消耗資源,計算機不能無限開啟子程序 2、如果開了過多的程序,cpu的切換程序的模式下是非常耗時的   因為程序的缺點,執行緒的出現就是為了解決程序的缺點,執行緒的開銷小於程序 1、所以執行緒就是輕量級的程序 2、一個程序裡面至少有一個執行緒

作業系統面試筆試題總結二程序與執行

程式在併發系統內執行的特點:程式執行的間斷性,相互通訊的可能性,資源分配的動態性 在下面關於併發性的敘述中正確的是:併發性是指若干事件在同一時間間隔發生 一般來說,為了實現多道程式設計,計算機最需要更大的記憶體 Unix作業系統的程序控制塊中常駐記憶體的是

python聊天程式socket+多執行

用Python實現點對點的聊天,2個程式,一個是client.py,一個是server.py,通過本機地址127.0.0.1連線進行通訊,利用多執行緒把傳送訊息和接收訊息分開獨立進行。 clien

Java Swing複習11進度條涉及多執行

package www10m10; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swin

Efficient Android Threading》Chapter 3---Threads on Android Android執行

Android中的執行緒 序:        閱讀英文原版,受益匪淺。修飾詞形容惟妙惟肖。        其中舉例之圖,經典,且秒不可言。亦當細細品味之。        翻譯為谷歌翻譯。 重點單詞日後補上,可用有道詞典積累,認識的單詞會越來越多  

Android開發————3、簡易備忘錄

實驗內容 1. android 開發環境搭建, 開發工具可以是eclipse+adt 或android studio。 2. 按照實驗課要求製作對應的app小程式,實現按鈕新增備忘,並且包含日期時間。 3. 編寫並提交實驗報告。 實驗步驟 實驗程式碼如下: <

React-Native 與原生的3種互動通訊Android

前言 最近到新公司,採用React-Native開發App。在某些效能方面有問題或者模組特殊的開發情況,不可避免的需要我們原生開發(Android\IOS)給予前端開發支援。 在為前端書寫模組部分,不可避免的要接觸核心的通訊部分。 大致分為2種情況:

Android資源之圖像資源狀態圖像資源

one android資源 nco 文件夾 nts 淡出 else if fontsize bsp 在上一篇博文中。我主要解說了XML圖像資源中的圖層資源,在此圖像資源博文中我會給大家陸續解說XMl圖像資源的圖像狀態資源、圖像級別資源、淡入淡出資源、嵌入圖像資源、剪切圖

Android資源之圖像資源圖像級別資源

ons 分享 博文 target button track off http tails 圖像狀態資源僅僅能定義有限的幾種狀態。假設須要很多其它的狀態,就要使用圖像級別資源。在該資源文件裏能夠定義隨意多個圖像級別。每一個圖像級別是一個整數區間,能夠通過ImageView

android mvp高速開發框架介紹dileber的簡單介紹

activity 數據 -c pos androi mod family 基於 ebe 今天我為大家介紹一款android mvp框架:dileber(https://github.com/dileber/dileber.git) 官方交流qq群:171443

關於Android Studio 3.0 報錯 com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details

當你使用android外掛for gradle 3.0時,會預設啟用Aapt2。 Android的Gradle 3.0外掛預設啟動Aapt2,目的是為了改進增量資源的處理。aapt2 適配之資源 id 固定 在網上大部分給出的解決方案 都是在工程目錄下開啟gradle.prop