首頁  >  文章  >  後端開發  >  C# 執行緒同步

C# 執行緒同步

WBOY
WBOY原創
2024-09-03 15:25:55298瀏覽

在指派給目前執行緒的任務完成之前,資源一次僅可供一個執行緒使用,而不會中斷任何其他線程,這種技術在 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.");
}
}
}
}

輸出:

C# 執行緒同步

說明:在上面的程式中,建立了一個名為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();
}
}

輸出:

C# 執行緒同步

說明: 在上面的程式中,創建了一個名為create 的類,在該類中定義了方法,我們使用了lock 關鍵字,這意味著操作該方法的線程會為其自身鎖定該方法,直到它完成執行而不允許其他執行緒存取該方法。這樣線程就可以同步執行。程式的輸出如上面的快照所示。

結論

在本教程中,我們透過程式設計範例及其輸出了解線程同步的定義、語法和工作原理,了解 C# 中 ThreadSynchronization 的概念。

推薦文章

這是 C# 執行緒同步指南。在這裡,我們討論 C# 執行緒同步簡介及其工作原理以及範例和程式碼實作。您也可以瀏覽我們其他推薦的文章以了解更多資訊 –

  1. C# 中的隨機數產生器
  2. Java 中的靜態建構子
  3. C# 中的 TextWriter
  4. C# 中的靜態建構子

以上是C# 執行緒同步的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:C# 線程睡眠下一篇:C# 線程睡眠