总结:
thread能启动非常多的线程同时工作,但threadpool和Task同时并行的线程数有限,约20个左右,但Threadpool设置最大并行数后则不会超过此数。
另:thread和threadpool的子线程中如果有未处理的异常,则主程序则会出现“*** 已停止工作”这样的错误(程序自动退出),而Task的子线程中出现未处理的异常时不影响主程序,即主程序不会退出。
public partial class Form1 : Form
{ int count = 0; int 总次数 = 0; Random r = new Random(); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //有300多个线程并行 this.count = 0; this.总次数 = 0; for (int m = 0; m < 400; m++) { System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(this.run)); t.Start(); } } private void button2_Click(object sender, EventArgs e) { //有10到40个线程并行 this.count = 0; this.总次数 = 0; int a,b; //得到32767和1000 System.Threading.ThreadPool.GetMaxThreads(out a,out b); for (int m = 0; m < 400; m++) { System.Threading.ThreadPool.QueueUserWorkItem(new WaitCallback( this.run),0); } } private void button5_Click(object sender, EventArgs e) { //有10到40个线程并行 this.count = 0; this.总次数 = 0; int a, b; //得到32767和1000 System.Threading.ThreadPool.GetMaxThreads(out a, out b); //设置为最大10后,并行数稳定在10个,设置最大100后,最多并行还是10到40个,这应该和机器有关 System.Threading.ThreadPool.SetMaxThreads(10, 100); for (int m = 0; m < 400; m++) { System.Threading.ThreadPool.QueueUserWorkItem(new WaitCallback(this.run), 0); } } private void button3_Click(object sender, EventArgs e) { //有10到40多个线程并行,运行效率基本同于QueueUserWorkItem this.count = 0; this.总次数 = 0; for (int m = 0; m < 400; m++) { System.Threading.Tasks.Task.Factory.StartNew(this.run); } } private void run() { Interlocked.Increment(ref this.总次数); Interlocked.Increment(ref this.count); System.Threading.Thread.Sleep(r.Next(10) * 1000); System.Threading.Interlocked.Decrement(ref this.count); this.异常(); } private void run(object ob) { Interlocked.Increment(ref this.总次数); Interlocked.Increment(ref this.count); System.Threading.Thread.Sleep(r.Next(10) * 1000); System.Threading.Interlocked.Decrement(ref this.count); this.异常(); } private void 异常() { throw new Exception("ddd"); //try //{ // throw new Exception("ddd"); //} //catch (Exception ex) //{ //} } private void button4_Click(object sender, EventArgs e) { this.textBox1.Text = this.count.ToString() + " / " + this.总次数; } }