首页 > 后端开发 > C#.Net教程 > C# 中的锁定

C# 中的锁定

WBOY
发布: 2024-09-03 15:18:38
原创
846 人浏览过

C# 中的锁结构确保没有其他线程可以进入线程已经执行的代码部分。它使尝试进入代码段的其他线程等待或阻塞,直到执行线程完成其执行。在多线程编程中使用锁是一种更快、更方便的处理线程的方法。

语法

lock(object_name) statement_block
登录后复制

哪里,

  • object_name 表示必须获取锁的对象的名称。
  • statement_block 是指在特定线程上成功获取锁后将执行的代码块。

Lock 在 C# 中是如何工作的?

  • 每当线程需要在不中断任何其他线程的情况下执行一段代码时,我们都会使用锁来确保一次只有一个线程可以访问该代码段。
  • 当一个线程获取特定代码段中的锁时,它会导致尝试访问该代码段的任何其他线程等待或被阻塞。
  • 在多线程编程中使用锁是一种更快、更方便的处理线程的方式。

在 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中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板