SlideShare a Scribd company logo
1 of 18
.Net Async
Programming
2015.04.10 Ian
Process
作業系統會配給 Process(處理緒) 一塊記憶體空間,
放著該 Process 所有的程式碼與資料,以及一些
系統服務的紀錄資料。
Thread
執行緒(thread)分配到的可能是位於該
Process 內的一段記憶體空間,紀錄著該執行緒/
工作的程式碼與資料,以及每次微處理機的執行
狀態,而那些屬於全域性的紀錄資料,並不會複
製到每一個執行緒/工作內。
Entry point -> Main Thread ex. Main() 方法
長時間作業,造成…
I/O Bound vs Compute Bound
I/O Bound :等待某個條件發生
Compute Bound:執行 CPU 運算
Synchronous Programming
中文:同步程式設計
有序或循序執行
A -> B -> C
Asynchronous
Programming
中文:非同步程式設計
無序或平行執行
工作流程,執行上的順序不影響結果:
A -> B -> C
A -> C -> B
B -> C -> A
B -> A -> C
C -> A -> B
C -> B -> A
async & await
C# 5.0
Async Method
含 async 修飾詞
含零到多個 await 結果
無 await 運算子只會發出警告,不影響程式執行
回傳 void 、Task、Task<TResult>
.Net Framework 4.5 提供,使用*Async() 結尾的
方法
例如:
同步方法:CopyTo()、Read()
非同步方法:CopyToAsync()、ReadAsync()
Async Improves
Responsiveness
In an asynchronous process, the application can
continue with other work that doesn't depend on
the web resource until the potentially blocking
task finishes.
Application area
Supporting APIs that contain
async methods
Web access HttpClient , SyndicationClient
Working with files
StorageFile, StreamWriter,
StreamReader, XmlReader
Working with images
MediaCapture, BitmapEncoder,
BitmapDecoder
WCF programming
Synchronous and Asynchronous
Operations
async Task<int> AccessTheWebAsync()
{
HttpClient client = new HttpClient();
Task<string> getStringTask =
client.GetStringAsync("http://msdn.microsoft.com");
DoIndependentWork();
// The await operator suspends AccessTheWebAsync.
// - AccessTheWebAsync can't continue until getStringTask is complete.
// - Meanwhile, control returns to the caller of AccessTheWebAsync.
// - Control resumes here when getStringTask is complete.
// - The await operator then retrieves the string result from getStringTask.
string urlContents = await getStringTask;
return urlContents.Length
}
Async methods are intended to be non-blocking
operations. An await expression in an async method
doesn’t block the current thread while the awaited task is
running. Instead, the expression signs up the rest of the
method as a continuation and returns control to the caller
of the async method.
The async and await keywords don't cause additional
threads to be created. Async methods don't require
multithreading because an async method doesn't run on its
own thread. The method runs on the current
synchronization context and uses time on the thread only
when the method is active. You can use Task.Run to move
CPU-bound work to a background thread, but a
background thread doesn't help with a process that's just
waiting for results to become available.
System.Thread.Task
Namespace
Task class (可以以多執行緒執行的非同步作業)
Parallel class
ThreadPool
.NET Framework 會在 IIS 維護一個 ThreadPool,
當IIS 接受到一個要求時就會從 ThreadPool 取出
一個Thread 處理此請求。
.NET Framework 4.5 預設最大 5000
Thread Starvation => HTTP 503 (服務無法使用)
同步與非同步的選擇
同步:
作業簡單
執行時間短暫
作業主要都是 CPU 作業,非需要耗用大量磁碟或
網路的作業。
對受限於 CPU 的作業採用非同步動作並沒有好處,
甚至會增加負擔。
同步與非同步的選擇
非同步:
透過非同步方法呼叫服務,並且使用 .NET
Framework 4.5(含) 以上。
作業上都是 IO Bound 或 Network Bound,而不
是 CPU Bound
平行性比簡單的程式碼重要
切換執行緒的效益大於 context switch
測試顯示出封鎖作業是網站效能的瓶頸或停滯等
待 Web Service,對這些封鎖呼叫採用非同步方
法可讓 IIS 為更多要求提供服務
多執行緒 vs 非同步
Task -> New Thread -> Get Internet resources
只是換一條執行緒來代替原本會被鎖定的 Main
Thread
Async -> ThreadPool -> Get Internet resources
在 ThreadPool 是使用 DMA( Direct Memory
Access)
Reference
http://www.ithome.com.tw/node/74544
https://msdn.microsoft.com/en-
us/library/hh191443.aspx
https://play.google.com/store/books/details/demo
_%E5%B0%8F%E6%9C%B1_%E9%99%B3%E
5%82%B3%E8%88%88_Bruce_%E7%8E%8B%
E8%82%B2%E6%B0%91_Dino_%E9%99%B3
%E4%BB%95%E5%82%91_91_ASP_NET_MV
C_5_%E7%B6%B2%E7%AB%99?id=mQqnBAA
AQBAJ

