Tuesday, 13 November 2007

Chapter 7, lesson 3: Asynchronous Programming Model (APM).

  • APM - allowing some portions of code to run on separate threadds.
  • Many classes support APM - supply BeginXXX and EndXXX. e.g. FileStreamBeginRead(), EndRead(). Allows asynch. execution of mthds.
  • EndXXX() - calling this blocks until asynch work is complete.
  • Rendevous Models - informs when to call EndXXX(). 3 models - Wait-Until-Done, polling, callback.
  • wait-until-done:
  • 1) strm.BeginRead(buffer,0,buffer.Length,null,null); - null for last 2 args as not Callback.
  • polling:
  • 1) while (!result.IsCompleted){//do work here Thread.Sleep(100);}
  • callback model:
  • 1) Create new AsyncCallback delegate (on another thread) when operation is complete - strmBeginRead(buffer,o,buffer.Length,new AsyncCallback(CompleteRead),strm);
  • ThreadPool - built-in, and can be used in many situations where might expect to to create own threads.
  • 1) WaitCallback workItem = new WaitCallback(WorkWithParameter);
  • 2) ThreadPool.QueueUserWorkItem()
  • ThreadPool - faster - threads reused + saves expensive setup costs. As threads become available, the thread pool posts new work to the thread.
  • change threa pool thread limits - 1) thread starvation, 2) startup thread speed.

No comments: