執行緒與執行緒處理Threads and threading

多執行緒處理可讓您提高應用程式的回應能力,如果應用程式在多處理器或多核心系統上執行,也可提高其輸送量。Multithreading allows you to increase the responsiveness of your application and, if your application runs on a multiprocessor or multi-core system, increase its throughput.

處理序和執行緒Processes and threads

「處理序」** 是正在執行的程式。A process is an executing program. 作業系統使用處理序來分隔正在執行的應用程式。An operating system uses processes to separate the applications that are being executed. 「執行緒」** 是要作業系統配置處理器時間的基本單位。A thread is the basic unit to which an operating system allocates processor time. 每個執行緒都有排程優先順序,並保有系統在執行緒執行暫停時用來儲存執行緒內容的一組結構。Each thread has a scheduling priority and maintains a set of structures the system uses to save the thread context when the thread's execution is paused. 執行緒內容包括執行緒順暢繼續執行所需的所有資訊,包括執行緒的一組 CPU 暫存器和堆疊。The thread context includes all the information the thread needs to seamlessly resume execution, including the thread's set of CPU registers and stack. 多個執行緒可在一個處理序中執行。Multiple threads can run in the context of a process. 一個處理序的所有執行緒都共用該處理序的虛擬位址空間。All threads of a process share its virtual address space. 執行緒可以執行任一部分的程式碼,包括另一個執行緒目前正在執行的部分。A thread can execute any part of the program code, including parts currently being executed by another thread.

注意

.NET Framework 提供使用「應用程式定義域」** 來隔離處理序內應用程式的方式 The .NET Framework provides a way to isolate applications within a process with the use of application domains. (應用程式域在 .NET Core 上不可用。有關詳細資訊,請參閱應用程式域一文中的應用程序域和線程部分。(Application domains are not available on .NET Core.) For more information, see the Application domains and threads section of the Application domains article.

根據預設,.NET 程式會從單一執行緒開始,通常稱為「主」** 執行緒。By default, a .NET program is started with a single thread, often called the primary thread. 不過,它可以建立額外的執行緒與主執行緒平行或同時執行程式碼。However, it can create additional threads to execute code in parallel or concurrently with the primary thread. 這些緒通常是輔助線程These threads are often called worker threads.

使用多執行緒的時機When to use multiple threads

多執行緒可讓您用來提高應用程式的回應能力,並利用多處理器或多核心系統來提高應用程式的輸送量。You use multiple threads to increase the responsiveness of your application and to take advantage of a multiprocessor or multi-core system to increase the application's throughput.

請想一想傳統型應用程式,主執行緒負責處理使用者介面項目和回應使用者動作。Consider a desktop application, in which the primary thread is responsible for user interface elements and responds to user actions. 背景工作執行緒可用來執行耗時的作業,否則這些作業會佔用主執行緒,而導致使用者介面沒有回應。Use worker threads to perform time-consuming operations that, otherwise, would occupy the primary thread and make the user interface non-responsive. 您也可以使用專用執行緒進行網路或裝置的通訊,以更快速地回應內送訊息或事件。You also can use a dedicated thread for network or device communication to be more responsive to incoming messages or events.

如果您的程式執行可平行完成的作業,那麼在個別執行緒中執行這些作業,並在多處理器或多核心系統上執行程式可減少總執行時間。If your program performs operations that can be done in parallel, the total execution time can be decreased by performing those operations in separate threads and running the program on a multiprocessor or multi-core system. 在這類系統上,使用多執行緒處理可提高輸送量及回應能力。On such a system, use of multithreading might increase throughput along with the increased responsiveness.

如何在 .NET 中使用多執行緒處理。How to use multithreading in .NET

從 .NET Framework 4 開始,建議使用工作平行程式庫 (TPL)平行 LINQ (PLINQ) 來利用多執行緒處理。Starting with the .NET Framework 4, the recommended way to utilize multithreading is to use Task Parallel Library (TPL) and Parallel LINQ (PLINQ). 如需詳細資訊,請參閱平行程式設計For more information, see Parallel programming.

TPL 和 PLINQ 都憑藉 ThreadPool 執行緒來運作。Both TPL and PLINQ rely on the ThreadPool threads. System.Threading.ThreadPool 類別會為 .NET 應用程式提供背景工作執行緒集區。The System.Threading.ThreadPool class provides a .NET application with a pool of worker threads. 您也可以使用執行緒集區執行緒。You also can use thread pool threads. 如需詳細資訊,請參閱受控執行緒集區For more information, see The managed thread pool.

最後,您可以使用 System.Threading.Thread 代表受控執行緒的類別。At last, you can use the System.Threading.Thread class that represents a managed thread. 如需詳細資訊,請參閱使用執行緒和執行緒處理For more information, see Using threads and threading.

多個執行緒可能需要存取共用資源。Multiple threads might need to access a shared resource. 為了讓資源保持在未損毀的狀態並避免競爭條件,您必須同步對該資源的執行緒存取。To keep the resource in a uncorrupted state and avoid race conditions, you must synchronize the thread access to it. 您也可能想要協調多個執行緒的互動。You also might want to coordinate the interaction of multiple threads. .NET 提供可用來同步對共用資源的存取或協調執行緒互動的一系列類型。.NET provides a range of types that you can use to synchronize access to a shared resource or coordinate thread interaction. 如需詳細資訊,請參閱同步處理原始物件概觀For more information, see Overview of synchronization primitives.

請務必處理執行緒中的例外狀況。Do handle exceptions in threads. 執行緒中未處理的例外狀況一般會終止處理序。Unhandled exceptions in threads generally terminate the process. 如需詳細資訊,請參閱受控執行緒中的例外狀況For more information, see Exceptions in managed threads.

另請參閱See also