More Related Content

Viewers also liked

What's Next Replay - AMQP
What's Next Replay - AMQPWhat's Next Replay - AMQP
What's Next Replay - AMQPZenikaOuest
 
Messaging with amqp and rabbitmq
Messaging with amqp and rabbitmqMessaging with amqp and rabbitmq
Messaging with amqp and rabbitmqSelasie Hanson
 
Easy enterprise application integration with RabbitMQ and AMQP
Easy enterprise application integration with RabbitMQ and AMQPEasy enterprise application integration with RabbitMQ and AMQP
Easy enterprise application integration with RabbitMQ and AMQPRabbit MQ
 
Extreme programming (xp) | David Tzemach
Extreme programming (xp) | David TzemachExtreme programming (xp) | David Tzemach
Extreme programming (xp) | David TzemachDavid Tzemach
 
Introduction to AMQP Messaging with RabbitMQ
Introduction to AMQP Messaging with RabbitMQIntroduction to AMQP Messaging with RabbitMQ
Introduction to AMQP Messaging with RabbitMQDmitriy Samovskiy
 
High powered messaging with RabbitMQ
High powered messaging with RabbitMQHigh powered messaging with RabbitMQ
High powered messaging with RabbitMQJames Carr
 
Software Development Life Cycle (SDLC)
Software Development Life Cycle (SDLC)Software Development Life Cycle (SDLC)
Software Development Life Cycle (SDLC)Mohamed Sami El-Tahawy
 
Software Development Life Cycle (SDLC)
Software Development Life Cycle (SDLC)Software Development Life Cycle (SDLC)
Software Development Life Cycle (SDLC)Angelin R
 

Viewers also liked (8)

What's Next Replay - AMQP
What's Next Replay - AMQPWhat's Next Replay - AMQP
What's Next Replay - AMQP
 
Messaging with amqp and rabbitmq
Messaging with amqp and rabbitmqMessaging with amqp and rabbitmq
Messaging with amqp and rabbitmq
 
Easy enterprise application integration with RabbitMQ and AMQP
Easy enterprise application integration with RabbitMQ and AMQPEasy enterprise application integration with RabbitMQ and AMQP
Easy enterprise application integration with RabbitMQ and AMQP
 
Extreme programming (xp) | David Tzemach
Extreme programming (xp) | David TzemachExtreme programming (xp) | David Tzemach
Extreme programming (xp) | David Tzemach
 
Introduction to AMQP Messaging with RabbitMQ
Introduction to AMQP Messaging with RabbitMQIntroduction to AMQP Messaging with RabbitMQ
Introduction to AMQP Messaging with RabbitMQ
 
High powered messaging with RabbitMQ
High powered messaging with RabbitMQHigh powered messaging with RabbitMQ
High powered messaging with RabbitMQ
 
Software Development Life Cycle (SDLC)
Software Development Life Cycle (SDLC)Software Development Life Cycle (SDLC)
Software Development Life Cycle (SDLC)
 
Software Development Life Cycle (SDLC)
Software Development Life Cycle (SDLC)Software Development Life Cycle (SDLC)
Software Development Life Cycle (SDLC)
 

