C#中线程和线程pool的使用和比较笔录
C#中线程和线程pool的使用和比较
1. 采用通常方式使用线程代价很高;
2. 对于生命周期段的线程使用线程迟;
3. 线程pool性能
(1) 在线程池中没有线程可以用时,起代价是与CreatThread线程相同的;
(2) 由于线程池线程可以被重用,因此当线程池中存在空闲线程时线程所带来的开销非常少。
(3) 线程池中没有线程时则回创建新的线程(线程池线程树小于25 个时),如果线程池中线程过一断时间没有被使用将自动清楚该线程,释放所占用的资源
命名空间 和变量
using System.Threading;
private int threadCounter = 0;
private int threadPoolCounter = 0;
private AutoResetEvent doneEvent;
//创建1000个线程启动
private void button1_Click(object sender, EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
threadCounter = 0;
//通知正在等待的线已发生事件
doneEvent = new AutoResetEvent(false);
label1.Text = "";
label2.Update();
//获取系统启动后经过的毫秒秒数
int elapsedTime = Environment.TickCount;
for (int i = 0; i < 1000; i++)
{
Thread workerThread = new Thread(new ThreadStart(MyWorkerThread));
workerThread.Start();
}
doneEvent.WaitOne();
//运行时间
elapsedTime = Environment.TickCount - elapsedTime;
label1.Text = "Creating threads: " + elapsedTime.ToString() + "mSec";
Cursor.Current = Cursors.Default;
}
private void MyWorkerThread()
{
//threadCounter++;
Interlocked.Increment(ref threadCounter);
Thread.Sleep(0);
if(threadCounter == 1000)
{
//设置事件的状态为终止
doneEvent.Set();
}
}
//1000个线程的pool
private void button2_Click(object sender, EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
threadPoolCounter = 0;
doneEvent = new AutoResetEvent(false);
label2.Text = "";
label2.Update();
int elapsedTime = Environment.TickCount;
for(int i = 0;i < 1000;i++)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(MyWaitCallBack));
}
doneEvent.WaitOne();
////运行时间
elapsedTime = Environment.TickCount - elapsedTime;
label2.Text = "Creating threads: " + elapsedTime.ToString() + "mSec";
Cursor.Current = Cursors.Default;
}
private void MyWaitCallBack(object stateInfo)
{
Interlocked.Increment(ref threadPoolCounter);
Thread.Sleep(0);
if (threadPoolCounter == 1000)
{
//设置事件的状态为终止
doneEvent.Set();
}
}
private void button1_Click(object sender, EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
threadCounter = 0;
//通知正在等待的线已发生事件
doneEvent = new AutoResetEvent(false);
label1.Text = "";
label2.Update();
//获取系统启动后经过的毫秒秒数
int elapsedTime = Environment.TickCount;
for (int i = 0; i < 1000; i++)
{
Thread workerThread = new Thread(new ThreadStart(MyWorkerThread));
workerThread.Start();
}
doneEvent.WaitOne();
//运行时间
elapsedTime = Environment.TickCount - elapsedTime;
label1.Text = "Creating threads: " + elapsedTime.ToString() + "mSec";
Cursor.Current = Cursors.Default;
}
private void MyWorkerThread()
{
//threadCounter++;
Interlocked.Increment(ref threadCounter);
Thread.Sleep(0);
if(threadCounter == 1000)
{
//设置事件的状态为终止
doneEvent.Set();
}
}
//1000个线程的pool
private void button2_Click(object sender, EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
threadPoolCounter = 0;
doneEvent = new AutoResetEvent(false);
label2.Text = "";
label2.Update();
int elapsedTime = Environment.TickCount;
for(int i = 0;i < 1000;i++)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(MyWaitCallBack));
}
doneEvent.WaitOne();
////运行时间
elapsedTime = Environment.TickCount - elapsedTime;
label2.Text = "Creating threads: " + elapsedTime.ToString() + "mSec";
Cursor.Current = Cursors.Default;
}
private void MyWaitCallBack(object stateInfo)
{
Interlocked.Increment(ref threadPoolCounter);
Thread.Sleep(0);
if (threadPoolCounter == 1000)
{
//设置事件的状态为终止
doneEvent.Set();
}
}