C# 執行緒同步
在指派給目前執行緒的任務完成之前,資源一次僅可供一個執行緒使用,而不會中斷任何其他線程,這種技術在 C# 中稱為同步。實際上,在多執行緒程式中,執行緒可以在所需的時間內存取任何資源,並且資源由執行緒非同步共享和執行,這是一項關鍵任務,可能會導致系統停止,因此執行緒必須同步執行通過執行緒的同步,我們可以保持執行緒的一致性,確保一個執行緒執行過程中不會有其他執行緒幹擾。
C# 執行緒同步語法
下面是C#線程同步的語法如下:
Thread thread_name = new Thread(method_name); thread_name.Start(); thread_name.Join();
或
Thread thread_name = new Thread(method_name); thread_name.Start(); method_name() { lock(this) { //thread_name thread is executed } }
其中 thread_name 是執行緒的名稱,method_name 是從呼叫 thread_name.Start() 時開始由該執行緒單獨存取的方法的名稱,thread_name.Join() 等待直到該執行緒完成停止所有其他執行緒的執行。
方法中的Lock關鍵字,method_name鎖定執行緒執行,以便在當前執行緒完成之前沒有其他執行緒可以存取該方法。
C# 執行緒同步的函數
- 在多執行緒程式中,執行緒可以在所需的時間內存取任何資源,但如果多個執行緒嘗試存取相同資源,則多個執行緒同時或非同步共享資源將成為一項關鍵任務,並且系統可能會停止執行。
- 為了解決這個問題,執行緒同步是必要的。透過執行緒同步,只有該特定執行緒可以在一定時間內存取資源,而不會受到其他執行緒的任何中斷。
- 執行緒的同步可以使用 join 關鍵字和 lock 關鍵字來完成。
- 當在執行緒上使用 join 關鍵字時,允許該執行緒完成其執行,而不會中斷任何其他執行緒。
- 當使用lock關鍵字時,執行緒正在執行的資源將被鎖定一段時間,直到執行緒完成執行。
實作 C# 執行緒同步的範例
下面是C#執行緒同步的範例:
範例#1
使用 join 關鍵字示範執行緒同步的 C# 程式。
代碼:
using System; using System.Threading; //a namespace called program is created namespace program { //a class called check is defined class check { //main method is called static void Main(string[] args) { //an instance of the thread class is created which operates on a method Thread firstthread = new Thread(secondfunction); //start method is used to begin the execution of the thread firstthread.Start(); //join method stops all other threads while the current thread is executing firstthread.Join(); Thread secondthread = new Thread(firstfunction); secondthread.Start(); secondthread.Join(); } private static void firstfunction(object obj) { for(inti=1;i<3;i++) { Console.WriteLine("First function is executed two times in a row because join method is called on the second thread operating on this method."); } } private static void secondfunction(object obj) { for(inti=1;i<3;i++) { Console.WriteLine("Second function is executed two times in a row because join method is called on the first thread operating on this method."); } } } }
輸出:
說明:在上面的程式中,建立了一個名為program的命名空間。然後定義一個名為 check 的類,在該類別中呼叫 main 方法。然後建立一個執行緒實例來操作一個方法,該方法使用 Start() 方法開始,並在同一執行緒上使用 join() 方法,以確保其執行不會被其他執行緒中斷。因此,輸出同步顯示在一行中。程式的輸出如上面的快照所示。
範例#2
使用 lock 關鍵字來示範執行緒同步的 C# 程式。
代碼:
using System; using System.Threading; //a class called create is created class create { public void func() { //lock is called on this method lock (this) { for (inti = 1; i<= 2; i++) { Console.WriteLine("The thread is executing"); } } } } class check { public static void Main(string[] args) { //an instance of the create class is created create c = new create(); //an instance of the thread class is created which operates on the method in another class Thread firstthread = new Thread(c.func); firstthread.Start(); Thread secondthread = new Thread(c.func); secondthread.Start(); } }
輸出:
說明: 在上面的程式中,創建了一個名為create 的類,在該類中定義了方法,我們使用了lock 關鍵字,這意味著操作該方法的線程會為其自身鎖定該方法,直到它完成執行而不允許其他執行緒存取該方法。這樣線程就可以同步執行。程式的輸出如上面的快照所示。
結論
在本教程中,我們透過程式設計範例及其輸出了解線程同步的定義、語法和工作原理,了解 C# 中 ThreadSynchronization 的概念。
推薦文章
這是 C# 執行緒同步指南。在這裡,我們討論 C# 執行緒同步簡介及其工作原理以及範例和程式碼實作。您也可以瀏覽我們其他推薦的文章以了解更多資訊 –
- C# 中的隨機數產生器
- Java 中的靜態建構子
- C# 中的 TextWriter
- C# 中的靜態建構子
以上是C# 執行緒同步的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

使用 C# 的 Active Directory 指南。在這裡,我們討論 Active Directory 在 C# 中的介紹和工作原理以及語法和範例。

多線程和異步的區別在於,多線程同時執行多個線程,而異步在不阻塞當前線程的情況下執行操作。多線程用於計算密集型任務,而異步用於用戶交互操作。多線程的優勢是提高計算性能,異步的優勢是不阻塞 UI 線程。選擇多線程還是異步取決於任務性質:計算密集型任務使用多線程,與外部資源交互且需要保持 UI 響應的任務使用異步。
