首页  >  文章  >  后端开发  >  C# 线程连接

C# 线程连接

PHPz
PHPz原创
2024-09-03 15:25:40815浏览

在 C# 中,系统线程命名空间下的线程连接类包含许多处理线程的方法。其中一种方法是 Thread.Join()。此方法用于使所有调用线程等待,直到当前线程终止或完成其任务。线程中的Join()方法可以用来实现同步,也可以用来确保特定线程已经终止。

C#提供了三种Join()方法的重载,如下:

  • 加入()
  • 加入(Int32)
  • 加入(时间跨度)

语法

Join() 方法的所有三个重载的语法如下:

public void Join();

上面的 Join() 方法不接受任何参数并阻塞调用线程,直到当前实例表示的线程完成执行或在执行标准 COM 和 SendMessage 泵送时终止。

public bool Join(int millisecondsTimeout);

上面的 Join() 方法接受一个整数作为参数,并阻塞调用线程,直到当前实例表示的线程完成执行或终止,或者在执行标准时经过使用整数参数“millisecondsTimeout”指定的时间(毫秒) COM 和 SendMessage 泵送。

public bool Join(TimeSpan timeout);

此 Join() 方法采用 TimeSpan 类型的参数并阻塞调用线程,直到当前实例表示的线程完成执行或终止,或者在执行标准 COM 和 SendMessage 泵送时使用参数“超时”指定的时间过去.

Thread Join() 方法在 C# 中如何工作?

要在 C# 中使用线程,我们需要首先导入 System.在我们的代码中使用 Threading 命名空间,以便我们可以访问 Threading 命名空间中存在的类。

Threading 命名空间下的 Thread 类包含 Join() 方法,该方法用于通过阻塞调用线程来使多个线程以同步方式工作,直到已经调用 Join() 方法的线程完成其任务并终止。如果调用Join()方法的线程没有终止,则调用线程将被无限期地阻塞。因此,join() 方法可以帮助我们确保特定线程已终止。

Join() 方法适用于处于活动状态的线程,我们可以使用 Thread.IsAlive 属性来检查这一点。如果我们在线程启动之前调用 Join() 方法,那么 Join() 方法将立即返回。同样,如果在线程已经终止时调用 Join() 方法,那么 Join() 方法也会立即返回。因此,如果线程没有运行,Join() 方法会立即返回。

我们不应该调用指示当前线程的线程对象的Join()方法。这将使我们的应用程序无响应,因为当前线程将无限期地等待自身。

请在下面的表格中找到有关 Join() 方法的三个重载的重要详细信息:

Method Parameters Returns
public void Join() It does not take any arguments. Returns void.
public bool Join(int millisecondsTimeout) An integer representing the number of milliseconds required to wait for the thread to terminate. Returns Boolean value; returns true if the thread has terminated and returns false if the time specified by the parameter has elapsed and the thread has not terminated.
public bool Join(TimeSpan timeout) A TimeSpan which indicates the amount of time required to wait for the thread to terminate. Returns Boolean value; returns true if the thread has terminated and returns false if the time specified by the parameter has elapsed and the thread has not terminated.
方法 参数 退货 public void Join() 它不需要任何参数。 返回无效。 public bool Join(int millisecondsTimeout) 一个整数,表示等待线程终止所需的毫秒数。 返回布尔值;如果线程已终止,则返回 true;如果经过参数指定的时间且线程尚未终止,则返回 false。 public bool Join(TimeSpan 超时) TimeSpan,指示等待线程终止所需的时间。 返回布尔值;如果线程已终止,则返回 true;如果经过参数指定的时间且线程尚未终止,则返回 false。 表>

Examples of C# Thread Join

Below are the examples of c# thread join:

Example #1

Example showing the use of Join() method on first thread and other two threads in the code wait until the first thread completes its execution.

Code:

using System;
using System.Threading;
namespace ConsoleApp4
{
public class Program
{
public void Display()
{
for (int i = 1; i <= 5; i++)
{
Console.WriteLine(Thread.CurrentThread.Name+" : " + i);
//suspending thread for specified time
Thread.Sleep(200);
}
}
}
public class ThreadDemo
{
public static void Main()
{
Program obj = new Program();
Thread thread1 = new Thread(new ThreadStart(obj.Display));
thread1.Name = "Thread1";
Thread thread2 = new Thread(new ThreadStart(obj.Display));
thread2.Name = "Thread2";
Thread thread3 = new Thread(new ThreadStart(obj.Display));
thread3.Name = "Thread3";
//starting thread1
thread1.Start();
//calling Join() on thread1
thread1.Join();
//starting thread2
thread2.Start();
//starting thread3
thread3.Start();
Console.ReadLine();
}
}
}

Output:

C# 线程连接

Example #2

Example calling Join() method on all threads.

Code:

using System;
using System.Threading;
namespace ConsoleApp4
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Main thread started");
Thread thread1 = new Thread(Method1);
Thread thread2 = new Thread(Method2);
Thread thread3 = new Thread(Method3);
thread1.Start();
thread2.Start();
thread3.Start();
thread1.Join();
thread2.Join();
thread3.Join();
Console.WriteLine("Main thread ended");
Console.ReadLine();
}
public static void Method1()
{
Console.WriteLine("Method1 - Thread1 starting execution");
Thread.Sleep(1000);
Console.WriteLine("Method1 - Thread1 execution completed");
}
public static void Method2()
{
Console.WriteLine("Method2 - Thread2 starting execution");
Thread.Sleep(2500);
Console.WriteLine("Method2 - Thread2 execution completed");
}
public static void Method3()
{
Console.WriteLine("Method3 - Thread3 starting execution");
Thread.Sleep(5000);
Console.WriteLine("Method3 - Thread3 execution completed");
}
}
}

Output:

C# 线程连接

Example #3

Example of Join(int millisecondsTimeout) method.

Code:

using System;
using System.Threading;
namespace ConsoleApp4
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Main thread started");
Thread thread1 = new Thread(Method1);
Thread thread2 = new Thread(Method2);
Thread thread3 = new Thread(Method3);
thread1.Start();
thread2.Start();
thread3.Start();
if (thread3.Join(2000))
{
Console.WriteLine("Execution of thread3 completed in 2 seconds.");
}
else
{
Console.WriteLine("Execution of thread3 not completed in 2 seconds.");
}
Console.WriteLine("Main thread ended");
Console.ReadLine();
}
public static void Method1()
{
Console.WriteLine("Method1 - Thread1 execution started");
Thread.Sleep(1000);
Console.WriteLine("Method1 - Thread1 execution completed");
}
public static void Method2()
{
Console.WriteLine("Method2 - Thread2 execution started");
Thread.Sleep(2500);
Console.WriteLine("Method2 - Thread2 execution completed");
}
public static void Method3()
{
Console.WriteLine("Method3 - Thread3 execution started");
Thread.Sleep(5000);
Console.WriteLine("Method3 - Thread3 execution completed");
}
}
}

Output:

C# 线程连接

Conclusion

Join() method in C# makes one thread wait till another thread completes its task and terminates. There are three overloads of the Join() method in C#. Join() method works on the thread which is in the alive state. It helps to achieve synchronization while working with multiple threads.

以上是C# 线程连接的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:C# Thread下一篇:C# Thread Sleep