码迷,mamicode.com
首页 > 编程语言 > 详细

关于多线程实例运用

时间:2016-11-17 13:05:43      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:str   pwa   back   top   i++   edm   object   read   line   

一、

class ThreadParam
{
public int ThreadID { get; set; }
public ManualResetEvent MSE { get; set; }
}

 

二、

static void Main(string[] args)
{



List<ThreadParam> list = new List<ThreadParam>();
Stopwatch watcher = new Stopwatch();
watcher.Start();
for (int i = 0; i < 5; i++)
{
ThreadParam tp = new ThreadParam();
tp.ThreadID = i;
tp.MSE = new ManualResetEvent(false);
Thread t = new Thread(new ParameterizedThreadStart(ThreadVoid));
t.IsBackground = true;
t.Start(tp);
list.Add(tp);
}
foreach (ThreadParam tp in list)
{
tp.MSE.WaitOne();
}
watcher.Stop();

Console.WriteLine("总用时:" + watcher.ElapsedMilliseconds.ToString() + "ms");
Console.ReadLine();
}

三、

static void ThreadVoid(object o)
{
ThreadParam tp = (ThreadParam)o;
Stopwatch watcher = new Stopwatch();
watcher.Start();
Console.WriteLine("线程" + tp.ThreadID.ToString() + ":开始");
Thread.Sleep(5000);
watcher.Stop();
Console.WriteLine("线程" + tp.ThreadID.ToString() + ":结束,用时" + watcher.ElapsedMilliseconds.ToString() + "ms");
tp.MSE.Set();
}

关于多线程实例运用

标签:str   pwa   back   top   i++   edm   object   read   line   

原文地址:http://www.cnblogs.com/SuperPander/p/6073198.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!