Similar to Ian .net async programming

高性能并发Web服务器实现核心内幕
高性能并发Web服务器实现核心内幕高性能并发Web服务器实现核心内幕
高性能并发Web服务器实现核心内幕ideawu
 
如何使用 Xhprof 分析網站效能 (真實案例2)
如何使用 Xhprof 分析網站效能 (真實案例2)如何使用 Xhprof 分析網站效能 (真實案例2)
如何使用 Xhprof 分析網站效能 (真實案例2)Cyril Wang
 
Java华为面试题
Java华为面试题Java华为面试题
Java华为面试题yiditushe
 
系统性能分析和优化.ppt
系统性能分析和优化.ppt系统性能分析和优化.ppt
系统性能分析和优化.pptFrank Cai
 
Erlang游戏开发
Erlang游戏开发Erlang游戏开发
Erlang游戏开发litaocheng
 
Showinnodbstatus公开
Showinnodbstatus公开Showinnodbstatus公开
Showinnodbstatus公开longxibendi
 
Asp.net 5 新功能與變革
Asp.net 5 新功能與變革Asp.net 5 新功能與變革
Asp.net 5 新功能與變革Gelis Wu
 
Lucene 全文检索实践
Lucene 全文检索实践Lucene 全文检索实践
Lucene 全文检索实践yiditushe
 
Lamp优化实践
Lamp优化实践Lamp优化实践
Lamp优化实践zhliji2
 
J2ee经典学习笔记
J2ee经典学习笔记J2ee经典学习笔记
J2ee经典学习笔记yiditushe
 
lua & ngx_lua 的介绍与应用
lua & ngx_lua 的介绍与应用lua & ngx_lua 的介绍与应用
lua & ngx_lua 的介绍与应用hugo
 
Lamp高性能设计
Lamp高性能设计Lamp高性能设计
Lamp高性能设计锐 张
 
Cassandra运维之道(office2003)
Cassandra运维之道(office2003)Cassandra运维之道(office2003)
Cassandra运维之道(office2003)haiyuan ning
 
MySQL源码分析.01.代码结构与基本流程
MySQL源码分析.01.代码结构与基本流程MySQL源码分析.01.代码结构与基本流程
MySQL源码分析.01.代码结构与基本流程Lixun Peng
 
高性能队列Fqueue的设计和使用实践
高性能队列Fqueue的设计和使用实践高性能队列Fqueue的设计和使用实践
高性能队列Fqueue的设计和使用实践孙立
 
MySQL新技术探索与实践
MySQL新技术探索与实践MySQL新技术探索与实践
MySQL新技术探索与实践Lixun Peng
 
Php Webservers
Php WebserversPhp Webservers
Php Webserverssamon127
 

Similar to Ian .net async programming (20)

高性能并发Web服务器实现核心内幕
高性能并发Web服务器实现核心内幕高性能并发Web服务器实现核心内幕
高性能并发Web服务器实现核心内幕
 
No sql数据库笔谈
No sql数据库笔谈No sql数据库笔谈
No sql数据库笔谈
 
如何使用 Xhprof 分析網站效能 (真實案例2)
如何使用 Xhprof 分析網站效能 (真實案例2)如何使用 Xhprof 分析網站效能 (真實案例2)
如何使用 Xhprof 分析網站效能 (真實案例2)
 
Java华为面试题
Java华为面试题Java华为面试题
Java华为面试题
 
系统性能分析和优化.ppt
系统性能分析和优化.ppt系统性能分析和优化.ppt
系统性能分析和优化.ppt
 
Erlang游戏开发
Erlang游戏开发Erlang游戏开发
Erlang游戏开发
 
Showinnodbstatus公开
Showinnodbstatus公开Showinnodbstatus公开
Showinnodbstatus公开
 
Rsync服务器架设方法.v0.1b
Rsync服务器架设方法.v0.1bRsync服务器架设方法.v0.1b
Rsync服务器架设方法.v0.1b
 
