Rumah >pembangunan bahagian belakang >Tutorial C#.Net >Kunci dalam C#
Pembinaan kunci dalam C# memastikan tiada utas lain boleh memasuki bahagian kod yang mana utas sudah dilaksanakan. Ia membuatkan utas lain yang cuba memasuki bahagian kod menunggu atau menyekat sehingga utas pelaksana menyelesaikan pelaksanaannya. Menggunakan kunci ialah cara yang lebih pantas dan mudah untuk mengendalikan benang dalam pengaturcaraan multithreading.
Sintaks
lock(object_name) statement_block
Di mana,
Cari contoh di bawah.
Atur cara C# untuk menunjukkan kunci untuk menyekat pelaksanaan utas lain semasa utas sudah dilaksanakan dalam bahagian kritikal kod:
Kod:
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(); } } }
Output:
Penjelasan: Dalam atur cara di atas, atur cara mentakrifkan ruang nama yang dipanggil "program". Ia kemudian mentakrifkan kelas bernama "semak." Kaedah ini menggunakan kunci untuk membuat sebarang utas lain yang cuba mengaksesnya menunggu atau menyekat sehingga utas yang sedang melaksanakan selesai pelaksanaannya. Gambar di atas memaparkan output.
Atur cara C# untuk menunjukkan kunci untuk menyekat pelaksanaan utas lain semasa utas sudah dilaksanakan dalam bahagian kritikal kod:
Kod:
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(); } } }
Output:
Penjelasan: Atur cara mentakrifkan ruang nama yang dipanggil "program" dan kemudian mentakrifkan kelas yang dipanggil "semak." Ia mencipta objek yang mewakili kunci. Ia menggunakan kata kunci "kunci" untuk mengunci objek yang dibuat sebelum ini.
Dalam tutorial ini, kami memahami konsep kunci masuk C# melalui definisi, sintaks dan kerja kunci melalui contoh pengaturcaraan dan outputnya.
Atas ialah kandungan terperinci Kunci dalam C#. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!