using System;
using System.Threading;
namespace 线程池
{
class Program
{
static void ThreadMethod(object state)
{
Console.WriteLine("线程开始"+Thread .CurrentThread .ManagedThreadId);
Thread.Sleep(2000);
Console.WriteLine("线程结束");
}
static void Main(string[] args)
{
ThreadPool.QueueUserWorkItem(ThreadMethod);//开启一个工作线程(适合耗时较短的任务)
ThreadPool.QueueUserWorkItem(ThreadMethod);
ThreadPool.QueueUserWorkItem(ThreadMethod);
ThreadPool.QueueUserWorkItem(ThreadMethod);
ThreadPool.QueueUserWorkItem(ThreadMethod);
Console.ReadKey();
}
}
}
//-------------------------------------------------------------------------
using System;
using System.Threading;
using System.Threading.Tasks;
namespace Task类开启线程
{
class Program
{
static void ThreadMethod()
{
Console.WriteLine("线程开始" + Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(2000);
Console.WriteLine("线程结束");
}
static void Main(string[] args)
{
//1.开启方式
/*Task t = new Task(ThreadMethod);
t.Start();*/
//2.开启方式
/*TaskFactory tf = new TaskFactory();
Task t = tf.StartNew(ThreadMethod);*/
Console.WriteLine("Main");
Console.ReadKey();
}
}
}