Presentation is loading. Please wait.

Presentation is loading. Please wait.

Joe Hummel, PhD Technical Staff: Pluralsight Adjunct Professor: UIC, LUC

Similar presentations


Presentation on theme: "Joe Hummel, PhD Technical Staff: Pluralsight Adjunct Professor: UIC, LUC"— Presentation transcript:

1 Joe Hummel, PhD joe@joehummel.net Technical Staff: Pluralsight Adjunct Professor: UIC, LUC http://www.joehummel.net/downloads.html

2  Motivation  Execution model  Parallel programming with Tasks  Parallel programming with Async / Await  Demos 2

3 3  Async programming:  Better responsiveness…  GUIs (desktop, web, mobile)  Cloud  Windows 8  Parallel programming:  Better performance…  Engineering  Oil and Gas  Pharma  Science  Social media Disk and network I/O tasks number crunching and big data processing

4 4 C C C C C C C C Main thread C Main > if… while… Main > if… while… Work Stmt1; Stmt2; Stmt3; Work Stmt1; Stmt2; Stmt3; Main > if… while… Main > if… while… Work1 Stmt1; Stmt2; Stmt3; Work1 Stmt1; Stmt2; Stmt3; Work2 Stmt4; Stmt5; Stmt6; Work2 Stmt4; Stmt5; Stmt6; Worker thread Main thread Threads share, run asynchr onously Threads run in parallel  Single core:  Multicore:

5  Threads(.NET 1.0)  Async Delegates  QueueUserWorkItem  BackgroundWorker  Task Parallel Library(.NET 4.0)  Async / Await(.NET 4.5) 5 Easier…

6  Mandelbrot set… 6

7  Programming model based on concept of a Task 7 Task == a unit of work; an object denoting an ongoing operation or computation.

8 8 Windows Process (.NET) App Domain App Domain App Domain App Domain App Domain App Domain.NET Thread Pool worker thread worker thread worker thread worker thread Parallel.For(... ); task global work queue Task Parallel Library Resource Manager Task Scheduler Windows

9  Asian options financial modeling… 9

10 10 void button1_Click(…) { var uictx = // grab UI thread context to run UI task: TaskScheduler.FromCurrentSynchronizationContext(); Task.Factory.StartNew(()=> { return DoLongLatencyOp(); } ).ContinueWith((antecedent) => { lstBox.Items.Add(antecedent.Result); }, uictx // execute this task on UI thread: ); } void button1_Click(…) { var uictx = // grab UI thread context to run UI task: TaskScheduler.FromCurrentSynchronizationContext(); Task.Factory.StartNew(()=> { return DoLongLatencyOp(); } ).ContinueWith((antecedent) => { lstBox.Items.Add(antecedent.Result); }, uictx // execute this task on UI thread: ); } void button1_Click(…) { var result = DoLongLatencyOp(); lstBox.Items.Add(result); } void button1_Click(…) { var result = DoLongLatencyOp(); lstBox.Items.Add(result); }

11 11 void button1_Click(…) { var result = DoLongLatencyOp(); lstBox.Items.Add(result); } void button1_Click(…) { var result = DoLongLatencyOp(); lstBox.Items.Add(result); } async void button1_Click(…) { var result = await Task.Run( () => DoLongRunningOp()); lstBox.Items.Add(result); } async void button1_Click(…) { var result = await Task.Run( () => DoLongRunningOp()); lstBox.Items.Add(result); } Tells compiler that method *may* perform an async, long-latency op Tells compiler to execute operation but don’t wait. Instead, start op as a separate task, and setup a continuation to execute the remaining code when op finishes ― RUNNING ON THE SAME THREAD CONTEXT!

12  For operations that may involve long latency ◦ File and network I/O are the classic use-case  For chunks of work you want to run in parallel ◦ And you have multi-core hardware 12

13  Hides the complexities of async programming ◦ No visible callback, predictable exception handling, …  Think chunky, not chatty ◦ i.e. designed for coarse-grain work / long-latency operations  Designed to be used with APM pattern ◦ Asynchronous Programming Model  APIs that offer async calls via APM? ◦ File I/O ◦ Network I/O ◦ Windows 8 API 13

14  Async web calls are a classic use-case 14 private byte[] GetURLContents(string url) { var content = new MemoryStream(); var webReq = (HttpWebRequest)WebRequest.Create(url); using (WebResponse response = webReq.GetResponse()) { using (Stream responseStream = response.GetResponseStream()) { responseStream.CopyTo(content); } return content.ToArray(); } private byte[] GetURLContents(string url) { var content = new MemoryStream(); var webReq = (HttpWebRequest)WebRequest.Create(url); using (WebResponse response = webReq.GetResponse()) { using (Stream responseStream = response.GetResponseStream()) { responseStream.CopyTo(content); } return content.ToArray(); } Synchronous Version

15  Asynchronous web requests… ◦ Work bottom-up changing sync calls to async calls ◦ Add await, async, and Task or Task as needed 15 private async Task GetURLContentsAsync(string url) { var content = new MemoryStream(); var webReq = (HttpWebRequest)WebRequest.Create(url); using (WebResponse response = await webReq.GetResponseAsync()) { using (Stream responseStream = response.GetResponseStream()) { await responseStream.CopyToAsync(content); } return content.ToArray(); } private async Task GetURLContentsAsync(string url) { var content = new MemoryStream(); var webReq = (HttpWebRequest)WebRequest.Create(url); using (WebResponse response = await webReq.GetResponseAsync()) { using (Stream responseStream = response.GetResponseStream()) { await responseStream.CopyToAsync(content); } return content.ToArray(); } Asynchronous Version

16 16

17  Thread-based execution model at very bottom  Task-based execution model on top  For Performance: ◦ Prefer Task Parallel Library  For Responsiveness: ◦ Prefer Async / Await 17

18  Presenter: Joe Hummel ◦ Email: joe@joehummel.net ◦ Materials: http://www.joehummel.net/downloads.html  For more info: ◦ MSDN Magazine, October 2011 (3 articles): 1.“Easier Asynchronous Programming with the New Visual Studio Async CTP” 2.“Pause and Play with Await” 3.“Async Performance: Understanding the Costs of Async and Await” 18


Download ppt "Joe Hummel, PhD Technical Staff: Pluralsight Adjunct Professor: UIC, LUC"

Similar presentations


Ads by Google