0. 首先定义一个函数用于给线程用
private void TestThread(String threadName)
{
Console.WriteLine("******************TestThread start {0} 当前线程ID={1} 当前时间为 {2}********************",
threadName, Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToString("HHmmss:fff"));
long lResult = 0;
for (int i = 0; i < 10000000; i++)
{
lResult += i;
}
Console.WriteLine("******************TestThread end {0}:{1} 当前线程ID={2} 当前时间为 {3}********************",
threadName, lResult, Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToString("HHmmss:fff"));
}
1. 增加一个按钮,用于点击后执行 .net1.1版本的方法
/// <summary>
/// .net1.1版本
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void net1_1_Click(object sender, EventArgs e)
{
Stopwatch sw = new Stopwatch();
sw.Start();
Console.WriteLine("******************net1_1_Click start {0}********************", Thread.CurrentThread.ManagedThreadId);
List<Thread> listThread = new List<Thread>();
ThreadStart threadStart = () => TestThread("threads");
for (int i = 0; i < 5; i++)
{
Thread thread = new Thread(threadStart);
listThread.Add(thread);
thread.Start();
}
listThread.ForEach(t => t.Join());
Console.WriteLine("******************net1_1_Click end {0}********************", Thread.CurrentThread.ManagedThreadId);
sw.Stop();
Console.WriteLine("总耗时为{0}",sw.ElapsedMilliseconds);
}
2. 增加一个按钮,用于点击后执行.net 2.0的版本的方法
/// <summary>
/// .net 2.0 出现了线程池
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void net2_0_Click(object sender, EventArgs e)
{
Stopwatch sw = new Stopwatch();
sw.Start();
Console.WriteLine("******************net2_0_Click start {0}********************", Thread.CurrentThread.ManagedThreadId);
//ThreadPool.QueueUserWorkItem(t => TestThread("thread pool1"));
//ThreadPool.QueueUserWorkItem(t => TestThread(t.ToString()), "thread pool2");
//ThreadPool.QueueUserWorkItem(t => TestThread(t.ToString()), "thread pool3");
//ThreadPool.QueueUserWorkItem(t => TestThread(t.ToString()), "thread pool4");
//ThreadPool.QueueUserWorkItem(t => TestThread(t.ToString()), "thread pool5");
using (ManualResetEvent m1 = new ManualResetEvent(false))//false 表示不初始化
using (ManualResetEvent m2 = new ManualResetEvent(false))//false 表示不初始化
using (ManualResetEvent m3 = new ManualResetEvent(false))//false 表示不初始化
using (ManualResetEvent m4 = new ManualResetEvent(false))//false 表示不初始化
using (ManualResetEvent m5 = new ManualResetEvent(false))//false 表示不初始化
{
ThreadPool.QueueUserWorkItem(t =>
{
TestThread("thread pool1");
m1.Set();//当m1执行结束的时候通知一下
});
ThreadPool.QueueUserWorkItem(
(t =>
{
TestThread(t.ToString());
m2.Set();
}), "thread pool2");
ThreadPool.QueueUserWorkItem(
(t =>
{
TestThread(t.ToString());
m3.Set();
}), "thread pool3");
ThreadPool.QueueUserWorkItem(
(t =>
{
TestThread(t.ToString());
m4.Set();
}), "thread pool4");
ThreadPool.QueueUserWorkItem(
(t =>
{
TestThread(t.ToString());
m5.Set();
}), "thread pool5");
m1.WaitOne();
m2.WaitOne();
m3.WaitOne();
m4.WaitOne();
m5.WaitOne();
}
Console.WriteLine("******************net2_0_Click end {0}********************", Thread.CurrentThread.ManagedThreadId);
sw.Stop();
Console.WriteLine("总耗时为{0}", sw.ElapsedMilliseconds);
}
3. 增加一个按钮,用于点击后执行.net 2.0的版本的方法
/// <summary>
/// .net 3.5以后出现了task
/// task的本质是基于线程池的
/// 只是api被强化
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void net3_5_Click(object sender, EventArgs e)
{
Stopwatch sw = new Stopwatch();
sw.Start();
Console.WriteLine("******************net3_5_Click start {0}********************", Thread.CurrentThread.ManagedThreadId);
//TaskFactory taskFactory = new TaskFactory();
//for(int i = 0; i < 5; i++)
//{
// taskFactory.StartNew(() => TestThread("Task"));
//}
TaskFactory taskFactory = new TaskFactory();
List<Task> listTask = new List<Task>();
for (int i = 0; i < 5; i++)
{
listTask.Add(taskFactory.StartNew(() => TestThread("Task")));
}
Task.WaitAll(listTask.ToArray());//等待所有线程结束
Console.WriteLine("******************net3_5_Click end {0}********************", Thread.CurrentThread.ManagedThreadId);
sw.Stop();
Console.WriteLine("总耗时为{0}", sw.ElapsedMilliseconds);
}
4、 task的一些高级功能
/// <summary>
/// task有很多高级功能
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void fancyTask_Click(object sender, EventArgs e)
{
Stopwatch sw = new Stopwatch();
sw.Start();
Console.WriteLine("******************net3_5_Click start {0}********************", Thread.CurrentThread.ManagedThreadId);
//TaskFactory taskFactory = new TaskFactory();
//for(int i = 0; i < 5; i++)
//{
// taskFactory.StartNew(() => TestThread("Task"));
//}
TaskFactory taskFactory = new TaskFactory();
List<Task> listTask = new List<Task>();
for (int i = 0; i < 5; i++)
{
listTask.Add(taskFactory.StartNew(() => TestThread("Task")));
}
//等待所有线程结束执行一个action
taskFactory.ContinueWhenAll(listTask.ToArray(), t => Console.WriteLine("ContinueWhenAll 当前线程为{0}", Thread.CurrentThread.ManagedThreadId));
//等待任意线程结束执行一个action
//taskFactory.ContinueWhenAny(listTask.ToArray(), t => Console.WriteLine("ContinueWhenAny 当前线程为{0}", Thread.CurrentThread.ManagedThreadId));
//Task.WaitAll(listTask.ToArray());//等待所有线程结束
//Task.WaitAny(listTask.ToArray());//等待任意一个线程结束
Console.WriteLine("******************net3_5_Click end {0}********************", Thread.CurrentThread.ManagedThreadId);
sw.Stop();
Console.WriteLine("总耗时为{0}", sw.ElapsedMilliseconds);
}