惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
爱范儿
爱范儿
人人都是产品经理
人人都是产品经理
博客园 - 司徒正美
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
量子位
罗磊的独立博客
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
小众软件
小众软件
C
Cybersecurity and Infrastructure Security Agency CISA
Cyberwarzone
Cyberwarzone
大猫的无限游戏
大猫的无限游戏
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
雷峰网
雷峰网
Simon Willison's Weblog
Simon Willison's Weblog
The Cloudflare Blog
博客园 - 三生石上(FineUI控件)
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Cyber Attacks, Cyber Crime and Cyber Security
博客园_首页
博客园 - 叶小钗
V
Vulnerabilities – Threatpost
T
The Exploit Database - CXSecurity.com
T
Tailwind CSS Blog
IT之家
IT之家
博客园 - 聂微东
Spread Privacy
Spread Privacy
V2EX - 技术
V2EX - 技术
S
Security Affairs
宝玉的分享
宝玉的分享
V
V2EX
C
Cisco Blogs
博客园 - Franky
美团技术团队
酷 壳 – CoolShell
酷 壳 – CoolShell
月光博客
月光博客
S
Securelist
J
Java Code Geeks
Webroot Blog
Webroot Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
P
Proofpoint News Feed
Last Week in AI
Last Week in AI
L
LINUX DO - 热门话题
NISL@THU
NISL@THU
WordPress大学
WordPress大学
W
WeLiveSecurity
T
Threatpost
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
腾讯CDC
阮一峰的网络日志
阮一峰的网络日志

博客园 - Goodspeed

几种常见的函数 Caesar cipher 遗传算法之背包问题 Transport scheme NOT recognized: [stomp] error running git Canvas 旋转的图片 canvas时钟 火箭起飞 让图标转起来 Tomcat启动脚本 Task中的异常处理 Parallel的陷阱 用Task代替TheadPool 使用ThreadPool代替Thread 正确停止线程 线程同步中使用信号量AutoResetEvent C#和.NET Framework的关系 为什么泛型不支持协变性? 可空值类型与值类型这间的转换
异步和多线程的区别
Goodspeed · 2014-11-02 · via 博客园 - Goodspeed

多线程会有一个工作线程,占用更多的CPU。

异步将使用DMA模式的IO操作

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var p = new Program();

            var url = "http://bj.58.com/";

            p.Asynchronous(url);
            p.MultiThread(url);
            Console.ReadKey();
        }

        void Asynchronous(string url)
        {
            var request = HttpWebRequest.Create(url);
            request.BeginGetResponse((IAsyncResult ar) => {
                var request_inner = ar.AsyncState as WebRequest;
                var response = request.EndGetResponse(ar);
                read(response, "Asynchronous");
            }, request);
        }

        void MultiThread(string url)
        {
            var t = new Thread(() =>
            {
                var request = HttpWebRequest.Create(url);
                var response = request.GetResponse();
                read(response, "MultiThread");
            });
            t.Start();
        }

        private static void read(WebResponse response, string funcname)
        {
            var stream = response.GetResponseStream();
            using (var reader = new StreamReader(stream))
            {
                Console.WriteLine("{0} {1}", funcname, reader.ReadToEnd().Length);
            }
        }
    }
}