在傳統操做系統中,每一個進程有一個地址空間,並且默認就有一個控制線程編程
線程顧名思義,就是一條流水線工做的過程(流水線的工做須要電源,電源就至關於cpu),而一條流水線必須屬於一個車間,一個車間的工做過程是一個進程,車間負責把資源整合到一塊兒,是一個資源單位,而一個車間內至少有一條流水線。網絡
因此,進程只是用來把資源集中到一塊兒(進程只是一個資源單位,或者說資源集合),而線程纔是cpu上的執行單位。多線程
多線程(即多個控制線程)的概念是,在一個進程中存在多個線程,多個線程共享該進程的地址空間,至關於一個車間內有多條流水線,都共用一個車間的資源。例如,北京地鐵與上海地鐵是不一樣的進程,而北京地鐵裏的13號線是一個線程,北京地鐵全部的線路共享北京地鐵全部的資源,好比全部的乘客能夠被全部線路拉。併發
一、Threads share the address space of the process that created it; processes have their own address space. 二、Threads have direct access to the data segment of its process; processes have their own copy of the data segment of the parent process. 三、Threads can directly communicate with other threads of its process; processes must use interprocess communication to communicate with sibling processes. 四、New threads are easily created; new processes require duplication of the parent process. 五、Threads can exercise considerable control over threads of the same process; processes can only exercise control over child processes. 六、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.
總結上述區別,無非兩個關鍵點,這也是咱們在特定的場景下須要使用多線程的緣由:ide
開啓一個字處理軟件進程,該進程確定須要辦不止一件事情,好比監聽鍵盤輸入,處理文字,定時自動將文字保存到硬盤,這三個任務操做的都是同一塊數據,於是不能用多進程。只能在一個進程裏併發地開啓三個線程,若是是單線程,那就只能是,鍵盤輸入時,不能處理文字和自動保存,自動保存時又不能輸入和處理文字。ui