📜  如何在C#中创建线程

📅  最后修改于: 2021-05-30 00:38:28             🧑  作者: Mango

在C#中,多线程系统基于Thread类构建,该类封装了线程的执行。此类包含一些有助于管理和创建线程的方法和属性,并且该类在System.Threading命名空间下定义。 System.Threading命名空间提供在多线程编程中使用的类和接口。

此命名空间中一些常用的类是:

Class Name Description
Mutex It is a synchronization primitive that can also be used for IPS (interprocess synchronization).
Monitor This class provides a mechanism that access objects in synchronize manner.
Semaphore This class is used to limit the number of threads that can access a resource or pool of resources concurrently.
Thread This class is used to creates and controls a thread, sets its priority, and gets its status.
ThreadPool This class provides a pool of threads that can be used to execute tasks, post work items, process asynchronous I/O, wait on behalf of other threads, and process timers.
ThreadLocal This class provides thread-local storage of data.
Timer This class provides a mechanism for executing a method on a thread pool thread at specified intervals. You are not allowed to inherit this class.
Volatile This class contains methods for performing volatile memory operations.

在C#程序中创建线程的步骤:

  1. 首先导入System.Threading命名空间,它在您的程序中创建线程中起着重要作用,因为您无需每次都编写类的完全限定名称。
    Using System;
    Using System.Threading
    
  2. 现在,在您的main方法中创建并初始化线程对象。
    public static void main()
    {
        Thread thr = new Thread(job1);
    }
    

    或者

    您还可以使用ThreadStart构造函数来初始化新实例。

    public static void main()
    {
         Thread thr = new Thread(new ThreadStart(job1));
    }
    
  3. 现在,您可以调用线程对象了。
    public static void main()
    {
        Thread thr = new Thread(job1);
        thr.Start();
    }
    

下面的程序说明了上述步骤的实际实现:

范例1:

// C# program to illustrate the
// creation of thread using
// non-static method
using System;
using System.Threading;
  
public class ExThread {
  
    // Non-static method
    public void mythread1()
    {
        for (int z = 0; z < 3; z++) {
            Console.WriteLine("First Thread");
        }
    }
}
  
// Driver Class
public class GFG {
  
    // Main Method
    public static void Main()
    {
        // Creating object of ExThread class
        ExThread obj = new ExThread();
  
        // Creating thread
        // Using thread class
        Thread thr = new Thread(new ThreadStart(obj.mythread1));
        thr.Start();
    }
}

输出:

First Thread
First Thread
First Thread

说明:在上面的示例中,我们有一个名为ExThread的类,其中包含一个名为mythread1()的非静态方法。因此,我们创建了一个实例,即ExThread类的obj ,并按照此语句中的说明将其引用到ThreadStart Thread a = new Thread(new ThreadStart(obj.mythread1)); 。使用T hread a = new Thread(new ThreadStart(obj.mythread1));语句,我们将创建一个名为thr的线程并初始化该线程的工作。通过使用thr.Start();陈述。

范例2:

// C# program to illustrate the creation
// of thread using static method
using System;
using System.Threading;
  
public class ExThread {
  
    // Static method for thread a
    public static void thread1()
    {
        for (int z = 0; z < 5; z++) {
            Console.WriteLine(z);
        }
    }
  
    // static method for thread b
    public static void thread2()
    {
        for (int z = 0; z < 5; z++) {
            Console.WriteLine(z);
        }
    }
}
  
// Driver Class
public class GFG {
  
    // Main method
    public static void Main()
    {
        // Creating and initializing threads
        Thread a = new Thread(ExThread.thread1);
        Thread b = new Thread(ExThread.thread2);
        a.Start();
        b.Start();
    }
}

输出 :

0
1
2
3
4
0
1
2
3
4

说明:在上面的示例中,我们有一个名为ExThread的类,并且包含两个名为thread1()thread2()的静态方法。因此,我们不需要创建ExThread类的实例。在这里,我们使用类名(例如ExThread.thread1ExThread.thread2来调用这些方法。通过使用Thread a = new Thread(ExThread.thread1);语句我们创建并初始化线程a的工作,与线程b相似。通过使用a.Start();b.Start();语句,计划执行的ab线程。

注意:由于上下文切换,这些程序的输出可能会有所不同。