SlideShare ist ein Scribd-Unternehmen logo
1 von 75
Downloaden Sie, um offline zu lesen
Async and parallel patterns and application design - TechDays2013 NL
Async and parallel patterns and
application design
Arie Leeuwesteijn
Technology Specialist Macaw
arie@macaw.nl
(@arieleeuw)
In this Session…
•   Threads and Tasks
•   Task Parallel Library
•   C# 5.0 async await
•   Concurrent Collection Classes
•   Reactive Extensions
•   TPL Dataflow
•   ASP.NET, MVC, Win8
Objectives
• Understanding Async and Parallelism

• Patterns, Frameworks and tools
  – Purpose
  – Scenarios
  – Combinations and Integrations
  – Real world examples
Why Async and Parallel?
• Multicore is the rule
   – Keep your cores busy
   – Used them in an efficient way
• Client
   – User experience
• Server
   – Scalability
   – Handling requests
• Real life is async and parallel
Question
• What’s the difference between Async
  and Parallel?

• Parallel : Having the work done by multiple
  workers at the same time
• Async: Use workers efficiently, don’t let them
  wait for slow actions to complete
History
• .NET 1.0
   – Asynchronous Programming Model, APM
• .NET 2.0
   – Event-Based Programming Model, EPM
• .NET 4.0
   – Task-based Programming Model, TAP
• .NET 4.5
   – C# 5.0, async, await(*)
(*) Async Targeting Pack for Visual Studio 2012 to target .NET 4.0
Asynchronous Programming Model
WebRequest client = WebRequest.CreateHttp(url);

client.BeginGetResponse(Callback, client);

private void Callback(IAsyncResult ar)
{
  WebRequest client =(WebRequest)ar.AsyncState;
  WebResponse resp = client.EndGetResponse(ar);
}
Asynchronous Programming Model
• BeginMethod, EndMethod pattern

• Complex code
  – Error handling
  – Object lifetime
  – Nesting async calls

• Synchronization context
Event-based Programming
Model = new WebClient();
var client

client.DownloadStringCompleted
   += DownloadCompleted;

private void DownloadCompleted(
   object sender,
   DownloadStringCompletedEventArgs e) {
   string resp = e.Result;
}

client.DownloadStringAsync(uri);
Event-based Programming
Model and EventHandlers
• Events

• Complex code
  – Error handling
  – Inconsistent synchronization context

• Possible Memory leaks
  – += handler keeps reference
  – Dangling handlers
Tasks
• Task and Task<T>
  – A promise to do something or return a value
  – Does not say when or how

• Task != Thread
  – A task is something you want to be done
  – A thread is a possible worker to perform that task
  – A task may not even need a thread
Why use Tasks
• Simplifies code
• One object to access to get the
  – Result, Status and possible Errors
• Composition
  – ContinueWith, WhenAll, WhenAny
• Task Cancelation
Creating a Task
// Using the Task constructor
var task1 = new Task<int>(() => {
   Thread.Sleep(1000);
   return 42;
});                           This is the work to be done

task1.Start();
var res = task1.Result;

// Using the TaskFactory
var task2 = new TaskFactory<int>().StartNew(() => {
   Thread.Sleep(1000);
   return 42;
});
TaskScheduler
• Handles low level queuing of Tasks to Threads
• Uses lightweight TreadPool
  – Number of cores
  – Default scheduler proviced by .NET
  – TaskScheduler.FromCurrentSynchronizationContext()
• Can specify alternative scheduler for task to run
  on
  – Task.Start()
TaskCreationOptions
• Task creation options to hint
  TaskScheduler
  – AttachedToParent
  – HideScheduler
  – LongRunning
  – PreferFairness
  – A few more..
Task Scheduler
                                                            LongRunning
                                                            PreferFairness
                                                            Work Stealing Task option
                                                            Local Queue Task option
            Global Queue (FIFO)
                                                            Run created by work outside
                                                            Task on standalone taskif task in
                                                            Idle threads by tasks placed in
                                                            Taskscreatedstealrunning no tasks in
                                                            Instead global
                                                            global queue Queue
                                                            local Queue (LIFO)
                                                            Local or of threadpool


                                               Threadpool
 Worker        Worker             Worker     Worker
 Thread 1      Thread 2           Thread 3   Thread n




                                                                    = Running Task
                                                                    = Queued Task
Outer task is executed by
Using PreferFairness                          thread from threadpool
Task<int> outer = Task<int>.Factory.StartNew( () => {
                                            Creates 2 more inner
   var cts = new CancellationTokenSource(); tasks, queued in local
   var token = cts.Token;                      task LIFO queue
   var tasks = new Task<int>[]{
       Task.Factory.StartNew(() => { methodA(token); }, token),
     Task.Factory.StartNew(() => { { methodB(token); }, token)
       Task.Factory.StartNew(() => methodA(token); }, token,
   };        TaskCreationOptions.PreferFairness),
    Task.Factory.StartNew(() => { methodB(token); }, token),
   var winnerIndex = Task.WaitAny(tasks);
             TaskCreationOptions.PreferFairness),,
   cts.Cancel();
                                             On a busy system one of
   return tasks[winnerIndex].Result;
});
                                               the tasks might never
return outer.Result;                         execute (no workstealing
                                                  by other thread)
ConcurrentExclusiveSchedulerPair
• Provides task schedulers that coordinate
  to execute tasks
• Scheduler properties
  – ConcurrentScheduler
     • Schedule tasks to this pair that may run
       concurrently with other tasks on this pair
  – ExclusiveScheduler
     • Schedule tasks to this pair that must run
       exclusively with regards to other tasks on this pair
SchedulerPairs
var schedPair =
       new ConcurrentExclusiveSchedulerPair();

// Tasks on this scheduler may run concurrently
readerTask.Start(schedPair.ConcurrentScheduler);

// Tasks on this scheduler run exclusivly
writerTask.Start(schedPair.ExclusiveScheduler);
How to Cancel a Task
• Tasks does not have Stop Method
• CancelationTokenSource
• -Create CancelationToken and pass to Task
   – Signal a Task to Stop
   – Task should check token for cancelation
   – Return or Throw Exception
• Additional options for timed cancellations etc.
• Can also be used for (timed) Wait actions
Cancelling a Task
var tokenSource2 = new CancellationTokenSource();
var ct = tokenSource2.Token;

var task = Task.Factory.StartNew(() => {
   while (true) {
       if (ct.IsCancellationRequested)
          ct.ThrowIfCancellationRequested();
       // Do Work
     }
 }, ct );

tokenSource2.Cancel();
Error and Exception Handling
• Exceptions occurred in execution of
  task thrown when accessing the Result
