ホームページ  >  記事  >  バックエンド開発  >  C# をロックインする

C# をロックインする

WBOY
WBOYオリジナル
2024-09-03 15:18:38681ブラウズ

C# のロック構造により、スレッドがすでに実行されているコードのセクションに他のスレッドが入ることができなくなります。これにより、コード セクションに入ろうとする他のスレッドは、実行中のスレッドが実行を完了するまで待機またはブロックされます。ロックを使用すると、マルチスレッド プログラミングでスレッドを処理するためのより高速かつ便利な方法になります。

構文

lock(object_name) statement_block

どこ、

  • object_name は、ロックを取得する必要があるオブジェクトの名前を表します。
  • statement_block は、特定のスレッドでロックが正常に取得された後に実行されるコードのブロックを指します。

C# ではロックはどのように機能しますか?

  • 他のスレッドを中断することなくコードのセクションでスレッドを実行する必要がある場合は常に、ロックを使用して、一度に 1 つのスレッドだけがそのコードのセクションにアクセスできるようにします。
  • スレッドがコードの特定のセクションでロックを取得すると、コードのそのセクションにアクセスしようとする他のスレッドは待機するかブロックされます。
  • ロックの使用は、マルチスレッド プログラミングでスレッドを処理するためのより高速かつ便利な方法です。

C# でロックを実装する例

以下の例を見つけてください。

例 #1

コードのクリティカル セクションでスレッドが既に実行されているときに、別のスレッドの実行をブロックするロックを示す C# プログラム:

コード:

using System;
using System.Threading;
//a namespace called program is defined
namespace program
{
//a class called check is defined
class check
{
//an object that defines a lock is created
static readonly object lockname = new object();
//a method called display is created in which the lock is used to make any other threads trying to access the method wait or block until thread that is already executing completes its execution
static void display()
{
//keyword lock is used to lock the object
lock (lockname)
{
for (int a = 1; a <= 3; a++)
{
//the output is displayed synchronously in a row by one thread followed by another thread because we have used lock on display method
Console.WriteLine("The value to be printed is: {0}", a);
}
}
}
static void Main(string[] args)
{
//an instance of the thread is created and the corresponding thread is executed on the display method
Thread firstthread = new Thread(display);
//an instance of the thread is created and the corresponding thread is executed on the display method
Thread secondthread = new Thread(display);
firstthread.Start();
secondthread.Start();
Console.ReadLine();
}
}
}

出力:

C# をロックインする

説明: 上記のプログラムでは、プログラムは「program」という名前空間を定義しています。次に、「check」という名前のクラスを定義します。このメソッドはロックを利用して、現在実行中のスレッドが実行を終了するまで、そのロックにアクセスしようとする他のスレッドを待機またはブロックさせます。上のスナップショットは出力を示しています。

例 #2

コードのクリティカル セクションでスレッドが既に実行されているときに、別のスレッドの実行をブロックするロックを示す C# プログラム:

コード:

using System;
using System.Threading;
//a namespace called program is defined
namespace program
{
//a class called check is defined
class check
{
//an object that defines a lock is created
static readonly object lockname = new object();
//a method called display is created in which the lock is used to make any other threads trying to access the method wait or block until thread that is already executing completes its execution
static void display()
{
//keyword lock is used to lock the object
lock (lockname)
{
for (int a = 1; a <= 3; a++)
{
//the output is displayed synchronously in a row by one thread followed by another thread because we have used lock on display method
Console.WriteLine("The first three lines are printed by first thread and the second three lines are printed by the second thread");
}
}
}
static void Main(string[] args)
{
//an instance of the thread is created and the corresponding thread is executed on the display method
Thread firstthread = new Thread(display);
//an instance of the thread is created and the corresponding thread is executed on the display method
Thread secondthread = new Thread(display);
firstthread.Start();
secondthread.Start();
Console.ReadLine();
}
}
}

出力:

C# をロックインする

説明: プログラムは、「program」という名前空間を定義し、次に「check」というクラスを定義します。ロックを表すオブジェクトを作成します。キーワード「lock」を使用して、以前に作成したオブジェクトをロックします。

結論

このチュートリアルでは、プログラミング例とその出力を通じてロックの定義、構文、動作を通じてロックイン C# の概念を理解します。

以上がC# をロックインするの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
前の記事:C# ソープ次の記事:C# ソープ