管理线程中的线程 [英] Managing Threads in threading
本文介绍了管理线程中的线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在做实验室练习.在本练习中,我不理解urls.length
和waitHandles.waitAll
.
I have a lab exercise. I do not understand urls.length
and waitHandles.waitAll
in this exercise.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net;
namespace LabEx1
{
class Program
{
static void Main(string[] args)
{
AutoResetEvent[] waitHandles = new AutoResetEvent[urls.length];
int i = 0;
foreach (string url in urls)
{
waitHandles[i] = new AutoResetEvent(false);
ThreadInfo ti = new ThreadInfo(url, waitHandles[i]);
ThreadPool.QueueUserWorkItem(getPage, ti);
i++;
waitHandles.waitAll(waitHandles);
}
}
class ThreadInfo
{
public string url;
public AutoResetEvent are;
public ThreadInfo(string _url, AutoResetEvent _are)
{
url = _url;
are = _are;
}
static void getPage(object data)
{
ThreadInfo ti = (ThreadInfo)data;
WebResponse wr = WebRequest.Create(ti.url).GetResponse();
Console.WriteLine(ti.url + ": " + wr.Headers["Content-Length"]);
wr.Close();
ti.are.Set();
}
}
}
}
推荐答案
Google不在您所在的位置吗?尝试使用Google搜索"AutoResetEvent",看看它能告诉您什么.
就urls.Length
问题而言,urls
似乎是一个字符串数组,并将该数组的长度传递给AutoRresetEvent
.当然,如果您研究过AutoResetEvent
,您就会知道为什么它将数组的长度作为参数传递.
Isn''t google available where you are? Try googling "AutoResetEvent" and see what it tells you.
As far as theurls.Length
issue is concerned,urls
appears to be an array of strings, and it''s passing the length of that array intoAutoRresetEvent
. Of course, if you had researchedAutoResetEvent
, you''d know why it''s passing the length of the array as a parameter.
检查此链接.
http://msdn.microsoft.com/en-us/library/system.threading. autoresetevent.aspx [ ^ ]
简而言之,AutoResetEvent
通知等待的线程已发生事件.
WaitAll
等待指定数组中的所有元素接收信号
http://msdn.microsoft.com/en-us/library/z6w25xa6.aspx [ ^ ]
check this link out.
http://msdn.microsoft.com/en-us/library/system.threading.autoresetevent.aspx[^]
In a nutshellAutoResetEvent
notifies a waiting thread that an event has occurred.
WaitAll
Waits for all the elements in the specified array to receive a signal
http://msdn.microsoft.com/en-us/library/z6w25xa6.aspx[^]
这篇关于管理线程中的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文