Asp.net 5 新功能與變革
Asp.net 5 新功能與變革Asp.net 5 新功能與變革
Asp.net 5 新功能與變革
 
Lucene 全文检索实践
Lucene 全文检索实践Lucene 全文检索实践
Lucene 全文检索实践
 
Lamp优化实践
Lamp优化实践Lamp优化实践
Lamp优化实践
 
J2ee经典学习笔记
J2ee经典学习笔记J2ee经典学习笔记
J2ee经典学习笔记
 
lua & ngx_lua 的介绍与应用
lua & ngx_lua 的介绍与应用lua & ngx_lua 的介绍与应用
lua & ngx_lua 的介绍与应用
 
Lamp高性能设计
Lamp高性能设计Lamp高性能设计
Lamp高性能设计
 
Cassandra运维之道(office2003)
Cassandra运维之道(office2003)Cassandra运维之道(office2003)
Cassandra运维之道(office2003)
 
MySQL源码分析.01.代码结构与基本流程
MySQL源码分析.01.代码结构与基本流程MySQL源码分析.01.代码结构与基本流程
MySQL源码分析.01.代码结构与基本流程
 
高性能队列Fqueue的设计和使用实践
高性能队列Fqueue的设计和使用实践高性能队列Fqueue的设计和使用实践
高性能队列Fqueue的设计和使用实践
 
MySQL新技术探索与实践
MySQL新技术探索与实践MySQL新技术探索与实践
MySQL新技术探索与实践
 
Php Webservers
Php WebserversPhp Webservers
Php Webservers
 
Php Webservers
Php WebserversPhp Webservers
Php Webservers
 

More from LearningTech

More from LearningTech (20)

vim
vimvim
vim
 
PostCss
PostCssPostCss
PostCss
 
ReactJs
ReactJsReactJs
ReactJs
 
Docker
DockerDocker
Docker
 
Semantic ui
Semantic uiSemantic ui
Semantic ui
 
node.js errors
node.js errorsnode.js errors
node.js errors
 
Process control nodejs
Process control nodejsProcess control nodejs
Process control nodejs
 
Expression tree
Expression treeExpression tree
Expression tree
 
SQL 效能調校
SQL 效能調校SQL 效能調校
SQL 效能調校
 
flexbox report
flexbox reportflexbox report
flexbox report
 
Vic weekly learning_20160504
Vic weekly learning_20160504Vic weekly learning_20160504
Vic weekly learning_20160504
 
Reflection &amp; activator
Reflection &amp; activatorReflection &amp; activator
Reflection &amp; activator
 
Peggy markdown
Peggy markdownPeggy markdown
Peggy markdown
 
Node child process
Node child processNode child process
Node child process
 
20160415ken.lee
20160415ken.lee20160415ken.lee
20160415ken.lee
 
Peggy elasticsearch應用
Peggy elasticsearch應用Peggy elasticsearch應用
Peggy elasticsearch應用
 
Expression tree
Expression treeExpression tree
Expression tree
 
Vic weekly learning_20160325
Vic weekly learning_20160325Vic weekly learning_20160325
Vic weekly learning_20160325
 
D3js learning tips
D3js learning tipsD3js learning tips
D3js learning tips
 
git command
git commandgit command
git command
 

Ian .net async programming

Editor's Notes

  1. IE、Chrome、Firefox
  2. 例如 IE 分頁。 長時間作業:讀寫大文件、連線存取大量資料等,造成沒有回應。 每一執行緒(無論是主執行緒或背景工作執行緒),都是處理縮(Process) 中一個獨立執行單元,它們能夠存取那些共享資料 解決之道:Worker thread (背景工作執行緒)
  3. Css js img 下載 及 Thread.Sleep -> I/O Bound
  4. 在返回呼叫者之前才完成它的工作
  5. 可能返回呼叫者之後才完成它的工作 使用頻率較小 也稱為非鎖定方式
  6. Parallel 為簡化在同步狀態下對 Task 的操作: For ForEach Invoke
  7. DDOS
  8. CPU Bound :適合採用多執行緒 IO Bound:適合採用非同步