C# 线程

王林
发布: 2024-09-03 15:25:25
原创
284 人浏览过

以下文章提供了 C# 线程的概述。程序的执行路径被定义为线程,并且每个线程都定义了唯一的控制流。当应用程序包含复杂且耗时的操作时,必须设置不同的执行路径或线程,其中每个线程负责特定的作业。这些轻量级的线程进程和实现并发编程的现代操作系统是使用线程的示例之一,通过使用线程,可以节省中央处理单元的周期浪费并提高应用程序的效率。

语法:

public sealed class Thread: System.Runtime.ConstrainedExecution.CriticalFinalizerObject
登录后复制

C# 线程类的工作

当线程的生命周期开始时,System.Threading.Thread类的对象被创建的时间。当线程终止或线程执行完成时,线程被创建并结束。

线程的生命周期有几种状态。

1。未启动状态:这种状态是指未调用start方法但创建了线程实例的情况。

2。就绪状态:该状态是线程已准备好运行并等待中央处理器周期的情况。

3。不可运行状态:此状态是线程在以下情况下无法执行的情况:

  • 已调用 Sleep 方法。
  • 已调用 Wait 方法。
  • 输入/输出操作出现阻塞。

4。死亡状态:该状态是线程执行完成,或者线程执行中止的情况。

  • 为了在 C# 中使用线程,我们必须使用 System.Threading.Thread 类。
  • 在使用多线程应用程序时,可以使用 C# 中的 System.Threading.Thread 类创建和访问单个线程。
  • 整个进程中最先执行的线程称为主线程。
  • 主线程在C#程序开始执行时自动创建。
  • 使用 Thread 类创建的线程称为主线程的子线程。
  • Thread 类的 CurrentThread 属性用于访问线程。

下面的程序演示了主线程的执行:

代码:

using System;
using System.Threading;
//a namespace called multithreading is created
namespace Multithreading
{
//a class called mainthread is created under multithreading namespace
class MainThread
{
//main method is called
static void Main(string[] args)
{
//an instance of the thread class is created
Thread thr = Thread.CurrentThread;
//Name method of thread class is accessed using the instance of the thread class
thr.Name = "Thread Class";
//the content is displayed as the output
Console.WriteLine("Welcome to {0}", thr.Name);
Console.ReadKey();
}
}
}
登录后复制

输出:

C# 线程

在上面的程序中,创建了一个名为多线程的命名空间。然后在多线程命名空间下创建一个名为mainthread的类。然后调用一个main方法。然后创建线程类的实例。然后使用线程类的实例访问线程类的Name方法。最后输出显示在屏幕上。

Thread 类的方法

下面是线程类的几个方法:

1.中止()

每当在线程上调用 Abort() 方法时,都会引发 ThreadAbortException 并开始终止线程的过程。调用该方法会导致线程终止。

示例: 

代码:

using System;
using System.Threading;
class ExThread
{
public void thr()
{
for (int y = 0; y < 3; y++)
{
Console.WriteLine(y);
}
}
}
class Example
{
public static void Main()
{
ExThread ob = new ExThread();
Thread th = new Thread(new ThreadStart(ob.thr));
th.Start();
Console.WriteLine("Aborting the thread");
th.Abort();
}
}
登录后复制

输出:

C# 线程

2.中断()

每当调用 Interrupt() 方法时,处于 WaitSleepJoin 线程状态的线程就会被中断。

3.加入()

每当调用 Join() 方法时,调用线程都会被阻塞,直到线程终止,并且标准 COM 和 SendMessage 泵送将在线程阻塞的同时继续执行。

实现 Interrupt() 和 Join() 的示例:

代码:

