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 响应的任务使用异步。