• System.AggregateException
  – InnerExceptions
 try {
    var result = task.Result;
 }
 catch( AggregateException ex )
 {
 foreach (var e in ex.InnerExceptions){
    // Exception handling here
 }
Task Parallel Library
• Task
• Parallel
  – Parallel.For, Parallel.ForEach, Invoke
• PLINQ
  – Parallel extensions for LINQ
  – AsParallel()
• TPL Dataflow
Stopping Parallel Loops
var cancellationSource = new CancellationTokenSource();     Using C#
var options = new ParallelOptions();                         Break
options.CancellationToken = cancellationSource.Token;
                                                              Using
Parallel.For(0, 10, options, (a, loopState) =>              loopstate
{
   . . . .
  // cancellationToken.Cancel can be called externally
  // or Break can be called directly if condition is true
    if (cancellationToken.IsCancellationRequested)
       loopState.Break(); // loopState.Stop();
   . . . .
});                 Throws OperationCanceledException
Parallel Options for Loops
• CancelationToken
  – Stopping all Threads in the loop
• MaxDegreeOfParallelism
  – If you need more or less Threads
• TaskScheduler
PLINQ
var query =
  (from d in data
   where …
   select d).AsParallel();

query.ForAll((d) => DoWork(d));
Concurent collections
• Set of thread safe collection classes
• System.Collections.Concurrent
  – ConcurrentBag<T>
  – ConcurrentDictionary<TKey, Tvalue>
  – ConcurrentQueue<T>
  – ConcurrentStack<T>
ConcurrentDictionary
var dict = new ConcurrentDictionary<string, string>();

var key =   "key01";
var value = "value01";

// Get a value
dict.GetOrAdd(key, k => GetValue(key));

// Update or add a value
value = "newvalue";
dict.AddOrUpdate(key, value, (k, o) => value);
C# 5.0
• New keywords for async
  async marks method or lambda
  expression as async
  await suspends execution of method until
  the awaited tasks returns
• Compiler does generate all the code
  to make this happen
C# 5.0
public int LongRunningMethod()
{
   Task.Delay(1000); Task to return
                Wait for
   var result = Control 2;returned to caller
                 21 * is
   return result;
}
public async Task<int> LongRunningMethodAsync()
{
   await Task.Delay(1000);
   var result = 21 * 2;
   return result;                 This code runs when Task returns
}
Run
The story of two Tasks (1)
await DoAsync("Task-1");
await DoAsync("Task-2");

Async but not parallel
Run
The story of two Tasks (2)
var t1 = DoAsync("Task-1");
var t2 = DoAsync("Task-2");

Parallel but returns immediately
Run
The story of two Tasks (3)
var t1 = DoAsync("Task-1");
var t2 = DoAsync("Task-2");
await t1;
await t2;

Parallel and wait for both tasks to return
The story of two Tasks (4)
var t1 = DoAsync("Task-1");
var t2 = DoAsync("Task-2");

await Task.WhenAll(t1,t2)

Parallel and wait for both tasks to return
But more efficient
Where await can’t be used
• catch and finally blocks
• lock blocks
• LINQ query expressions
  – Allowed in extension methods (. syntax)
• Unsafe code
How does await work
• Does the C# compiler depends on .NET Task class
  for await keyword ?
• Compiler needs GetAwaiter() method
• Method must return a class that implements
  – GetResult()
  – IsCompleted()
  – INotifyCompleted.OnCompleted
• Implemented in Task
Demo
• Implementing an awaitable class
  – See what the compiler needs for await
  – Just for demo purposes
Make something async using
Task
• Simply create a new Task to do the work
public static async Task<int> MyAsync()
{
   var result = await new Task<int>(() =>
   {
       // your long running code code here...
       var calculatedResult = LongCalculation();
       return calculatedResult;
   });
    return result;
}
Tasks without Threads
• Task construction always take code to
  execute on threadpool
• But what if you…
  –   Already have a thread
  –   Don’t need a thread
  –   Waiting for async IO, network etc
  –   Handle events
TaskCompletionSource
• Create Task objects that don't execute
  code.
var tcs = new TaskCompletionSource<int>();
return tcs.Task;
Demo await anything
• What you’ll see..
  – await oneSecond;

  – await techDays;

  – await DayOfWeek.Saturday;

  – return await Process.Start("HelloWorld.exe");
Task Interop patterns
 • Why
   – Unified Task-based programming model
   – Composition

 • Asynchronous to Task-based application model
   – Factory.TaskFromAsync

 • Event-basedto Task-based application model
   – TaskCompletionSource
Async to Task-based
WebRequest wr =
     WebRequest.CreateHttp(url);

var t = Task<Stream>.Factory.FromAsync(
           wr.BeginGetRequestStream,
           wr.EndGetRequestStream,
           null
        );
Event to Tasks-based (1)
public static Task<string> DownloadStringAsync(Uri url){

    var wc = new WebClient();
    var tcs = new TaskCompletionSource<string>();

    wc.DownloadStringCompleted += (s,e) =>{
       if (e.Error != null ) tcs.TrySetException(e.Error);
       else if (e.Cancelled) tcs.TrySetCanceled();
       else
         tcs.TrySetResult(e.Result)
    }
                                          Task complete
    wc.DownloadStringAsync(url);
    return tcs.Task;
}
Event to Task-based (2)
private Task<string> LoadDocumentAsync(
WebBrowser browser, string url)
{
   var tcs = new TaskCompletionSource<string>();
   WebBrowserDocumentCompletedEventHandler handler = null;
                      Event source lifetime
    handler = (sender, managed externally
                       e) =>
    {
         tcs.SetResult(browser.DocumentText);
         browser.DocumentCompleted -= handler;
    };

    browser.DocumentCompleted += handler;
    browser.Url = new Uri(url);        Need to unregister
    return tcs.Task;                       handler
}
Using Lazy<T> with async
• Lazy<T> is thread safe and supports async
// sync version
   async version
var lazy = new=Lazy<string>(() =>
    lazyAsync   new Lazy<Task<string>>(async () =>   {
{
   Thread.Sleep(5000);
   await Task.Delay(5000);            Factory code
   return DateTime.Now.ToString();
});
});
var value = lazy.Value;
var value = await lazyAsync.Value;
Using Lazy<T> with async
var lazyAsync = new Lazy<Task<string>>(async () =>
{
    Console.WriteLine("I'm only called once...");
    await Task.Delay(5000);
    return DateTime.Now.ToString();
});

// Starts and wait for task
var t1 = lazyAsync.Value;
// Waits for same task
var t2 = lazyAsync.Value;
Building an async cache
public class AsyncCache<TKey, TValue>{

    private Func<TKey,Task<TValue>> factory;
    private ConcurrentDictionary<TKey,Lazy<Task<TValue>>> dict;
var cache = new AsyncCache<string, string>( async k =>
 public AsyncCache(Func<TKey, Task<TValue>> valueFactory){
   {
   factory = valueFactory;
   dictwait Task.Delay(1000);
        = new ConcurrentDictionary<TKey, Lazy<Task<TValue>>>();
 }
       return DateTime.Now.ToString();
   });
 public Task<TValue> this[TKey key] {
   get {
     return dict.GetOrAdd(key,
var t = await cache["key1"];
        toAdd => new Lazy<Task<TValue>>(() => factory(toAdd))).Value; }
   }

}
SynchronizationContext
• Used when code needs specific context
  – e.g. updating UI
  – SynchronizationContext class is abstract
  – Static Current property returns relevant
    instance (Post is specific)
  Ensures one at a time   Dispatcher.BeginInvoke   Control.BeginInvoke
SynchronizationContext
private void Button1Click(object sender,EventArgs e)
{
   var ctx = SynchronizationContext.Current;
   new TaskFactory().StartNew(() => {
     ctx.Post(state => {
        label1.Text = string.Format("Update from Task")
     }, null});
}

• await Task continues automatically on
  Current context
Windows8
• WinRT is based on COM
• Async based on interfaces like this
  – IAsyncAction
  – IAsyncOperation<T>
• Task nor CLR do not implement these
  interfaces
Win8
• How can we call these new asynch methods from
  .NET or use Tasks in Win8 applications?
• WindowsRuntimeSystemExtensions methods take
  care of conversions
 public static Task AsTask<TResult>(
   this IAsyncOperation<TResult> source);

 public static IAsyncOperation<TResult>
 AsAsyncOperation<TResult>(this Task<TResult> source);
Async and ASP.NET
• Async controller methods
  – Don’t block your IIS worker threads
  – Avoid 503 (Server too busy) errors
• MVC4/.NET4.5 supports async/await
  – Possible in MVC3, but more complex
  – WebForms also have support for async
• Always use async proxies when calling
  external services
Async in MVC3
public class MVC3Controller : AsyncController{

    public void IndexAsync(){
       AsyncManager.OutstandingOperations.Increment(1);
       Task.Factory.StartNew(() =>{
          var client = new WebClient();
          string reply = client.DownloadString(uri);
          AsyncManager.Parameters["data"] = reply;
          AsyncManager.OutstandingOperations.Decrement();});
    }

    public ActionResult IndexCompleted(string data){
      this.ViewBag.Result = data;
      return View();
    }
}
Async in MVC4
public class MVC4Controller : Controller
{
   public async Task<ActionResult> Index() {
      var client = new WebClient();
      this.ViewBag.Result =
            await client.DownloadStringTaskAsync(uri);
      return View();
   }
}
Async service proxies
Parallel programming models
• .NET 4.0
  – Here’s the data, now setup computing
  – Primitives for Tasks and Data parallelism
• The Inverse model
  – Setup the Computing, now here’s the data
  – Primitives for Dataflow parallelism
Enumerables and
Observables
                IEnumerator<T>                IEnumerable<T>                 IObserver<T>                      IObservable<T>



Current{get;}               GetEnumerator()                     OnNext(T)                  IDisposable
                                                                                     Subscribe(IObserver<T>)
MoveNext()                                                     OnError(Ex)

   Reset()                                                     OnCompled()




    Iterations, pull model                                       Subscription, pushmodel
Reactive Extensions and TPL
Dataflow
• Reactive Extension (Rx)
   – Coordination and composition of event streams
   – LINQ-based API

• Dataflow (TDF)
   – Building blocks for message passing and parallelizing
   – Explicit control over how data is buffered and moved

• Many similarities, but each address distinct needs
Reactive Extensions Rx
• Library for composing asynchronous
  and event-based programs using
  observable sequences and LINQ-style
  query operators.
  – Rx for .NET, Rx.NET
  – Rx for JavaScript, RxJS
  – Rx for Windows Phone
Reactive Extensions
var observable1 = Observable.Range(1, 20);
var subscription1 = observable1.Subscribe<int>(Console.WriteLine);

var oneSecond = TimeSpan.FromSeconds(1);
var observable2 = Observable.Timer(oneSecond,oneSecond);
var subscription2 = observable2.Subscribe<int>(Console.WriteLine);

var observer3 = observable1
  .Select((i) => i)
  .Skip(2)                           // skip first two values
  .Where(i => (i % 2 == 0))          // only get the even values
  .Zip(observable2, (i, t) => i)     // one value per second
  .Subscribe((i) => Console.WriteLine(i));
Reactive Extensions
var mouseMove = Observable
  .FromEventPattern<MouseEventHandler, MouseEventArgs>(
     h => this.MouseMove += h, h => this.MouseMove -= h);

var mouseUp = Observable
   .FromEvent<MouseEventHandler, MouseEventArgs>(
      h => this.MouseUp += h, h => this.MouseUp -= h);

var mouseDown = Observable
   .FromEvent<MouseEventHandler, MouseEventArgs>(
      h => this.MouseDown += h, h => this.MouseDown -= h);

var observable = mouseMove      // Get mousemove positions
   .SkipUntil(mouseDown)        // Skip until mouse button down
   .TakeUntil(mouseUp)          // Take until mouse button is up
   .Select(a => a.EventArgs.Location);
Reactive Extensions
• Many operators
  – Skip, Take, Zip, Throttle, Buffer, Repeat…
• ObserveOn() and SubscribeOn()
  methods
  – Optional Scheduler and Context
    parameters
  – Specify which thread/context the observer
    and subscribers run on
TPL Dataflow
• Primitives for in-process message/data
  passing
  – Blocks for buffering and processing data
• Linkable to form a network
  – Data automatically propagated from sources to
    linked targets
  – Enables building powerful parallel and
    asynchronous pipelinesBased
• Integrates with Task, IObservable,…
Executor Blocks
Buffering Blocks
Join Blocks
Building a Dataflow network
                            transform1.LinkTo(join.Target1);
                            transform2.LinkTo(join.Target2);




input.LinkTo(action1);          join.LinkTo(action2);
input.LinkTo(transform1);
input.LinkTo(transform2);
ActionBlock in action

var actionBlock = new ActionBlock<int>((i) =>
  {
   Console.WriteLine("[{0}]t{1}",
     Thread.CurrentThread.ManagedThreadId,i);
  }
);
for (var i = 0; i < 10; i++)
   actionBlock.Post(i);
ActionBlock in action


var actionBlock = new ActionBlock<int>((i) =>
  {
    Console.WriteLine("[{0}]t{1}",
       Thread.CurrentThread.ManagedThreadId,i);
  },
  new ExecutionDataflowBlockOptions() {
     MaxDegreeOfParallelism = 4 }
);
                                              Max 4 instances
for (var i = 0; i < 10; i++)                     running
   actionBlock.Post(i);
Linking Blocks


var actionBlock = new ActionBlock<int>((i) => Console.WriteLine(i));

var transformBlock = new TransformBlock<int, int>((i) => i * i);

transformBlock.LinkTo(actionBlock);

for (var i = 0; i < 10; i++)
   transformBlock.Post(i);
Buffering


var actionBlock = new ActionBlock<int>((i) => Console.WriteLine(i));

var bufferBlock = new BufferBlock<int>(new DataflowBlockOptions(
   { BoundedCapacity = 10 }
);
                                        Post blocks if buffer is full
bufferBlock.LinkTo(actionBlock);
How about JavaScript?
• Use async in the browser
  – Reactive Extensions for JavaScript
  – jQuery Defered and Promises
Thank you…

Arie Leeuwesteijn
arie@macaw.nl
@arieleeuw
http://tinyurl.com/c78tn5j

Weitere ähnliche Inhalte

Was ist angesagt?

Os7 2
Os7 2Os7 2
Os7 2issbp
 
Distributed computing with Ray. Find your hyper-parameters, speed up your Pan...
Distributed computing with Ray. Find your hyper-parameters, speed up your Pan...Distributed computing with Ray. Find your hyper-parameters, speed up your Pan...
Distributed computing with Ray. Find your hyper-parameters, speed up your Pan...Jan Margeta
 
Hands-on Learning with KubeFlow + Keras/TensorFlow 2.0 + TF Extended (TFX) + ...
Hands-on Learning with KubeFlow + Keras/TensorFlow 2.0 + TF Extended (TFX) + ...Hands-on Learning with KubeFlow + Keras/TensorFlow 2.0 + TF Extended (TFX) + ...
Hands-on Learning with KubeFlow + Keras/TensorFlow 2.0 + TF Extended (TFX) + ...Chris Fregly
 
Towards JVM Dynamic Languages Toolchain
Towards JVM Dynamic Languages ToolchainTowards JVM Dynamic Languages Toolchain
Towards JVM Dynamic Languages ToolchainAttila Szegedi
 
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple SpacesConcurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spacesluccastera
 
Message-passing concurrency in Python
Message-passing concurrency in PythonMessage-passing concurrency in Python
Message-passing concurrency in PythonSarah Mount
 
Peyton jones-2011-parallel haskell-the_future
Peyton jones-2011-parallel haskell-the_futurePeyton jones-2011-parallel haskell-the_future
Peyton jones-2011-parallel haskell-the_futureTakayuki Muranushi
 
Simon Peyton Jones: Managing parallelism
Simon Peyton Jones: Managing parallelismSimon Peyton Jones: Managing parallelism
Simon Peyton Jones: Managing parallelismSkills Matter
 
Mission planning of autonomous quadrotors
Mission planning of autonomous quadrotorsMission planning of autonomous quadrotors
Mission planning of autonomous quadrotorsIvano Malavolta
 
Presentation on Shared Memory Parallel Programming
Presentation on Shared Memory Parallel ProgrammingPresentation on Shared Memory Parallel Programming
Presentation on Shared Memory Parallel ProgrammingVengada Karthik Rangaraju
 
Erlang
ErlangErlang
ErlangESUG
 
Java Performance Tuning
Java Performance TuningJava Performance Tuning
Java Performance TuningMinh Hoang
 
Understanding Android Benchmarks
Understanding Android BenchmarksUnderstanding Android Benchmarks
Understanding Android BenchmarksKoan-Sin Tan
 

Was ist angesagt? (20)

Os7 2
Os7 2Os7 2
Os7 2
 
Distributed computing with Ray. Find your hyper-parameters, speed up your Pan...
Distributed computing with Ray. Find your hyper-parameters, speed up your Pan...Distributed computing with Ray. Find your hyper-parameters, speed up your Pan...
Distributed computing with Ray. Find your hyper-parameters, speed up your Pan...
 
Hands-on Learning with KubeFlow + Keras/TensorFlow 2.0 + TF Extended (TFX) + ...
Hands-on Learning with KubeFlow + Keras/TensorFlow 2.0 + TF Extended (TFX) + ...Hands-on Learning with KubeFlow + Keras/TensorFlow 2.0 + TF Extended (TFX) + ...
Hands-on Learning with KubeFlow + Keras/TensorFlow 2.0 + TF Extended (TFX) + ...
 
Towards JVM Dynamic Languages Toolchain
Towards JVM Dynamic Languages ToolchainTowards JVM Dynamic Languages Toolchain
Towards JVM Dynamic Languages Toolchain
 
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple SpacesConcurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
 
Message-passing concurrency in Python
Message-passing concurrency in PythonMessage-passing concurrency in Python
Message-passing concurrency in Python
 
Lecture6
Lecture6Lecture6
Lecture6
 
Openmp
OpenmpOpenmp
Openmp
 
Peyton jones-2011-parallel haskell-the_future
Peyton jones-2011-parallel haskell-the_futurePeyton jones-2011-parallel haskell-the_future
Peyton jones-2011-parallel haskell-the_future
 
Simon Peyton Jones: Managing parallelism
Simon Peyton Jones: Managing parallelismSimon Peyton Jones: Managing parallelism
Simon Peyton Jones: Managing parallelism
 
Java Performance Tuning
Java Performance TuningJava Performance Tuning
Java Performance Tuning
 
Mission planning of autonomous quadrotors
Mission planning of autonomous quadrotorsMission planning of autonomous quadrotors
Mission planning of autonomous quadrotors
 
Presentation on Shared Memory Parallel Programming
Presentation on Shared Memory Parallel ProgrammingPresentation on Shared Memory Parallel Programming
Presentation on Shared Memory Parallel Programming
 
Erlang
ErlangErlang
Erlang
 
Mkl mic lab_0
Mkl mic lab_0Mkl mic lab_0
Mkl mic lab_0
 
Introduction to OpenMP
Introduction to OpenMPIntroduction to OpenMP
Introduction to OpenMP
 
OpenMp
OpenMpOpenMp
OpenMp
 
Java Performance Tuning
Java Performance TuningJava Performance Tuning
Java Performance Tuning
 
Understanding Android Benchmarks
Understanding Android BenchmarksUnderstanding Android Benchmarks
Understanding Android Benchmarks
 
Java multi thread programming on cmp system
Java multi thread programming on cmp systemJava multi thread programming on cmp system
Java multi thread programming on cmp system
 

Andere mochten auch

Async best practices DotNet Conference 2016
Async best practices DotNet Conference 2016 Async best practices DotNet Conference 2016
Async best practices DotNet Conference 2016 Lluis Franco
 
Threads c sharp
Threads c sharpThreads c sharp
Threads c sharpDeivaa
 
No More Deadlocks; Asynchronous Programming in .NET
No More Deadlocks; Asynchronous Programming in .NETNo More Deadlocks; Asynchronous Programming in .NET
No More Deadlocks; Asynchronous Programming in .NETFilip Ekberg
 
Async Programming with C#5: Basics and Pitfalls
Async Programming with C#5: Basics and PitfallsAsync Programming with C#5: Basics and Pitfalls
Async Programming with C#5: Basics and PitfallsEastBanc Tachnologies
 
Asynchronous programming
Asynchronous programmingAsynchronous programming
Asynchronous programmingFilip Ekberg
 
Asynchronous programming in ASP.NET
Asynchronous programming in ASP.NETAsynchronous programming in ASP.NET
Asynchronous programming in ASP.NETAlex Thissen
 

Andere mochten auch (6)

Async best practices DotNet Conference 2016
Async best practices DotNet Conference 2016 Async best practices DotNet Conference 2016
Async best practices DotNet Conference 2016
 
Threads c sharp
Threads c sharpThreads c sharp
Threads c sharp
 
No More Deadlocks; Asynchronous Programming in .NET
No More Deadlocks; Asynchronous Programming in .NETNo More Deadlocks; Asynchronous Programming in .NET
No More Deadlocks; Asynchronous Programming in .NET
 
Async Programming with C#5: Basics and Pitfalls
Async Programming with C#5: Basics and PitfallsAsync Programming with C#5: Basics and Pitfalls
Async Programming with C#5: Basics and Pitfalls
 
Asynchronous programming
Asynchronous programmingAsynchronous programming
Asynchronous programming
 
Asynchronous programming in ASP.NET
Asynchronous programming in ASP.NETAsynchronous programming in ASP.NET
Asynchronous programming in ASP.NET
 

Ähnlich wie Async and parallel patterns and application design - TechDays2013 NL

.NET Multithreading/Multitasking
.NET Multithreading/Multitasking.NET Multithreading/Multitasking
.NET Multithreading/MultitaskingSasha Kravchuk
 
Groovy concurrency
Groovy concurrencyGroovy concurrency
Groovy concurrencyAlex Miller
 
Parallel Programming With Dot Net
Parallel Programming With Dot NetParallel Programming With Dot Net
Parallel Programming With Dot NetNeeraj Kaushik
 
Concurrent Programming in Java
Concurrent Programming in JavaConcurrent Programming in Java
Concurrent Programming in JavaRuben Inoto Soto
 
Concurrency and Thread-Safe Data Processing in Background Tasks
Concurrency and Thread-Safe Data Processing in Background TasksConcurrency and Thread-Safe Data Processing in Background Tasks
Concurrency and Thread-Safe Data Processing in Background TasksWO Community
 
Real Time Operating System Concepts
Real Time Operating System ConceptsReal Time Operating System Concepts
Real Time Operating System ConceptsSanjiv Malik
 
.NET Core Summer event 2019 in Brno, CZ - Async demystified -- Karel Zikmund
.NET Core Summer event 2019 in Brno, CZ - Async demystified -- Karel Zikmund.NET Core Summer event 2019 in Brno, CZ - Async demystified -- Karel Zikmund
.NET Core Summer event 2019 in Brno, CZ - Async demystified -- Karel ZikmundKarel Zikmund
 
Multi core programming 1
Multi core programming 1Multi core programming 1
Multi core programming 1Robin Aggarwal
 
Effective java item 80 and 81
Effective java   item 80 and 81Effective java   item 80 and 81
Effective java item 80 and 81Isaac Liao
 
Parallel and Async Programming With C#
Parallel and Async Programming With C#Parallel and Async Programming With C#
Parallel and Async Programming With C#Rainer Stropek
 
Parallel programming patterns (UA)
Parallel programming patterns (UA)Parallel programming patterns (UA)
Parallel programming patterns (UA)Oleksandr Pavlyshak
 
Parallel programming patterns - Олександр Павлишак
Parallel programming patterns - Олександр ПавлишакParallel programming patterns - Олександр Павлишак
Parallel programming patterns - Олександр ПавлишакIgor Bronovskyy
 
Asynchronous Orchestration DSL on squbs
Asynchronous Orchestration DSL on squbsAsynchronous Orchestration DSL on squbs
Asynchronous Orchestration DSL on squbsAnil Gursel
 
Continuous Application with FAIR Scheduler with Robert Xue
Continuous Application with FAIR Scheduler with Robert XueContinuous Application with FAIR Scheduler with Robert Xue
Continuous Application with FAIR Scheduler with Robert XueDatabricks
 
NDC Sydney 2019 - Async Demystified -- Karel Zikmund
NDC Sydney 2019 - Async Demystified -- Karel ZikmundNDC Sydney 2019 - Async Demystified -- Karel Zikmund
NDC Sydney 2019 - Async Demystified -- Karel ZikmundKarel Zikmund
 
Parallel programming
Parallel programmingParallel programming
Parallel programmingSwain Loda
 
.Net Multithreading and Parallelization
.Net Multithreading and Parallelization.Net Multithreading and Parallelization
.Net Multithreading and ParallelizationDmitri Nesteruk
 
Functional Programming in Javascript - IL Tech Talks week
Functional Programming in Javascript - IL Tech Talks weekFunctional Programming in Javascript - IL Tech Talks week
Functional Programming in Javascript - IL Tech Talks weekyoavrubin
 
Aplicações assíncronas no Android com
Coroutines & Jetpack
Aplicações assíncronas no Android com
Coroutines & JetpackAplicações assíncronas no Android com
Coroutines & Jetpack
Aplicações assíncronas no Android com
Coroutines & JetpackNelson Glauber Leal
 

Ähnlich wie Async and parallel patterns and application design - TechDays2013 NL (20)

.NET Multithreading/Multitasking
.NET Multithreading/Multitasking.NET Multithreading/Multitasking
.NET Multithreading/Multitasking
 
Groovy concurrency
Groovy concurrencyGroovy concurrency
Groovy concurrency
 
Parallel Programming With Dot Net
Parallel Programming With Dot NetParallel Programming With Dot Net
Parallel Programming With Dot Net
 
Concurrent Programming in Java
Concurrent Programming in JavaConcurrent Programming in Java
Concurrent Programming in Java
 
Concurrency and Thread-Safe Data Processing in Background Tasks
Concurrency and Thread-Safe Data Processing in Background TasksConcurrency and Thread-Safe Data Processing in Background Tasks
Concurrency and Thread-Safe Data Processing in Background Tasks
 
Real Time Operating System Concepts
Real Time Operating System ConceptsReal Time Operating System Concepts
Real Time Operating System Concepts
 
.NET Core Summer event 2019 in Brno, CZ - Async demystified -- Karel Zikmund
.NET Core Summer event 2019 in Brno, CZ - Async demystified -- Karel Zikmund.NET Core Summer event 2019 in Brno, CZ - Async demystified -- Karel Zikmund
.NET Core Summer event 2019 in Brno, CZ - Async demystified -- Karel Zikmund
 
Multi core programming 1
Multi core programming 1Multi core programming 1
Multi core programming 1
 
Effective java item 80 and 81
Effective java   item 80 and 81Effective java   item 80 and 81
Effective java item 80 and 81
 
Parallel and Async Programming With C#
Parallel and Async Programming With C#Parallel and Async Programming With C#
Parallel and Async Programming With C#
 
Parallel programming patterns (UA)
Parallel programming patterns (UA)Parallel programming patterns (UA)
Parallel programming patterns (UA)
 
Parallel programming patterns - Олександр Павлишак
Parallel programming patterns - Олександр ПавлишакParallel programming patterns - Олександр Павлишак
Parallel programming patterns - Олександр Павлишак
 
Parallel Programming
Parallel ProgrammingParallel Programming
Parallel Programming
 
Asynchronous Orchestration DSL on squbs
Asynchronous Orchestration DSL on squbsAsynchronous Orchestration DSL on squbs
Asynchronous Orchestration DSL on squbs
 
Continuous Application with FAIR Scheduler with Robert Xue
Continuous Application with FAIR Scheduler with Robert XueContinuous Application with FAIR Scheduler with Robert Xue
Continuous Application with FAIR Scheduler with Robert Xue
 
NDC Sydney 2019 - Async Demystified -- Karel Zikmund
NDC Sydney 2019 - Async Demystified -- Karel ZikmundNDC Sydney 2019 - Async Demystified -- Karel Zikmund
NDC Sydney 2019 - Async Demystified -- Karel Zikmund
 
Parallel programming
Parallel programmingParallel programming
Parallel programming
 
.Net Multithreading and Parallelization
.Net Multithreading and Parallelization.Net Multithreading and Parallelization
.Net Multithreading and Parallelization
 
Functional Programming in Javascript - IL Tech Talks week
Functional Programming in Javascript - IL Tech Talks weekFunctional Programming in Javascript - IL Tech Talks week
Functional Programming in Javascript - IL Tech Talks week
 
Aplicações assíncronas no Android com
Coroutines & Jetpack
Aplicações assíncronas no Android com
Coroutines & JetpackAplicações assíncronas no Android com
Coroutines & Jetpack
Aplicações assíncronas no Android com
Coroutines & Jetpack
 

Kürzlich hochgeladen

Bird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystemBird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystemAsko Soukka
 
Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1DianaGray10
 
20230202 - Introduction to tis-py
20230202 - Introduction to tis-py20230202 - Introduction to tis-py
20230202 - Introduction to tis-pyJamie (Taka) Wang
 
COMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a WebsiteCOMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a Websitedgelyza
 
UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7DianaGray10
 
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1DianaGray10
 
OpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability AdventureOpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability AdventureEric D. Schabell
 
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...DianaGray10
 
UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8DianaGray10
 
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfIaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfDaniel Santiago Silva Capera
 
Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.YounusS2
 
9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding Team9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding TeamAdam Moalla
 
Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Adtran
 
Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )Brian Pichman
 
Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024SkyPlanner
 
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IES VE
 
Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)Commit University
 
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDEADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDELiveplex
 
Designing A Time bound resource download URL
Designing A Time bound resource download URLDesigning A Time bound resource download URL
Designing A Time bound resource download URLRuncy Oommen
 

Kürzlich hochgeladen (20)

Bird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystemBird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystem
 
Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1
 
20230202 - Introduction to tis-py
20230202 - Introduction to tis-py20230202 - Introduction to tis-py
20230202 - Introduction to tis-py
 
COMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a WebsiteCOMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a Website
 
UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7
 
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
 
OpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability AdventureOpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability Adventure
 
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
 
UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8
 
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfIaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
 
Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.
 
9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding Team9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding Team
 
Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™
 
Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )
 
Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024
 
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
 