using System;
using System.Threading;
class Thr
{
Thread th;
public Thr(String name1)
{
th = new Thread(this.Runaway);
th.Name = name1;
th.Start();
}
public void Runaway()
{
Thread th1 = Thread.CurrentThread;
try
{
Console.WriteLine(" Execution of " + th1.Name + " has begun");
for(int y=0; y<3; y++)
{
Console.WriteLine(" Printing of " + th1.Name + " has begun" + y);
Thread.Sleep(200);
}
Console.WriteLine(" Execution of " + th1.Name + " is finished");
}
catch(ThreadInterruptedException e)
{
Console.WriteLine("Thread Interruption" + e);
}
}
public static void Main(String[] ar)
{
Thr ob = new Thr("Thread demo");
ob.th.Interrupt();
ob.th.Join();
}
}
登录后复制

输出:

C# 线程

4. ResetAbort()

每当调用 ResetAbort() 方法时,当前线程的终止请求就会被取消。

示例:

代码:

using System;
using System.Threading;
using System.Security.Permissions;
class Thread1
{
public void Jobthread()
{
try
{
for (int r = 0; r < 3; r++)
{
Console.WriteLine(" Working of thread has begun ");
Thread.Sleep(10);
}
}
catch (ThreadAbortException e)
{
Console.WriteLine("ThreadAbortException is caught and must be reset");
Console.WriteLine("The message looks like this: {0}", e.Message);
Thread.ResetAbort();
}
Console.WriteLine("Thread is working fine");
Thread.Sleep(200);
Console.WriteLine("Thread is done");
}
}
class Driver
{
public static void Main()
{
Thread1 obj = new Thread1();
Thread Th = new Thread(obj.Jobthread);
Th.Start();
Thread.Sleep(100);
Console.WriteLine("thread abort");
Th.Abort();
Th.Join();
Console.WriteLine("end of main thread");
}
}
登录后复制

输出:

C# 线程

5.开始()

每当调用 Start() 方法时,都会启动一个线程。

示例:

代码:

using System;
using System.Threading;
class Test
{
static void Main()
{
Thread td = new Thread (new ThreadStart (Got));
td.Start();
}
static void Got()
{
Console.WriteLine ("this is thread Start() method");
}
}
登录后复制

输出:

C# 线程

6.睡眠(int毫秒超时)

每当调用 Sleep(int millisecondsTimeout) 方法时,线程就会暂停指定的时间。

示例:

代码:

using System;
using System.Threading;
namespace Examplethr
{
class MyThread
{
static void Main(string[] args)
{
Thread th = Thread.CurrentThread;
th.Name = "This is the First Thread";
Console.WriteLine("The Name of the thread is : {0}", th.Name);
Console.WriteLine("The priority of the thread is : {0}", th.Priority);
Console.WriteLine("Pausing the child thread");
// using Sleep() method
Thread.Sleep(100);
Console.WriteLine("Resuming the child thread");
Console.ReadKey();
}
}
}
登录后复制

输出:

C# 线程

7. Suspend()

Whenever Suspend() method is called, the current thread is suspended if it is not suspended.

8. Resume()

Whenever Resume() method is called, the suspended thread is resumed.

9. Yield()

Whenever Yield() method is called, the calling thread must result in execution to the other thread which is ready to start running on the current processor. The thread to yield to is selected by the operating system.

Example to implement Suspend() Resume() and Yield()

Code:

using System;
using System.Runtime.CompilerServices;
using System.Threading;
class Program
{
public static void Main ()
{
bool finish = false;
Thread th = new Thread (() => {
while (!finish) {}
});
Thread th1 = new Thread (() => {
while (!finish) {
GC.Collect ();
Thread.Yield ();
}
});
th.Start ();
th1.Start ();
Thread.Sleep (10);
for (int j = 0; j < 5 * 4 * 2; ++j) {
th.Suspend ();
Thread.Yield ();
th.Resume ();
if ((j + 1) % (5) == 0)
Console.Write ("Hello ");
if ((j + 1) % (5 * 4) == 0)
Console.WriteLine ();
}
finish = true;
th.Join ();
th1.Join ();
}
}
登录后复制

Output:

C# 线程

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

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!