首页 新闻 会员 周边 捐助

多线程下载文件问题

0
悬赏园豆:50 [已解决问题] 解决于 2010-03-27 22:00

多线程下载的时候为什么有线程偷懒,只有两个线程在工作,如图我开启了5个线程,却只有0和3在工作。

代码
1 using System;
2  using System.Collections.Generic;
3  using System.Text;
4  using System.Drawing;
5  using System.Net;
6  using System.IO;
7 using System.Threading;
8
9 namespace ConsoleApplication9
10 {
11 class Program
12 {
13 static void Main(string[] args)
14 {
15
16
17 Thread[] ts = new Thread[5];
18 for (int i = 0; i < ts.Length; i++)
19 {
20 ts[i] = new Thread(new ThreadStart(down));
21 ts[i].Name = "线程 " + i.ToString();
22 ts[i].Start();
23
24 }
25 }
26
27 static int ran = 0;
28 static void down()
29 {
30 using (FileStream fs = new FileStream(Thread.CurrentThread.Name, FileMode.Create))
31 {
32 string url = "http://dl_dir.qq.com/qqfile/qq/QQ2010/QQ2010Beta2.exe";
33 HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
34 req.AddRange(ran, ran + 1024 * 1024 * 10);
35 Stream stream = req.GetResponse().GetResponseStream();
36 byte[] buffer = new byte[256];
37 int i = stream.Read(buffer, 0, 256);
38 while (i > 0)
39 {
40 i = stream.Read(buffer, 0, 256);
41 Console.WriteLine(Thread.CurrentThread.Name);
42 fs.Write(buffer, 0, i);
43 Thread.Sleep(500);
44 }
45 }
46
47 }
48
49
50 }
51 }

 

问题2,如果我把下载方法改成下面这样,线程都不工作了,似乎是死锁?

这段代码的第7行是添加下载范围,如果没这段似乎就不出现此问题了,难道是ms的基础类锁定有问题吗

 

1
2 static int ran = 0;
3 static void down()
4 {
5 string url = "http://dl_dir.qq.com/qqfile/qq/QQ2010/QQ2010Beta2.exe";
6 HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
7 req.AddRange(1,500000);
8 Stream stream = req.GetResponse().GetResponseStream();
9 byte[] buffer = new byte[256];
10 int i = stream.Read(buffer, 0, 256);
11 while (i > 0)
12 {
13 i = stream.Read(buffer, 0, 256);
14 }
15 }

 

 

 

错误如图(把项目暂停时查看变量值才知道线程都已经停了,可它们还没工作呢)

请高手帮忙解决下啊

问题补充: 上面代码直接复制会连行号也复制过去,所以看下面代码 第一部分源码 ------------------------------------- using System; using System.Collections.Generic; using System.Text; using System.Drawing; using System.Net; using System.IO; using System.Threading; namespace ConsoleApplication9 { class Program { static void Main(string[] args) { Thread[] ts = new Thread[5]; for (int i = 0; i < ts.Length; i++) { ts[i] = new Thread(new ThreadStart(down)); ts[i].Name = "线程 " + i.ToString(); ts[i].Start(); } } static int ran = 0; static void down() { using (FileStream fs = new FileStream(Thread.CurrentThread.Name, FileMode.Create)) { string url = "http://dl_dir.qq.com/qqfile/qq/QQ2010/QQ2010Beta2.exe"; HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest; req.AddRange(ran, ran + 1024 * 1024 * 10); Stream stream = req.GetResponse().GetResponseStream(); byte[] buffer = new byte[256]; int i = stream.Read(buffer, 0, 256); while (i > 0) { i = stream.Read(buffer, 0, 256); Console.WriteLine(Thread.CurrentThread.Name); fs.Write(buffer, 0, i); Thread.Sleep(500); } } } } }
一個魜D.蓝的主页 一個魜D.蓝 | 初学一级 | 园豆:40
提问于:2010-03-26 16:16
< >
分享
最佳答案
0

webrequest好像有并发线程数量的限制问题,是可以修改的,你试着改一下看看:)

收获园豆:50
LittlePeng | 老鸟四级 |园豆:3445 | 2010-03-27 19:02
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册