using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Test
{
class Program
{
static void Main()
{
CurrentThreadInfo("主线程Main()的");
Parallel.Invoke(() => { Work1(); }, () => { Work2(); });
Console.ReadLine();
}
private static void Work1()
{
Console.WriteLine("Work1 run {");
for (int i = 0; i < 10; i++)
{
Console.WriteLine("\tWork1 ***---{0} {1} 线程Id:{2}",
i.ToString(), DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), Thread.CurrentThread.ManagedThreadId);
}
Console.WriteLine("end Work1 }");
CurrentThreadInfo("Work1()的方法");
}
private static void Work2()
{
Console.WriteLine("Work2 run {");
for (int i = 100; i < 110; i++)
{
Console.WriteLine("\tWork2 ###_{0} {1} 线程Id:{2}",
i.ToString(), DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), Thread.CurrentThread.ManagedThreadId);
}
Console.WriteLine("end Work2 }");
CurrentThreadInfo("Work2()的方法");
}
/// <summary>
/// 当前线程信息
/// </summary>
private static void CurrentThreadInfo(string name)
{
StringBuilder builder = new StringBuilder();
builder.AppendLine("");
builder.AppendLine(String.Format("{0}线程Id:\t\t{1}", name, Thread.CurrentThread.ManagedThreadId));
builder.AppendLine(String.Format("{0}是否使用线程池:\t{1}", name, Thread.CurrentThread.IsThreadPoolThread));
builder.AppendLine(String.Format("{0}是否后台线程:\t{1}", name, Thread.CurrentThread.IsBackground));
builder.AppendLine(String.Format("{0}线程状态:\t\t{1}", name, Thread.CurrentThread.ThreadState.ToString()));
builder.AppendLine(String.Format("{0}当前时间:\t\t{1}", name, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
builder.AppendLine("");
Console.WriteLine(builder.ToString());
}
}
}
/*
主线程Main()的线程Id: 10
主线程Main()的是否使用线程池: False
主线程Main()的是否后台线程: False
主线程Main()的线程状态: Running
主线程Main()的当前时间: 2016-07-28 23:34:16
Work1 run {
Work1 ***---0 2016-07-28 23:34:16 线程Id:10
Work1 ***---1 2016-07-28 23:34:16 线程Id:10
Work2 run {
Work2 ###_100 2016-07-28 23:34:16 线程Id:6
Work2 ###_101 2016-07-28 23:34:16 线程Id:6
Work2 ###_102 2016-07-28 23:34:16 线程Id:6
Work2 ###_103 2016-07-28 23:34:16 线程Id:6
Work2 ###_104 2016-07-28 23:34:16 线程Id:6
Work2 ###_105 2016-07-28 23:34:16 线程Id:6
Work2 ###_106 2016-07-28 23:34:16 线程Id:6
Work2 ###_107 2016-07-28 23:34:16 线程Id:6
Work2 ###_108 2016-07-28 23:34:16 线程Id:6
Work1 ***---2 2016-07-28 23:34:16 线程Id:10
Work1 ***---3 2016-07-28 23:34:16 线程Id:10
Work1 ***---4 2016-07-28 23:34:16 线程Id:10
Work1 ***---5 2016-07-28 23:34:16 线程Id:10
Work1 ***---6 2016-07-28 23:34:16 线程Id:10
Work1 ***---7 2016-07-28 23:34:16 线程Id:10
Work1 ***---8 2016-07-28 23:34:16 线程Id:10
Work1 ***---9 2016-07-28 23:34:16 线程Id:10
Work2 ###_109 2016-07-28 23:34:16 线程Id:6
end Work2 }
Work2()的方法线程Id: 6
Work2()的方法是否使用线程池: True
Work2()的方法是否后台线程: True
Work2()的方法线程状态: Background
Work2()的方法当前时间: 2016-07-28 23:34:16
end Work1 }
Work1()的方法线程Id: 10
Work1()的方法是否使用线程池: False
Work1()的方法是否后台线程: False
Work1()的方法线程状态: Running
Work1()的方法当前时间: 2016-07-28 23:34:16
*/
总结:
Parallel.Invoke 使用的是:前台主线程 + 线程池后台线程。
转自:http://www.cnblogs.com/rose0705/articles/5716766.html