本帖最后由 ro4ters 于 2015-8-6 22:34 编辑
[C#] 纯文本查看 复制代码 并行与普通区别: 普通: 1000个账号,每个账号执行需要耗时1s.
等任务执行完.1000s.
并行: 1s内同时(“歧义:不是绝对的同时”)执行1000个账号
private void btnExec_Click(objectsender, EventArgs e)
{
List<Users>ulist = new List<Users>();
ulist.Add(new Users("君临0", "123456"));
ulist.Add(new Users("君临1", "123456"));
ulist.Add(new Users("君临2", "123456"));
ulist.Add(new Users("君临3", "123456"));
//多线程1. 一条线程执行多任务.
Thread th = newThread(new ThreadStart(delegate{
for (inti = 0; i < ulist.Count; i++)
{
lock(obj)
{
x =GetIndexHtml(ulist);
//x 得到值以后才能继续
}
}
}));
th.Start();
}
object obj = new object();
string data = "{0},{1}";
string url = "www.xuanjics.com";
int x;//公共变量
private intGetIndexHtml(Users user)
{
/*
get 过程
*/
string str = string.Format(data,user.Name, user.Pass);
return 0;
}
//多线程2
for (inti = 0; i < ulist.Count; i++)
{
//多少个任务多少条线程.
Thread th = newThread(new ThreadStart(delegate
{
lock(obj) //这还有用么?
{
x =GetIndexHtml(ulist);
//x 得到值以后才能继续
}
}));
th.Start();
}
//多线程3
for (inti = 0; i < ulist.Count; i++)
{
lock (obj) //这是可以的
{
//多少个任务多少条线程.
Threadth = new Thread(new ThreadStart(delegate
{
x =GetIndexHtml(ulist);
//x 得到值以后才能继续
}));
th.Start();
}
}
视频下载地址:http://bbs.msdn5.com/forum-2-1.html |