首页 > 代码库 > using System.Threading;

using System.Threading;

技术分享
 1         /// <summary>
 2         /// 执行动作:耗时而已
 3         /// </summary>
 4         private void TestThread(string threadName)
 5         {
 6             Console.WriteLine("TestThread Start  Name={2}当前线程的id:{0},当前时间为{1},",
 7                 System.Threading.Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"), threadName);
 8             long sum = 0;
 9             for (int i = 1; i < 999999999; i++)
10             {
11                 sum += i;
12             }
13             //Thread.Sleep(1000);
14             Console.WriteLine("TestThread End  Name={2}当前线程的id:{0},当前时间为{1},计算结果{3}",
15                 System.Threading.Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"), threadName, sum);
16         }
TestThread

 同步方法

 1         private void btnSync_Click(object sender, EventArgs e)
 2         {
 3             Stopwatch watch = new Stopwatch();
 4             watch.Start();
 5 
 6             Console.WriteLine();
 7             Console.WriteLine("*********************************btnSync_Click Start*********************************************");
 8             Console.WriteLine("btnSync_Click当前主线程的id:{0}", Thread.CurrentThread.ManagedThreadId);
 9 
10             for (int i = 0; i < 5; i++)
11             {
12                 string name = string.Format("btnSync_Click_{0}", i);
13                 TestThread(name);
14             }
15             watch.Stop();
16             Console.WriteLine("*********************************btnSync_Click End {0}********************************************", watch.ElapsedMilliseconds);
17             Console.WriteLine();
18 
19         }    

异步调用

1 异步调用
2 1 同步方法会卡界面,异步多线程不会,原因:同步方法占用了UI线程,无法响应其他操作;异步多线程,UI线程不需要等待计算,计算由子线程完成
3 2 同步方法慢,只有主线程计算;异步方法快,启动了多个线程同时计算,会占用更多的资源(多线程的调度管理,也是需要消耗资源的)
4 3 异步方法的启动和执行时无序,原因:线程的申请是无序获取;不同线程同一个任务的执行速度也不一定相同;

 

 1        Stopwatch watch = new Stopwatch();
 2             watch.Start();
 3 
 4             Console.WriteLine();
 5             Console.WriteLine("*********************************btnAsync_Click Start*********************************************");
 6             Console.WriteLine("btnAsync_Click当前主线程的id:{0}", Thread.CurrentThread.ManagedThreadId);
 7 
 8             List<IAsyncResult> asyncResultList = new List<IAsyncResult>();
 9 
10             for (int i = 0; i < 5; i++)
11             {
12                 string name = string.Format("btnSync_Click_{0}", i);
13                 Action<string> act = TestThread;
14                 //act.Invoke(name);
15                 asyncResultList.Add(act.BeginInvoke(name, null, null));
16                 //Thread.Sleep(1);
17             }
18           watch.Stop();
19 
20             Console.WriteLine("*********************************btnAsync_Click End {0}********************************************", watch.ElapsedMilliseconds);
21             Console.WriteLine();    

 

等待:

1             foreach (var item in asyncResultList)
2             {
3                 item.AsyncWaitHandle.WaitOne();
4             }
5 或者
6             while (asyncResultList.Count(r => r.IsCompleted) < 5)
7             {
8                 Thread.Sleep(100);//主线程等待着
9             }

共有资源的访问

 

private int IntValue = http://www.mamicode.com/0;
private static object IntValueLock = new object();

 

 1        for (int i = 0; i < 10000; i++)
 2             {
 3                 IntValue++;
 4             }
 5 
 6             Action act = () =>
 7                 {
 8                     lock (IntValueLock)
 9                     {
10                         IntValue++;
11                         Console.WriteLine(IntValue.ToString());
12                     }
13                 };
14             for (int i = 0; i < 10000; i++)
15             {
16                 act.BeginInvoke(null, null);
17             }

 

using System.Threading;