悬赏园豆:50
[已关闭问题]
关闭于 2010-03-26 16:39
<p style="margin-top: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;">线程偷懒?</p>
<p style="margin-top: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;">多线程下载的时候为什么有线程偷懒,只有两个线程在工作,如图我开启了5个线程,却只有0和3在工作。</p>
<p style="margin-top: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;"><img style="border: 0px initial initial;" src="http://pic002.cnblogs.com/img/c5812/201003/2010032616091861.jpg" alt="" /></p>
<p style="margin-top: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;">代码如下:</p>
<div class="cnblogs_code" style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #f5f5f5; font-size: 13px; font-family: 'Courier New'; line-height: 19px; word-break: break-all; overflow-x: auto; overflow-y: auto; background-position: initial initial; background-repeat: initial initial; padding: 5px; margin: 0px; border: 1px solid #cccccc;" onclick="cnblogs_code_show('f7eb0e47-26f1-43a7-9c35-67cef2e1749a')"><img id="code_img_closed_f7eb0e47-26f1-43a7-9c35-67cef2e1749a" class="code_img_closed" style="vertical-align: middle; padding-right: 5px; border: 0px initial initial;" src="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" alt="" /><span class="cnblogs_code_collapse" style="background-color: #ffffff; font-family: 'Courier New'; padding: 2px; border: 1px solid #808080;">代码</span></div>
<p style="margin-top: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;"> </p>
<p style="margin-top: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;">问题2,如果我把下载方法改成下面这样,线程都不工作了,似乎是死锁?</p>
<p style="margin-top: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;">这段代码<strong>的第7行</strong>是添加下载范围,如果没这段似乎就不出现此问题了,难道是ms的基础类锁定有问题吗</p>
<p style="margin-top: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;"> </p>
<div class="cnblogs_code" style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #f5f5f5; font-size: 13px; font-family: 'Courier New'; line-height: 19px; word-break: break-all; overflow-x: auto; overflow-y: auto; background-position: initial initial; background-repeat: initial initial; padding: 5px; margin: 0px; border: 1px solid #cccccc;">
<pre><div style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #f5f5f5; font-size: 12px; font-family: verdana, Arial, Helvetica; line-height: 18px; background-position: initial initial; background-repeat: initial initial; margin: 0px;"><span style="font-family: 'Courier New'; color: #008080;"> 1</span> <span style="font-family: 'Courier New'; color: #000000;"><br /></span><span style="font-family: 'Courier New'; color: #008080;"> 2</span> <span style="font-family: 'Courier New'; color: #000000;"> </span><span style="font-family: 'Courier New'; color: #0000ff;">static</span><span style="font-family: 'Courier New'; color: #000000;"> </span><span style="font-family: 'Courier New'; color: #0000ff;">int</span><span style="font-family: 'Courier New'; color: #000000;"> ran </span><span style="font-family: 'Courier New'; color: #000000;">=</span><span style="font-family: 'Courier New'; color: #000000;"> </span><span style="font-family: 'Courier New'; color: #800080;">0</span><span style="font-family: 'Courier New'; color: #000000;">;<br /></span><span style="font-family: 'Courier New'; color: #008080;"> 3</span> <span style="font-family: 'Courier New'; color: #000000;"> </span><span style="font-family: 'Courier New'; color: #0000ff;">static</span><span style="font-family: 'Courier New'; color: #000000;"> </span><span style="font-family: 'Courier New'; color: #0000ff;">void</span><span style="font-family: 'Courier New'; color: #000000;"> down()<br /></span><span style="font-family: 'Courier New'; color: #008080;"> 4</span> <span style="font-family: 'Courier New'; color: #000000;"> { <br /></span><span style="font-family: 'Courier New'; color: #008080;"> 5</span> <span style="font-family: 'Courier New'; color: #000000;"> </span><span style="font-family: 'Courier New'; color: #0000ff;">string</span><span style="font-family: 'Courier New'; color: #000000;"> url </span><span style="font-family: 'Courier New'; color: #000000;">=</span><span style="font-family: 'Courier New'; color: #000000;"> </span><span style="font-family: 'Courier New'; color: #800000;">"</span><span style="font-family: 'Courier New'; color: #800000;">http://dl_dir.qq.com/qqfile/qq/QQ2010/QQ2010Beta2.exe</span><span style="font-family: 'Courier New'; color: #800000;">"</span><span style="font-family: 'Courier New'; color: #000000;">;<br /></span><span style="font-family: 'Courier New'; color: #008080;"> 6</span> <span style="font-family: 'Courier New'; color: #000000;"> HttpWebRequest req </span><span style="font-family: 'Courier New'; color: #000000;">=</span><span style="font-family: 'Courier New'; color: #000000;"> WebRequest.Create(url) </span><span style="font-family: 'Courier New'; color: #0000ff;">as</span><span style="font-family: 'Courier New'; color: #000000;"> HttpWebRequest;<br /></span><span style="font-family: 'Courier New'; color: #008080;"> 7</span> <span style="font-family: 'Courier New'; color: #000000;"> <span style="font-family: 'Courier New'; background-color: #339966;">req.AddRange(</span></span><span style="font-family: 'Courier New'; color: #800080;"><span style="font-family: 'Courier New'; background-color: #339966;">1</span></span><span style="font-family: 'Courier New'; color: #000000;"><span style="font-family: 'Courier New'; background-color: #339966;">,</span></span><span style="font-family: 'Courier New'; color: #800080;"><span style="font-family: 'Courier New'; background-color: #339966;">500000</span></span><span style="font-family: 'Courier New'; color: #000000;"><span style="font-family: 'Courier New'; background-color: #339966;">);</span><br /></span><span style="font-family: 'Courier New'; color: #008080;"> 8</span> <span style="font-family: 'Courier New'; color: #000000;"> Stream stream </span><span style="font-family: 'Courier New'; color: #000000;">=</span><span style="font-family: 'Courier New'; color: #000000;"> req.GetResponse().GetResponseStream();<br /></span><span style="font-family: 'Courier New'; color: #008080;"> 9</span> <span style="font-family: 'Courier New'; color: #000000;"> </span><span style="font-family: 'Courier New'; color: #0000ff;">byte</span><span style="font-family: 'Courier New'; color: #000000;">[] buffer </span><span style="font-family: 'Courier New'; color: #000000;">=</span><span style="font-family: 'Courier New'; color: #000000;"> </span><span style="font-family: 'Courier New'; color: #0000ff;">new</span><span style="font-family: 'Courier New'; color: #000000;"> </span><span style="font-family: 'Courier New'; color: #0000ff;">byte</span><span style="font-family: 'Courier New'; color: #000000;">[</span><span style="font-family: 'Courier New'; color: #800080;">256</span><span style="font-family: 'Courier New'; color: #000000;">];<br /></span><span style="font-family: 'Courier New'; color: #008080;">10</span> <span style="font-family: 'Courier New'; color: #000000;"> </span><span style="font-family: 'Courier New'; color: #0000ff;">int</span><span style="font-family: 'Courier New'; color: #000000;"> i </span><span style="font-family: 'Courier New'; color: #000000;">=</span><span style="font-family: 'Courier New'; color: #000000;"> stream.Read(buffer, </span><span style="font-family: 'Courier New'; color: #800080;">0</span><span style="font-family: 'Courier New'; color: #000000;">, </span><span style="font-family: 'Courier New'; color: #800080;">256</span><span style="font-family: 'Courier New'; color: #000000;">);<br /></span><span style="font-family: 'Courier New'; color: #008080;">11</span> <span style="font-family: 'Courier New'; color: #000000;"> </span><span style="font-family: 'Courier New'; color: #0000ff;">while</span><span style="font-family: 'Courier New'; color: #000000;"> (i </span><span style="font-family: 'Courier New'; color: #000000;">></span><span style="font-family: 'Courier New'; color: #000000;"> </span><span style="font-family: 'Courier New'; color: #800080;">0</span><span style="font-family: 'Courier New'; color: #000000;">)<br /></span><span style="font-family: 'Courier New'; color: #008080;">12</span> <span style="font-family: 'Courier New'; color: #000000;"> {<br /></span><span style="font-family: 'Courier New'; color: #008080;">13</span> <span style="font-family: 'Courier New'; color: #000000;"> i </span><span style="font-family: 'Courier New'; color: #000000;">=</span><span style="font-family: 'Courier New'; color: #000000;"> stream.Read(buffer, </span><span style="font-family: 'Courier New'; color: #800080;">0</span><span style="font-family: 'Courier New'; color: #000000;">, </span><span style="font-family: 'Courier New'; color: #800080;">256</span><span style="font-family: 'Courier New'; color: #000000;">);<br /></span><span style="font-family: 'Courier New'; color: #008080;">14</span> <span style="font-family: 'Courier New'; color: #000000;"> } <br /></span><span style="font-family: 'Courier New'; color: #008080;">15</span> <span style="font-family: 'Courier New'; color: #000000;"> }</span></div></pre>
</div>
<p style="margin-top: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;"> </p>
<p style="margin-top: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;"> </p>
<p style="margin-top: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;"> </p>
<p style="margin-top: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;">错误如图(把项目暂停时查看变量值才知道线程都已经停了,可它们还没工作呢)<img style="border: 0px initial initial;" src="http://pic002.cnblogs.com/img/c5812/201003/2010032616140340.jpg" alt="" /></p>
<p style="margin-top: 0px; margin-right: auto; margin-bottom: 10px; margin-left: auto;">请高手帮忙解决下啊</p>