一:线程的概念
线程:
被定义为程序执行的路径,每个线程都定义了一个独特的控制流。如果应用程序涉及到复杂和好事的操作,可以通过开辟
不同的线程来执行复杂或者耗时的任务
进程和线程?
每一个应用程序在运行期间就是一个进程。每个进程可以有多个线程,每个进程默认都有一个主线程。所有的代码默认都是在主线程中进行执行的,使用线程可以提高程序执行的效率
线程的生命周期中的各种状态?
1.未启用状态:线程创建好了,但是没有调用start方法进行开启
2.就绪状态:线程准备好了等待CPU周期时的状态
3.不可运行状态:(调用了sleep方法;调用了wait方法,提高I/O操作阻塞)
4.死亡状态:当线程已经完成执行或者终止时的状态
线程在C#使用System.Threading.Thread 类进行创建,程序在执行的时候,主线程默认会被创建
例如:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _1线程的概念
{
internal static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Thread th = Thread.CurrentThread; //获取当前线程
th.Name = "主线程";// 赋值的
Console.WriteLine("当前线程是"+th.Name); // 获取当前线程的名称
//演示费时的操作
// Console.WriteLine(FeiBo(45)); // 如果把费时操作放在主线程里面执行,阻塞主线程,后续代码不会执行
//直到费时操作执行完之后,
// 开辟分线程
//new ThreadStart() 参数是回调函数。在参数这个函数中执行的代码就是在分线程执行的
ThreadStart th2 = new ThreadStart(FeiShi);
Thread th3 = new Thread(th2); //把ThreadStart类型强转成thread类型
th3.Start(); //开启
Console.WriteLine("11111");
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
static void FeiShi()
{
Console.WriteLine(FeiBo(10));
}
// 计算斐波那契数列 1 1 2 3 5 8 13 21...
// 计算第n位的数,等于前一位加上前俩位的数
// 使用递归计算
static int FeiBo(int n)
{
if (n <= 2)
{
return 1;
}
return FeiBo(n-1)+FeiBo(n-2);
}
}
}
二:线程的休眠和终止
start开启线程
创建对象,参数是线程函数

sleep休眠线程
Sleep休眠参数是以ms为单位,2000ms=2s
Abort终止线程

创建两个线程,两个线程对应的函数是同一个函数,那么线程函数打印的顺序是否是一定的?
这个程序打印顺序是任何顺序的,线程之间有上下文切换,所以每一次执行的结果不一样
线程顺序的例子
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _3线程顺序的例子
{
internal static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
public static int A = 10;
[STAThread]
static void Main()
{
// 多个线程,执行的不同线程函数,问执行线程函数的顺序? 顺序不一定
Thread t1 = new Thread(new ThreadStart(MyThread.test1));
Thread t2 = new Thread(new ThreadStart(MyThread.test2));
t1.Start();
t2.Start();
Console.WriteLine(A);
Thread t3 = new Thread(new ThreadStart(MyThread.Test3));
int a = 1;
for (int i = 0; i <= 10000; i++)
{
while (a > 0)
{
if (a % 3 == 2 && a % 5 == 4 && a % 7 == 6 && a % 9 == 8 && a % 11 == 0)
{
Console.WriteLine("第" + i + "满足的人数:" + a);
a++;
break;
}
a++;
}
}
// 3人一张桌子 余2个人;
// 5人一张桌子 余4个人
// 7人一张桌子 余6个人
// 9人一张桌子 余8个人
// 11人一张桌子 正好坐满
// 要求把前100个数打印出来
// 先不要线程去写
// 写到线程里面
// 观察窗体出现快慢
Thread t2 = new Thread(new ThreadStart(MyThread.test3));
t2.Start();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
public class MyThread
{
public static void test3()
{
int a = 1;
for (int i = 0; i <= 10000; i++)
{
while (a > 0)
{
if (a % 3 == 2 && a % 5 == 4 && a % 7 == 6 && a % 9 == 8 && a % 11 == 0)
{
Console.WriteLine("第" + i + "满足的人数:" + a);
a++;
break;
}
a++;
}
}
}
public static void test1()
{
Program.A++;
for (int i = 0; i < 10; i++)
{
Console.WriteLine("test1"+":"+i);
}
}
public static void test2()
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("test2" + ":" + i);
}
}
}
}
🆗///有什么不懂的可以私信我哦///
本文详细介绍了C#中的线程概念,包括线程定义、进程与线程的关系,以及线程生命周期中的不同状态。通过示例展示了如何使用Thread类创建线程、线程的休眠与终止,以及线程顺序的不确定性。
1838

被折叠的 条评论
为什么被折叠?



