![]() 读了我这篇文章,你的多线程技术就脱胎换骨啦~~嘿嘿
在多线程操作里面,我们经常把把独立的任务放在另外一个线程里处理,比如:炒鸡蛋(用我的生活中的例子哈哈),炒鸡蛋的步骤为
1.从冰箱取鸡蛋 2.关上冰箱 3.在锅里烧油 4.把鸡蛋在碗里捣碎 5.炒鸡蛋 通常做法,我们一般可以把第二步放到另外一个线程里去操作,因为他的操作结果完全不影响整个炒鸡蛋的过程。 那么我们要想把第三步 和 第四步 同事执行,执行完毕后再执行第五步,这样的话,就要用到一个线程同步,那么我们就要用到一个join()等待线程函数,具体怎么用呢 我用一个搜索引擎的例子作为比方: 我个人往往的习惯是把一个线程要调用的方法写到一个类里,例如我的多个线程要完成的工作是去各大搜索引擎上抓取结果。 |
<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type=text/javascript>
</script>
我于是新建了一个线程方法类
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Collections;
- namespace ManyThread
- {
- public class MyThread
- {
- private string SearhEnginName = string.Empty;
- public string SearhEnginName1
- {
- get { return SearhEnginName; }
- set { SearhEnginName = value; }
- }
- private ArrayList SearchEnginRes = new ArrayList();
- public ArrayList SearchEnginRes1
- {
- get { return SearchEnginRes; }
- set { SearchEnginRes = value; }
- }
- public MyThread(string EnginName)
- {
- this.SearhEnginName = EnginName;
- }
- public void getRes() {
- switch (SearhEnginName)
- {
- case"百度":
- getBaidu();
- break;
- case"Google":
- getGoogle();
- break;
- case "雅虎":
- getYaHu();
- break;
- default:
- break;
- }
- }
- private void getBaidu() {
- for (int i = 0; i < 1000000; i++)
- {
- this.SearchEnginRes.Add("baidu:1+" + i);
- if (i%5==0)
- {
- this.SearchEnginRes.Add("baidu:5的倍数");
- }
- }
- }
- private void getYaHu() {
- for (int i = 0; i < 1000000; i++)
- {
- this.SearchEnginRes.Add("YaHu:1+" + i);
- if (i % 5 == 0)
- {
- this.SearchEnginRes.Add("YaHu:5的倍数");
- }
- }
- }
- private void getGoogle() {
- for (int i = 0; i < 1000000; i++)
- {
- this.SearchEnginRes.Add("YaHu:1+" + i);
- if (i % 5 == 0)
- {
- this.SearchEnginRes.Add("YaHu:5的倍数");
- }
- }
- }
- }
- }
然后再main方法去调用这些函数,我这里用了一个join()线程等待工作,他的作用就是要等待这个线程完成所有工作,我new了一个Thread 数组,把几个线程放到数组里面,然后依次启动线程,启动完后,在分别调用每个线程的join方法,意思就是让主线程等待所有线程数组里面的线程执行完毕所有的方法
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Threading;
- using System.Collections;
- using System.Diagnostics;
- namespace ManyThread
- {
- class Program
- {
- static void Main(string[] args)
- {
- MyThread MySearchBaidu = new MyThread("百度");
- MyThread MySearchGoogle = new MyThread("Google");
- MyThread MySearchYaHu = new MyThread("雅虎");
- MyThread[] Search = new MyThread[3];
- Search[0] = MySearchBaidu;
- Search[1] = MySearchGoogle;
- Search[2] = MySearchYaHu;
- Thread [] threadList = new Thread[3];
- Stopwatch temp = new Stopwatch();
- temp.Start();
- //单线程算法Start
- //for (int i = 0; i < Search.Length; i++)
- //{
- // Search[i].getRes();
- //}
- //单线程算法End
- //多线程算法Start
- for (int i = 0; i < 3; i++)
- {
- threadList[i] = new Thread(new ThreadStart(Search[i].getRes));
- threadList[i].Start();
- Console.WriteLine(Search[i].SearhEnginName1 + "已启动");
- }
- for (int i = 0; i < threadList.Length; i++)
- {
- threadList[i].Join();
- }
- //多线程算法End
- for (int i = 0; i < Search.Length; i++)
- {
- Console.WriteLine(Search[i].SearhEnginName1 + ":" + Search[i].SearchEnginRes1.Count);
- }
- temp.Stop();
- Console.WriteLine(" 所用毫秒数:" + temp.Elapsed + "/r/n");
- Console.Read();
- }
- }
- }
还有一种方法就是用回调函数,此时暂时不写了,等有时间了再写,呵呵,这个例子如果有什么问题请直接给我留言,或者发Email
本文通过实例详细介绍了如何使用多线程技术进行任务并行处理,包括线程创建、线程间同步及结果整合等内容。


被折叠的 条评论
为什么被折叠?