201610817 - edge part1
201610817 - edge part1201610817 - edge part1
201610817 - edge part1
 
Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)
 
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDEADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
 
Designing A Time bound resource download URL
Designing A Time bound resource download URLDesigning A Time bound resource download URL
Designing A Time bound resource download URL
 

Async and parallel patterns and application design - TechDays2013 NL

  • 2. Async and parallel patterns and application design Arie Leeuwesteijn Technology Specialist Macaw arie@macaw.nl (@arieleeuw)
  • 3. In this Session… • Threads and Tasks • Task Parallel Library • C# 5.0 async await • Concurrent Collection Classes • Reactive Extensions • TPL Dataflow • ASP.NET, MVC, Win8
  • 4. Objectives • Understanding Async and Parallelism • Patterns, Frameworks and tools – Purpose – Scenarios – Combinations and Integrations – Real world examples
  • 5. Why Async and Parallel? • Multicore is the rule – Keep your cores busy – Used them in an efficient way • Client – User experience • Server – Scalability – Handling requests • Real life is async and parallel
  • 6. Question • What’s the difference between Async and Parallel? • Parallel : Having the work done by multiple workers at the same time • Async: Use workers efficiently, don’t let them wait for slow actions to complete
  • 7. History • .NET 1.0 – Asynchronous Programming Model, APM • .NET 2.0 – Event-Based Programming Model, EPM • .NET 4.0 – Task-based Programming Model, TAP • .NET 4.5 – C# 5.0, async, await(*) (*) Async Targeting Pack for Visual Studio 2012 to target .NET 4.0
  • 8. Asynchronous Programming Model WebRequest client = WebRequest.CreateHttp(url); client.BeginGetResponse(Callback, client); private void Callback(IAsyncResult ar) { WebRequest client =(WebRequest)ar.AsyncState; WebResponse resp = client.EndGetResponse(ar); }
  • 9. Asynchronous Programming Model • BeginMethod, EndMethod pattern • Complex code – Error handling – Object lifetime – Nesting async calls • Synchronization context
  • 10. Event-based Programming Model = new WebClient(); var client client.DownloadStringCompleted += DownloadCompleted; private void DownloadCompleted( object sender, DownloadStringCompletedEventArgs e) { string resp = e.Result; } client.DownloadStringAsync(uri);
  • 11. Event-based Programming Model and EventHandlers • Events • Complex code – Error handling – Inconsistent synchronization context • Possible Memory leaks – += handler keeps reference – Dangling handlers
  • 12. Tasks • Task and Task<T> – A promise to do something or return a value – Does not say when or how • Task != Thread – A task is something you want to be done – A thread is a possible worker to perform that task – A task may not even need a thread
  • 13. Why use Tasks • Simplifies code • One object to access to get the – Result, Status and possible Errors • Composition – ContinueWith, WhenAll, WhenAny • Task Cancelation
  • 14. Creating a Task // Using the Task constructor var task1 = new Task<int>(() => { Thread.Sleep(1000); return 42; }); This is the work to be done task1.Start(); var res = task1.Result; // Using the TaskFactory var task2 = new TaskFactory<int>().StartNew(() => { Thread.Sleep(1000); return 42; });
  • 15. TaskScheduler • Handles low level queuing of Tasks to Threads • Uses lightweight TreadPool – Number of cores – Default scheduler proviced by .NET – TaskScheduler.FromCurrentSynchronizationContext() • Can specify alternative scheduler for task to run on – Task.Start()
  • 16. TaskCreationOptions • Task creation options to hint TaskScheduler – AttachedToParent – HideScheduler – LongRunning – PreferFairness – A few more..
  • 17. Task Scheduler LongRunning PreferFairness Work Stealing Task option Local Queue Task option Global Queue (FIFO) Run created by work outside Task on standalone taskif task in Idle threads by tasks placed in Taskscreatedstealrunning no tasks in Instead global global queue Queue local Queue (LIFO) Local or of threadpool Threadpool Worker Worker Worker Worker Thread 1 Thread 2 Thread 3 Thread n = Running Task = Queued Task
  • 18. Outer task is executed by Using PreferFairness thread from threadpool Task<int> outer = Task<int>.Factory.StartNew( () => { Creates 2 more inner var cts = new CancellationTokenSource(); tasks, queued in local var token = cts.Token; task LIFO queue var tasks = new Task<int>[]{ Task.Factory.StartNew(() => { methodA(token); }, token), Task.Factory.StartNew(() => { { methodB(token); }, token) Task.Factory.StartNew(() => methodA(token); }, token, }; TaskCreationOptions.PreferFairness), Task.Factory.StartNew(() => { methodB(token); }, token), var winnerIndex = Task.WaitAny(tasks); TaskCreationOptions.PreferFairness),, cts.Cancel(); On a busy system one of return tasks[winnerIndex].Result; }); the tasks might never return outer.Result; execute (no workstealing by other thread)
  • 19. ConcurrentExclusiveSchedulerPair • Provides task schedulers that coordinate to execute tasks • Scheduler properties – ConcurrentScheduler • Schedule tasks to this pair that may run concurrently with other tasks on this pair – ExclusiveScheduler • Schedule tasks to this pair that must run exclusively with regards to other tasks on this pair
  • 20. SchedulerPairs var schedPair = new ConcurrentExclusiveSchedulerPair(); // Tasks on this scheduler may run concurrently readerTask.Start(schedPair.ConcurrentScheduler); // Tasks on this scheduler run exclusivly writerTask.Start(schedPair.ExclusiveScheduler);
  • 21. How to Cancel a Task • Tasks does not have Stop Method • CancelationTokenSource • -Create CancelationToken and pass to Task – Signal a Task to Stop – Task should check token for cancelation – Return or Throw Exception • Additional options for timed cancellations etc. • Can also be used for (timed) Wait actions
  • 22. Cancelling a Task var tokenSource2 = new CancellationTokenSource(); var ct = tokenSource2.Token; var task = Task.Factory.StartNew(() => { while (true) { if (ct.IsCancellationRequested) ct.ThrowIfCancellationRequested(); // Do Work } }, ct ); tokenSource2.Cancel();
  • 23. Error and Exception Handling • Exceptions occurred in execution of task thrown when accessing the Result • System.AggregateException – InnerExceptions try { var result = task.Result; } catch( AggregateException ex ) { foreach (var e in ex.InnerExceptions){ // Exception handling here }
  • 24. Task Parallel Library • Task • Parallel – Parallel.For, Parallel.ForEach, Invoke • PLINQ – Parallel extensions for LINQ – AsParallel() • TPL Dataflow
  • 25. Stopping Parallel Loops var cancellationSource = new CancellationTokenSource(); Using C# var options = new ParallelOptions(); Break options.CancellationToken = cancellationSource.Token; Using Parallel.For(0, 10, options, (a, loopState) => loopstate { . . . . // cancellationToken.Cancel can be called externally // or Break can be called directly if condition is true if (cancellationToken.IsCancellationRequested) loopState.Break(); // loopState.Stop(); . . . . }); Throws OperationCanceledException
  • 26. Parallel Options for Loops • CancelationToken – Stopping all Threads in the loop • MaxDegreeOfParallelism – If you need more or less Threads • TaskScheduler
  • 27. PLINQ var query = (from d in data where … select d).AsParallel(); query.ForAll((d) => DoWork(d));
  • 28. Concurent collections • Set of thread safe collection classes • System.Collections.Concurrent – ConcurrentBag<T> – ConcurrentDictionary<TKey, Tvalue> – ConcurrentQueue<T> – ConcurrentStack<T>
  • 29. ConcurrentDictionary var dict = new ConcurrentDictionary<string, string>(); var key = "key01"; var value = "value01"; // Get a value dict.GetOrAdd(key, k => GetValue(key)); // Update or add a value value = "newvalue"; dict.AddOrUpdate(key, value, (k, o) => value);
  • 30. C# 5.0 • New keywords for async async marks method or lambda expression as async await suspends execution of method until the awaited tasks returns • Compiler does generate all the code to make this happen
  • 31. C# 5.0 public int LongRunningMethod() { Task.Delay(1000); Task to return Wait for var result = Control 2;returned to caller 21 * is return result; } public async Task<int> LongRunningMethodAsync() { await Task.Delay(1000); var result = 21 * 2; return result; This code runs when Task returns }
  • 32. Run The story of two Tasks (1) await DoAsync("Task-1"); await DoAsync("Task-2"); Async but not parallel
  • 33. Run The story of two Tasks (2) var t1 = DoAsync("Task-1"); var t2 = DoAsync("Task-2"); Parallel but returns immediately
  • 34. Run The story of two Tasks (3) var t1 = DoAsync("Task-1"); var t2 = DoAsync("Task-2"); await t1; await t2; Parallel and wait for both tasks to return
  • 35. The story of two Tasks (4) var t1 = DoAsync("Task-1"); var t2 = DoAsync("Task-2"); await Task.WhenAll(t1,t2) Parallel and wait for both tasks to return But more efficient
  • 36. Where await can’t be used • catch and finally blocks • lock blocks • LINQ query expressions – Allowed in extension methods (. syntax) • Unsafe code
  • 37. How does await work • Does the C# compiler depends on .NET Task class for await keyword ? • Compiler needs GetAwaiter() method • Method must return a class that implements – GetResult() – IsCompleted() – INotifyCompleted.OnCompleted • Implemented in Task
  • 38. Demo • Implementing an awaitable class – See what the compiler needs for await – Just for demo purposes
  • 39. Make something async using Task • Simply create a new Task to do the work public static async Task<int> MyAsync() { var result = await new Task<int>(() => { // your long running code code here... var calculatedResult = LongCalculation(); return calculatedResult; }); return result; }
  • 40. Tasks without Threads • Task construction always take code to execute on threadpool • But what if you… – Already have a thread – Don’t need a thread – Waiting for async IO, network etc – Handle events
  • 41. TaskCompletionSource • Create Task objects that don't execute code. var tcs = new TaskCompletionSource<int>(); return tcs.Task;
  • 42. Demo await anything • What you’ll see.. – await oneSecond; – await techDays; – await DayOfWeek.Saturday; – return await Process.Start("HelloWorld.exe");
  • 43. Task Interop patterns • Why – Unified Task-based programming model – Composition • Asynchronous to Task-based application model – Factory.TaskFromAsync • Event-basedto Task-based application model – TaskCompletionSource
  • 44. Async to Task-based WebRequest wr = WebRequest.CreateHttp(url); var t = Task<Stream>.Factory.FromAsync( wr.BeginGetRequestStream, wr.EndGetRequestStream, null );
  • 45. Event to Tasks-based (1) public static Task<string> DownloadStringAsync(Uri url){ var wc = new WebClient(); var tcs = new TaskCompletionSource<string>(); wc.DownloadStringCompleted += (s,e) =>{ if (e.Error != null ) tcs.TrySetException(e.Error); else if (e.Cancelled) tcs.TrySetCanceled(); else tcs.TrySetResult(e.Result) } Task complete wc.DownloadStringAsync(url); return tcs.Task; }
  • 46. Event to Task-based (2) private Task<string> LoadDocumentAsync( WebBrowser browser, string url) { var tcs = new TaskCompletionSource<string>(); WebBrowserDocumentCompletedEventHandler handler = null; Event source lifetime handler = (sender, managed externally e) => { tcs.SetResult(browser.DocumentText); browser.DocumentCompleted -= handler; }; browser.DocumentCompleted += handler; browser.Url = new Uri(url); Need to unregister return tcs.Task; handler }
  • 47. Using Lazy<T> with async • Lazy<T> is thread safe and supports async // sync version async version var lazy = new=Lazy<string>(() => lazyAsync new Lazy<Task<string>>(async () => { { Thread.Sleep(5000); await Task.Delay(5000); Factory code return DateTime.Now.ToString(); }); }); var value = lazy.Value; var value = await lazyAsync.Value;
  • 48. Using Lazy<T> with async var lazyAsync = new Lazy<Task<string>>(async () => { Console.WriteLine("I'm only called once..."); await Task.Delay(5000); return DateTime.Now.ToString(); }); // Starts and wait for task var t1 = lazyAsync.Value; // Waits for same task var t2 = lazyAsync.Value;
  • 49. Building an async cache public class AsyncCache<TKey, TValue>{ private Func<TKey,Task<TValue>> factory; private ConcurrentDictionary<TKey,Lazy<Task<TValue>>> dict; var cache = new AsyncCache<string, string>( async k => public AsyncCache(Func<TKey, Task<TValue>> valueFactory){ { factory = valueFactory; dictwait Task.Delay(1000); = new ConcurrentDictionary<TKey, Lazy<Task<TValue>>>(); } return DateTime.Now.ToString(); }); public Task<TValue> this[TKey key] { get { return dict.GetOrAdd(key, var t = await cache["key1"]; toAdd => new Lazy<Task<TValue>>(() => factory(toAdd))).Value; } } }
  • 50. SynchronizationContext • Used when code needs specific context – e.g. updating UI – SynchronizationContext class is abstract – Static Current property returns relevant instance (Post is specific) Ensures one at a time Dispatcher.BeginInvoke Control.BeginInvoke
  • 51. SynchronizationContext private void Button1Click(object sender,EventArgs e) { var ctx = SynchronizationContext.Current; new TaskFactory().StartNew(() => { ctx.Post(state => { label1.Text = string.Format("Update from Task") }, null}); } • await Task continues automatically on Current context
  • 52. Windows8 • WinRT is based on COM • Async based on interfaces like this – IAsyncAction – IAsyncOperation<T> • Task nor CLR do not implement these interfaces
  • 53. Win8 • How can we call these new asynch methods from .NET or use Tasks in Win8 applications? • WindowsRuntimeSystemExtensions methods take care of conversions public static Task AsTask<TResult>( this IAsyncOperation<TResult> source); public static IAsyncOperation<TResult> AsAsyncOperation<TResult>(this Task<TResult> source);
  • 54. Async and ASP.NET • Async controller methods – Don’t block your IIS worker threads – Avoid 503 (Server too busy) errors • MVC4/.NET4.5 supports async/await – Possible in MVC3, but more complex – WebForms also have support for async • Always use async proxies when calling external services
  • 55. Async in MVC3 public class MVC3Controller : AsyncController{ public void IndexAsync(){ AsyncManager.OutstandingOperations.Increment(1); Task.Factory.StartNew(() =>{ var client = new WebClient(); string reply = client.DownloadString(uri); AsyncManager.Parameters["data"] = reply; AsyncManager.OutstandingOperations.Decrement();}); } public ActionResult IndexCompleted(string data){ this.ViewBag.Result = data; return View(); } }
  • 56. Async in MVC4 public class MVC4Controller : Controller { public async Task<ActionResult> Index() { var client = new WebClient(); this.ViewBag.Result = await client.DownloadStringTaskAsync(uri); return View(); } }
  • 58. Parallel programming models • .NET 4.0 – Here’s the data, now setup computing – Primitives for Tasks and Data parallelism • The Inverse model – Setup the Computing, now here’s the data – Primitives for Dataflow parallelism
  • 59. Enumerables and Observables IEnumerator<T> IEnumerable<T> IObserver<T> IObservable<T> Current{get;} GetEnumerator() OnNext(T) IDisposable Subscribe(IObserver<T>) MoveNext() OnError(Ex) Reset() OnCompled() Iterations, pull model Subscription, pushmodel
  • 60. Reactive Extensions and TPL Dataflow • Reactive Extension (Rx) – Coordination and composition of event streams – LINQ-based API • Dataflow (TDF) – Building blocks for message passing and parallelizing – Explicit control over how data is buffered and moved • Many similarities, but each address distinct needs
  • 61. Reactive Extensions Rx • Library for composing asynchronous and event-based programs using observable sequences and LINQ-style query operators. – Rx for .NET, Rx.NET – Rx for JavaScript, RxJS – Rx for Windows Phone
  • 62. Reactive Extensions var observable1 = Observable.Range(1, 20); var subscription1 = observable1.Subscribe<int>(Console.WriteLine); var oneSecond = TimeSpan.FromSeconds(1); var observable2 = Observable.Timer(oneSecond,oneSecond); var subscription2 = observable2.Subscribe<int>(Console.WriteLine); var observer3 = observable1 .Select((i) => i) .Skip(2) // skip first two values .Where(i => (i % 2 == 0)) // only get the even values .Zip(observable2, (i, t) => i) // one value per second .Subscribe((i) => Console.WriteLine(i));
  • 63. Reactive Extensions var mouseMove = Observable .FromEventPattern<MouseEventHandler, MouseEventArgs>( h => this.MouseMove += h, h => this.MouseMove -= h); var mouseUp = Observable .FromEvent<MouseEventHandler, MouseEventArgs>( h => this.MouseUp += h, h => this.MouseUp -= h); var mouseDown = Observable .FromEvent<MouseEventHandler, MouseEventArgs>( h => this.MouseDown += h, h => this.MouseDown -= h); var observable = mouseMove // Get mousemove positions .SkipUntil(mouseDown) // Skip until mouse button down .TakeUntil(mouseUp) // Take until mouse button is up .Select(a => a.EventArgs.Location);
  • 64. Reactive Extensions • Many operators – Skip, Take, Zip, Throttle, Buffer, Repeat… • ObserveOn() and SubscribeOn() methods – Optional Scheduler and Context parameters – Specify which thread/context the observer and subscribers run on
  • 65. TPL Dataflow • Primitives for in-process message/data passing – Blocks for buffering and processing data • Linkable to form a network – Data automatically propagated from sources to linked targets – Enables building powerful parallel and asynchronous pipelinesBased • Integrates with Task, IObservable,…
  • 69. Building a Dataflow network transform1.LinkTo(join.Target1); transform2.LinkTo(join.Target2); input.LinkTo(action1); join.LinkTo(action2); input.LinkTo(transform1); input.LinkTo(transform2);
  • 70. ActionBlock in action var actionBlock = new ActionBlock<int>((i) => { Console.WriteLine("[{0}]t{1}", Thread.CurrentThread.ManagedThreadId,i); } ); for (var i = 0; i < 10; i++) actionBlock.Post(i);
  • 71. ActionBlock in action var actionBlock = new ActionBlock<int>((i) => { Console.WriteLine("[{0}]t{1}", Thread.CurrentThread.ManagedThreadId,i); }, new ExecutionDataflowBlockOptions() { MaxDegreeOfParallelism = 4 } ); Max 4 instances for (var i = 0; i < 10; i++) running actionBlock.Post(i);
  • 72. Linking Blocks var actionBlock = new ActionBlock<int>((i) => Console.WriteLine(i)); var transformBlock = new TransformBlock<int, int>((i) => i * i); transformBlock.LinkTo(actionBlock); for (var i = 0; i < 10; i++) transformBlock.Post(i);
  • 73. Buffering var actionBlock = new ActionBlock<int>((i) => Console.WriteLine(i)); var bufferBlock = new BufferBlock<int>(new DataflowBlockOptions( { BoundedCapacity = 10 } ); Post blocks if buffer is full bufferBlock.LinkTo(actionBlock);
  • 74. How about JavaScript? • Use async in the browser – Reactive Extensions for JavaScript – jQuery Defered and Promises