故這幾天看了下線(xiàn)程參考手冊結合自己的心得整理一下放在博客上作為自己的學(xué)習筆記。
好了,我們知道“負載”是一個(gè)很時(shí)尚,很牛X的玩意,往大處說(shuō),網(wǎng)站需要負載,數據庫需要負載。往小處說(shuō),線(xiàn)程也需要負載,面對海量的
用戶(hù)請求,我們的單線(xiàn)程肯定扛不住,那么怎么辦,一定要負載,所以說(shuō)多線(xiàn)程是我們碼農必須要熟練掌握的一門(mén)技術(shù)。
在framework中給我們提供了一個(gè)Threading命名空間,下面是一個(gè)msdn上不完整的截圖:

在后面的系列中我也是主要整理這幾個(gè)類(lèi)的使用方法和應用場(chǎng)景。
一:Thread的使用
我們知道這個(gè)類(lèi)代表處理器線(xiàn)程,在Thread中有幾個(gè)比較常用和重要的方法。
<1> sleep: 這個(gè)算是最簡(jiǎn)單的了。
<2> join: 這個(gè)可以讓并發(fā)行處理變成串行化,什么意思呢?上代碼說(shuō)話(huà)最清楚。
1 class Test 2 { 3 static void Main() 4 { 5 Thread t = new Thread(Run); 6 7 t.Start(); 8 9 //Join相當于把Run方法內嵌如此10 t.Join();11 12 //該死的t.Join(),害的我主線(xiàn)程必須在你執行完后才能執行。13 Console.WriteLine("我是主線(xiàn)程:" + Thread.CurrentThread.GetHashCode());14 }15 16 static void Run()17 {18 //等待5s19 Thread.Sleep(5000);20 21 Console.WriteLine("我是線(xiàn)程:" + Thread.CurrentThread.GetHashCode());22 }23 }

<3> Interrupt和Abort:這兩個(gè)關(guān)鍵字都是用來(lái)強制終止線(xiàn)程,不過(guò)兩者還是有區別的。
① Interrupt: 拋出的是 ThreadInterruptedException 異常。
Abort: 拋出的是 ThreadAbortException 異常。
② Interrupt:如果終止工作線(xiàn)程,只能管到一次,工作線(xiàn)程的下一次sleep就管不到了,相當于一個(gè)
contine操作。
Abort:這個(gè)就是相當于一個(gè)break操作,工作線(xiàn)程徹底死掉。
Interrupt:
1 namespace Test
2 {
3 class Program
4 {
5 static void Main(string[] args)
6 {
7 Thread t = new Thread(new ThreadStart(Run));
8
9 t.Start();
10
11 //阻止動(dòng)作
12 t.Interrupt();
13
14 Console.Read();
15 }
16
17 static void Run()
18 {
19 for (int i = 1; i <= 3; i++)
20 {
21 Stopwatch watch = new Stopwatch();
22
23 try
24 {
25 watch.Start();
26 Thread.Sleep(2000);
27 watch.Stop();
28
29 Console.WriteLine("第{0}延遲執行:{1}ms", i, watch.ElapsedMilliseconds);
30 }
31 catch (ThreadInterruptedException e)
32 {
33 Console.WriteLine("第{0}延遲執行:{1}ms,不過(guò)拋出異常", i, watch.ElapsedMilliseconds);
34 }
35 }
36 }
37 }
38 }

Abort: 工作線(xiàn)程直接退出,不帶走一片云彩。
1 namespace Test 2 { 3 class Program 4 { 5 static void Main(string[] args) 6 { 7 Thread t = new Thread(new ThreadStart(Run)); 8 9 t.Start();10 11 Thread.Sleep(100);12 13 //阻止動(dòng)作14 t.Abort();15 16 Console.Read();17 }18 19 static void Run()20 {21 for (int i = 1; i <= 3; i++)22 {23 Stopwatch watch = new Stopwatch();24 25 try26 {27 watch.Start();28 Thread.Sleep(2000);29 watch.Stop();30 31 Console.WriteLine("第{0}延遲執行:{1}ms", i, watch.ElapsedMilliseconds);32 }33 catch (ThreadAbortException e)34 {35 Console.WriteLine("第{0}延遲執行:{1}ms,不過(guò)拋出異常", i, watch.ElapsedMilliseconds);36 }37 }38 }39 }40 }

二:線(xiàn)程使用場(chǎng)景
可能線(xiàn)程的使用有點(diǎn)類(lèi)似wcf,做一些耗時(shí)但不很及時(shí)的需求,比如可以開(kāi)線(xiàn)程下圖片,連接數據庫等等,當然線(xiàn)程可以用來(lái)做負載,這里就做
一個(gè)小demo,找一個(gè)美女網(wǎng)站,面對如此多的圖片,一個(gè)線(xiàn)程真的吃不消啊,

看了下網(wǎng)站主體上有4個(gè)tab頁(yè),那么我們就開(kāi)4個(gè)線(xiàn)程來(lái)負載
1 class Program
2 {
3 static void Main(string[] args)
4 {
5 string[] str = { "model", "sexy", "belle", "stars" };
6
7 for (int url = 0; url < str.Length; url++)
8 {
9 Thread thread = new Thread(DownLoad);
10
11 thread.Start(str[url]);
12 }
13 Console.Read();
14 }
15
16 public static void DownLoad(object category)
17 {
18 string url = string.Empty;
19
20 for (int purl = 9014; purl > 10; purl--)
21 {
22 for (int pageSize = 0; pageSize < 20; pageSize++)
23 {
24 try
25 {
26 if (pageSize == 0)
27 url = "http://www.mm8mm8.com/" + category + "/" + purl + ".html";
28 else
29 url = "http://www.mm8mm8.com/" + category + "/" + purl + "_" + pageSize + ".html";
30
31 //創(chuàng )建http鏈接
32 var request = (HttpWebRequest)WebRequest.Create(url);
33
34 request.Timeout = 1000 * 5; //5s過(guò)期
35
36 var response = (HttpWebResponse)request.GetResponse();
37
38 Stream stream = response.GetResponseStream();
39
40 StreamReader sr = new StreamReader(stream);
41
42 string content = sr.ReadToEnd();
43
44 var list = GetHtmlImageUrlList(content);
45
46 WebClient client = new WebClient();
47
48 string[] directory = { @"C:\MM\", @"D:\MM\", @"E:\MM\", @"F:\MM\" };
49
50 var directoryName = directory[new Random().Next(0, directory.Length)];
51
52 if (!Directory.Exists(directoryName))
53 Directory.CreateDirectory(directoryName);
54
55 var fileName = string.Empty;
56
57 if (list.Count == 0)
58 {
59 Console.WriteLine("時(shí)間:" + DateTime.Now + " 當前網(wǎng)址:" + url + " 未發(fā)現圖片");
60 break;
61 }
62
63 try
64 {
65
66 fileName = category + "_" + purl + "_" + (pageSize + 1) + ".jpg";
67
68 var localFile = directoryName + fileName;
69
70 var imageRequest = (HttpWebRequest)WebRequest.Create(list[0]);
71
72 imageRequest.Timeout = 1000 * 5; //5s 超時(shí)
73
74 var imageResponse = (HttpWebResponse)imageRequest.GetResponse();
75
76 var s = imageResponse.GetResponseStream();
77
78 Image image = Image.FromStream(s);
79
80 image.Save(localFile);
81
82 image.Dispose();
83
84 Console.WriteLine("時(shí)間:" + DateTime.Now + " 圖片:" + fileName + " 已經(jīng)下載 存入磁盤(pán)位置:" + localFile);
85
86 }
87 catch (Exception e)
88 {
89 Console.WriteLine("時(shí)間:" + DateTime.Now + " 當前圖片:" + fileName + " 錯誤信息:" + e.Message);
90 continue;
91 }
92 }
93 catch (Exception ex)
94 {
95 Console.WriteLine("時(shí)間:" + DateTime.Now + " 當前網(wǎng)址:" + url + " 錯誤信息:" + ex.Message);
96 }
97 }
98 }
99 }
100
101 /// <summary>
102 /// 取得HTML中所有圖片的 URL。
103 /// </summary>
104 /// <param name="sHtmlText">HTML代碼</param>
105 /// <returns>圖片的URL列表</returns>
106 public static List<string> GetHtmlImageUrlList(string sHtmlText)
107 {
108 // 定義正則表達式用來(lái)匹配 img 標簽
109 Regex regImg = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);
110
111 // 搜索匹配的字符串
112 MatchCollection matches = regImg.Matches(sHtmlText);
113
114 List<string> sUrlList = new List<string>();
115
116 // 取得匹配項列表
117 foreach (Match match in matches)
118 sUrlList.Add(match.Groups["imgUrl"].Value);
119 return sUrlList;
120 }
121 }

三:對線(xiàn)程的一些思考
我們知道線(xiàn)程的優(yōu)點(diǎn)還是比較多的,每個(gè)線(xiàn)程都需要默認的堆??臻g,所以說(shuō)線(xiàn)程數受到內存空間大小的限制,如果線(xiàn)程數開(kāi)的太多
反而適得其反,進(jìn)程被分配的時(shí)間片會(huì )被線(xiàn)程分的更細,也就導致了處理器需要更頻繁的在線(xiàn)程之間來(lái)回切換。
聯(lián)系客服