1、Thread相关基本操作
private void ThreadBasicOperation()
{
ThreadStart threadStart = () =>
{
Thread.Sleep(3000);
this.DoSomethingLong("btnThreadClick ThreadStart");
Thread.Sleep(3000);
};
Thread thread = new Thread(threadStart);
thread.Start();
ParameterizedThreadStart parameterizedThreadStart = s =>
{
this.DoSomethingLong($"btnThreadClick ParameterizedThreadStart {s}");
};
Thread threadPar = new Thread(parameterizedThreadStart);
threadPar.Start("牧舟");
}
![多线程基本操作](https://img-blog.csdnimg.cn/aa9cc5ee740a4998a12c05068393bb23.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjI0ODQ0Nw==,size_16,color_FFFFFF,t_70)
2、控制线程执行顺序
private void ControlThreadOrder01()
{
[什么是回调?](https://blog.csdn.net/zjpp2580369/article/details/83027547)
ThreadStart threadStart = () =>
{
Thread.Sleep(2000);
this.DoSomethingLong("扩展封装,回调委托");
Thread.Sleep(2000);
};
Action actionCallBack = () =>
{
Console.WriteLine("好好学习,天天向上!");
};
this.ThreadWithCallBack(threadStart, actionCallBack);
}
private void ThreadWithCallBack(ThreadStart threadStart,Action actionCallBack)
{
ThreadStart method = () =>
{
actionCallBack.Invoke();
threadStart.Invoke();
};
Thread thread = new Thread(method);
thread.Start();
}
![扩展封装控制线程执行顺序](https://img-blog.csdnimg.cn/61263b5446fe406fabf0eb33aa5034d1.png)
3、获取异步结果,不卡顿
private void ControlThreadOrder01WithReturn()
{
Func<int> func = () =>
{
Thread.Sleep(3000);
return DateTime.Now.Year;
};
Func<int> funcCallBack = this.ThreadWithReturnFunc<int>(func);
int year = funcCallBack.Invoke();
Console.WriteLine($"今年是 {year} 年……");
}
private T ThreadWithReturnT<T>(Func<T> func)
{
T t = default(T);
ThreadStart threadStart = () =>
{
t = func.Invoke();
};
Thread thread = new Thread(threadStart);
thread.Start();
thread.Join();
return t;
}
private Func<T> ThreadWithReturnFunc<T>(Func<T> func)
{
T t = default(T);
ThreadStart threadStart = () =>
{
t = func.Invoke();
};
Thread thread = new Thread(threadStart);
thread.Start();
return new Func<T>(() =>
{
thread.Join();
return t;
});
}
4、线程池分配线程
private void AllocationThread()
{
WaitCallback waitCallback = o =>
{
Console.WriteLine($"参数:{o}");
this.DoSomethingLong("AllocationThread");
};
ThreadPool.QueueUserWorkItem(waitCallback);
ThreadPool.QueueUserWorkItem(waitCallback, "好好学习,天天向上!");
}
![线程池分配一个线程](https://img-blog.csdnimg.cn/0ddac4882b8b4e55b401afde132572d3.png)
5、线程池获取和设置线程数
private void SetTheNumberOfThreads()
{
Console.WriteLine("================获取线程池中最大、最小线程数================");
ThreadPool.GetMinThreads(out int minworkerThreads, out int mincompletionPortThreads);
Console.WriteLine($"Min this workerThreads={minworkerThreads}, this completionPortThreads={mincompletionPortThreads}");
ThreadPool.GetMaxThreads(out int maxworkerThreads, out int maxcompletionPortThreads);
Console.WriteLine($"Min this workerThreads={minworkerThreads}, this completionPortThreads={mincompletionPortThreads}");
Console.WriteLine("================设置线程池中线程的数量================");
ThreadPool.SetMinThreads(8, 8);
ThreadPool.SetMaxThreads(64, 64);
ThreadPool.GetMinThreads(out int defminworkerThreads, out int defmincompletionPortThreads);
Console.WriteLine($"Min this defminworkerThreads={defminworkerThreads} this defmincompletionPortThreads={defmincompletionPortThreads}");
ThreadPool.GetMaxThreads(out int defmaxworkerThreads, out int defmaxcompletionPortThreads);
Console.WriteLine($"Max this defmaxworkerThreads={defmaxworkerThreads} this defmaxcompletionPortThreads={defmaxcompletionPortThreads}");
}
6、观望式线程等待
private void WaitAndSeeThreadWaiting()
{
ManualResetEvent mre = new ManualResetEvent(false);
ThreadPool.QueueUserWorkItem(o =>
{
Console.WriteLine($"参数o:{o}");
this.DoSomethingLong("牧舟=牧舟=牧舟");
mre.Set();
});
Console.WriteLine("mre.WaitOne之前的代码……");
mre.WaitOne();
Console.WriteLine("mre.WaitOne之后的代码……");
Console.WriteLine("任务全部完成了");
}
![观望式线程等待](https://img-blog.csdnimg.cn/aa8a14ac69324963a29b002b7ff58f7b.png)
7、线程死锁
private void ThreadDeadLock()
{
ThreadPool.SetMaxThreads(4, 4);
ManualResetEvent mre = new ManualResetEvent(false);
for(int i = 0; i < 10; i++)
{
int k = i;
ThreadPool.QueueUserWorkItem(t =>
{
Console.WriteLine($"ThreadId = {Thread.CurrentThread.ManagedThreadId.ToString("00")}");
if (k == 9)
{
mre.Set();
}
else
{
mre.WaitOne();
}
});
}
if (mre.WaitOne())
{
Console.WriteLine("所有任务执行完成……");
}
}
![线程死锁](https://img-blog.csdnimg.cn/80b417c748c746fcbc64912a68281a42.png)