异步 ThreadPool
生活随笔
收集整理的這篇文章主要介紹了
异步 ThreadPool
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
線程池是單例,一個進程里只有一個線程池
private void btnThreadPool_Click(object sender, EventArgs e){Stopwatch watch = new Stopwatch();watch.Start();Console.WriteLine();Console.WriteLine("****************btnThreadPool_Click Start 主線程id={0}*****************", Thread.CurrentThread.ManagedThreadId);List<ManualResetEvent> mreList = new List<ManualResetEvent>();for (int i = 0; i < 5; i++){int b = i;mreList.Add(new ManualResetEvent(false));WaitCallback method = t =>{this.TestThreadWithoutReturn(string.Format("btnThreadPool_Click_{0}", b));Console.WriteLine("我是回調 btnThreadPool_Click_{0}", b);mreList[b].Set();};ThreadPool.QueueUserWorkItem(method);//WaitCallback method = t => this.TestThreadWithoutReturn(t.ToString ());//ThreadPool.QueueUserWorkItem(method, string.Format("btnThreadPool_Click_{0}", b));} private void TestThreadWithoutReturn(string name){Console.WriteLine("TestThread Start Name={2},當前線程的id:{0},當前時間為{1}", Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToString("hh:mm:ss:fff"), name);long Sum = 0;for (int i = 1; i < 999999999; i++){Sum += i;}//Thread.Sleep(2000);Console.WriteLine("TestThread End Name={2},當前線程的id:{0},當前時間為{1}", Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToString("hh:mm:ss:fff"), name);}?
轉載于:https://www.cnblogs.com/xiao9426926/p/6429540.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的异步 ThreadPool的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: distable:table-cell
- 下一篇: 台式电脑无线U盘WIFL怎么安装 如何安