【正文】
ering or you need a lot of puting time, the suspension is not the problem, so users will need to enter the thread of highclass leisure, and graphics rendering, or low priority operation can be. Independent of each other between these threads. In the above example, graphics rendering, or a lot of puting is clearly need to stop using a lot of CPU time, during this period of time, the user waiting for them there is no need to reinput information is pleted, we will process designed to separate the two threads, one for the user39。一個進(jìn)程對應(yīng)著一段程序 ,它是由一些在同一個程序里面獨(dú)立的同時的運(yùn)行的線程組成的。實際上 ,系統(tǒng)運(yùn)行每個進(jìn)程的時間有 2 毫秒 ,然后調(diào)度其他的線程。在這種調(diào)度方式下 ,可能一個執(zhí)行時間很長的線程使得其他所有需要 cpu 的線程 ” 餓死 ” 。如果系統(tǒng)找到一個這樣的線程,就立即暫停當(dāng)前執(zhí)行的線程和激活滿足條件的線程。你可能會對多個用戶同時訪問數(shù)據(jù)庫或者 web 服務(wù)器感到吃驚,他們是怎么工作的?這是因為為每個連接到數(shù)據(jù)庫或者web 服務(wù)器的用戶建立了 獨(dú)立的線程來維護(hù)用戶的狀態(tài)。 有多種方法可以設(shè)計多線程的應(yīng)用程序。如果由于程序忙于一個任 務(wù),有可能會導(dǎo)致屏幕變成空白,這顯然需要我們的程序來處理這樣的事件。 我們應(yīng)該把握一個原則,對于那些對時間要求比較緊迫需要立即得到相應(yīng)的任務(wù),我們因該給予高的優(yōu)先級,而其他的線程優(yōu)先級應(yīng)該低于她的優(yōu)先級。線程可以有不同的優(yōu)先級,舉例子來說,在我們的應(yīng)用程序里面,繪制圖形或者做大量運(yùn)算的同時要接受用戶的輸入,顯然用戶的輸入需要得到第一時間的響應(yīng),而圖形繪制或者運(yùn)算則需要 大量的時間,暫停一下問題不大,因此用戶輸入線程將需要高的悠閑級,而圖形繪制或者運(yùn)算低優(yōu)先級即可。如果程序可以分成獨(dú)立的不同的任務(wù),使用多線程,即使某一部分任務(wù)失敗了,對其他的也沒有影響,不會導(dǎo)致整個程序崩潰。 當(dāng)正在執(zhí)行的線程想釋放 cpu 的控制權(quán)給一個低優(yōu)先級的線程,當(dāng)前線程就轉(zhuǎn)入睡眠狀態(tài)而讓低優(yōu)先級的線程占有 cpu。 占用 cpu 的線程擁有對 cpu 的控制權(quán),只有 它自己主動釋放 cpu 時,其他的線程才可以使用 cpu。線程的的切換和調(diào)度是如此之快 ,以至于感覺是所有的線程是同步執(zhí)行的。在一個進(jìn)程里 ,線程的調(diào)度有搶占式或者非搶占的模式。 it needs to be the first time responses, and its priority because of the highpriority. 附錄 二 多線程相對于操作系統(tǒng)而言 ,指的是可以同時執(zhí)行同一個程序的不同部分的能力 ,每個執(zhí)行的部分被成為線程。 it has a starting point for the implementation of the order of series and a destination. It is responsible for the maintenance of its own stack, the stack for exception handling, priority scheduling and a number of other threading systems reimplementation of the information needed. From this concept, it seems there is no thread with the process of distinction, in fact, thread and process is certainly different: the process of a plete independence to have their own memory space and data, but the same thread within a process is shared memory space and data. Corresponds to a process for some procedures, it is by the same procedure in which an independent threads running at the same time the position. Thread is sometimes referred to as parallel program running on lightweight processes, threads are lightweight processes called because it is dependent on the operation and process the context of the environment, and the process of using resources. In a process, the thr