读了我这篇文章,你的多线程技术就脱胎换骨啦~~

本文通过实例详细介绍了如何使用多线程技术进行任务并行处理,包括线程创建、线程间同步及结果整合等内容。
 
读了我这篇文章,你的多线程技术就脱胎换骨啦~~嘿嘿
在多线程操作里面,我们经常把把独立的任务放在另外一个线程里处理,比如:炒鸡蛋(用我的生活中的例子哈哈),炒鸡蛋的步骤为
1.从冰箱取鸡蛋
2.关上冰箱
3.在锅里烧油
4.把鸡蛋在碗里捣碎
5.炒鸡蛋
通常做法,我们一般可以把第二步放到另外一个线程里去操作,因为他的操作结果完全不影响整个炒鸡蛋的过程。
那么我们要想把第三步 和 第四步 同事执行,执行完毕后再执行第五步,这样的话,就要用到一个线程同步,那么我们就要用到一个join()等待线程函数,具体怎么用呢
我用一个搜索引擎的例子作为比方:
我个人往往的习惯是把一个线程要调用的方法写到一个类里,例如我的多个线程要完成的工作是去各大搜索引擎上抓取结果。
<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type=text/javascript> </script>

我于是新建了一个线程方法类

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Collections;
  5. namespace ManyThread
  6. {
  7.     public class MyThread
  8.     {
  9.         private string SearhEnginName = string.Empty;
  10.         public string SearhEnginName1
  11.         {
  12.             get { return SearhEnginName; }
  13.             set { SearhEnginName = value; }
  14.         }
  15.         private ArrayList SearchEnginRes = new ArrayList();
  16.         public ArrayList SearchEnginRes1
  17.         {
  18.             get { return SearchEnginRes; }
  19.             set { SearchEnginRes = value; }
  20.         }
  21.         public MyThread(string EnginName)
  22.         {
  23.             this.SearhEnginName = EnginName;
  24.         }
  25.         public void getRes() {
  26.             switch (SearhEnginName)
  27.             {
  28.                 case"百度":
  29.                     getBaidu();
  30.                     break;
  31.                 case"Google":
  32.                     getGoogle();
  33.                     break;
  34.                 case "雅虎":
  35.                     getYaHu();
  36.                     break;
  37.                 default:
  38.                     break;
  39.             }
  40.         }
  41.         private void getBaidu() {
  42.             
  43.             for (int i = 0; i < 1000000; i++)
  44.             {
  45.                 this.SearchEnginRes.Add("baidu:1+" + i);
  46.                 if (i%5==0)
  47.                 {
  48.                     this.SearchEnginRes.Add("baidu:5的倍数");
  49.                 }
  50.             }
  51.         }
  52.         private void getYaHu() {
  53.             for (int i = 0; i < 1000000; i++)
  54.             {
  55.                 this.SearchEnginRes.Add("YaHu:1+" + i);
  56.                 if (i % 5 == 0)
  57.                 {
  58.                     this.SearchEnginRes.Add("YaHu:5的倍数");
  59.                 }
  60.             }
  61.         }
  62.         private void getGoogle() {
  63.             for (int i = 0; i < 1000000; i++)
  64.             {
  65.                 this.SearchEnginRes.Add("YaHu:1+" + i);
  66.                 if (i % 5 == 0)
  67.                 {
  68.                     this.SearchEnginRes.Add("YaHu:5的倍数");
  69.                 }
  70.             }
  71.         }
  72.     }
  73. }

 

然后再main方法去调用这些函数,我这里用了一个join()线程等待工作,他的作用就是要等待这个线程完成所有工作,我new了一个Thread 数组,把几个线程放到数组里面,然后依次启动线程,启动完后,在分别调用每个线程的join方法,意思就是让主线程等待所有线程数组里面的线程执行完毕所有的方法

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading;
  5. using System.Collections;
  6. using System.Diagnostics;
  7. namespace ManyThread
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             MyThread MySearchBaidu = new MyThread("百度");
  14.             MyThread MySearchGoogle = new MyThread("Google");
  15.             MyThread MySearchYaHu = new MyThread("雅虎");
  16.             MyThread[] Search = new MyThread[3];
  17.             Search[0] = MySearchBaidu;
  18.             Search[1] = MySearchGoogle;
  19.             Search[2] = MySearchYaHu;
  20.             Thread [] threadList = new Thread[3];
  21.             Stopwatch temp = new Stopwatch();
  22.             temp.Start();
  23.             //单线程算法Start
  24.             //for (int i = 0; i < Search.Length; i++)
  25.             //{
  26.             //    Search[i].getRes();
  27.             //}
  28.             //单线程算法End
  29.             //多线程算法Start
  30.             for (int i = 0; i < 3; i++)
  31.             {
  32.                 threadList[i] = new Thread(new ThreadStart(Search[i].getRes));
  33.                 threadList[i].Start();
  34.                 Console.WriteLine(Search[i].SearhEnginName1 + "已启动");
  35.             }
  36.             for (int i = 0; i < threadList.Length; i++)
  37.             {
  38.                 threadList[i].Join();
  39.             }
  40.             //多线程算法End
  41.             for (int i = 0; i < Search.Length; i++)
  42.             {
  43.                 Console.WriteLine(Search[i].SearhEnginName1 + ":" + Search[i].SearchEnginRes1.Count);
  44.             }
  45.             temp.Stop();
  46.             Console.WriteLine(" 所用毫秒数:" + temp.Elapsed + "/r/n");
  47.             Console.Read();
  48.         }
  49.     }
  50. }

还有一种方法就是用回调函数,此时暂时不写了,等有时间了再写,呵呵,这个例子如果有什么问题请直接给我留言,或者发Email

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值