zoukankan      html  css  js  c++  java
  • 转载 多线程实际运用<第七篇>

    1、单线程采集100个页面

    复制代码
        class Program
        {
            static int i = 6991275;
            static void Main(string[] args)
            {
                Stopwatch sw = new Stopwatch();
                sw.Start();
                getTitle();
                sw.Stop();
                Console.WriteLine("采集100个页面完成,用时:" + sw.ElapsedMilliseconds + "毫秒");
    
                Console.ReadKey();
            }
    
            static void getTitle()
            {
                for (int j = 0; j < 100; j++)
                {
                    WebClient wc = new WebClient();
                    wc.BaseAddress = "http://www.juedui100.com/";
                    wc.Encoding = Encoding.UTF8;
                    string html = wc.DownloadString("user/" + ++i + ".html");
                    Regex reg = new Regex(@"<title>(.*)</title>");
                    Console.WriteLine(reg.Match(html));
                }
            }
        }
    复制代码

      输出:

      

    2、多线程采集100个页面

    复制代码
        class Program
        {
            static int i = 6991275;
            static volatile int k = 1;
            static void Main(string[] args)
            {
                Stopwatch sw = new Stopwatch();
                sw.Start();
                for (int i = 0; i < 5; i++)
                {
                    ThreadPool.QueueUserWorkItem(getTitle);
                }
                while (true)
                {
                    if (k == 5)
                    {
                        sw.Stop();
                        break;
                    }
                }
                Console.WriteLine("采集100个页面完成,用时:" + sw.ElapsedMilliseconds + "毫秒");
                Console.ReadKey();
            }
    
            static void getTitle(object o)
            {
                while(i < 6991375)
                {
                    WebClient wc = new WebClient();
                    wc.BaseAddress = "http://www.juedui100.com/";
                    wc.Encoding = Encoding.UTF8;
                    string html = wc.DownloadString("user/" + Interlocked.Increment(ref i) + ".html");
                    Regex reg = new Regex(@"<title>(.*)</title>");
                    Console.WriteLine(reg.Match(html));
                }
                k++;
            }
        }
    复制代码

      输出如下:

      

      单纯从执行时间来看,采集100个页面,用5个线程效率提升2倍多,当然这跟带宽也有关系啦。

      有问题,好像K++多个线程执行的时候有问题,也要Interlocked.Increment。以后再改、

  • 相关阅读:
    Norton我错怪了你啊~~
    RUNRMTCMD命令使用
    如何查看QTEMP的内容?可以查看别人的QTEMP的
    关于文件的ShareODP和USROPN
    虚拟主机权限之log4net
    如何向远程系统提交命令?
    在5250上面实现复制粘贴
    php与数据库对应实体类的命名
    Action Script 中的 super
    Linux下源码编译方式安装MySQL5.5.12(转)
  • 原文地址:https://www.cnblogs.com/Jeely/p/10750744.html
Copyright © 2011-2022 走看看