Prog in C# – Chapter 1.1

Chapter 1.1 – Implementing Multithreading and Asynchronous Processing

More Info

For more information on how the ThreadPool works and how to configure it, see http://msdn.microsoft.com/en-us/library/system.threading.threadpool.aspx.

Exam Tip

When using async and await keep in mind that you should never have a method marked async without and await statements.  You should also avoid returning from an async method except when it’s an event handler.

Concurrent Collections

  • BlockingCollection<T>
  • ConcurrentBag<T>
  • ConcurrentDictionary<TKey,T>
  • ConcurrentQueue<T>
  • ConcurrentStack<T>

Objective Summary

  • A thread  can be seen as a virtualized CPU.
  • Using multiple threads can improve responsiveness and enables you to make use of multiple processors.
  • The Thread class can be used if you want to create your own threads explicitly.  Otherwise, you can use ThreadPool to queue work and let the runtime handle things.
  • A Task object encapsulates a job that needs to be executed.  Tasks are the recommended way to create multithreaded code.
  • The Parallel class can be used to run code in parallel.
  • PLINQ is an extension to LINQ to run queries in parallel.
  • The new async and await operators can be used to write asynchronous code more easily.
  • Concurrent collections can be used to safely work with data in a multithreaded (concurrent access) environment.

Thought Experiment

Implementing multithreading

You need to build a new application, and you look into mutlithreading capabilities.  Your application consists of a client application that communicates with a web server.

Explain how multithreading can help with your client application.

Multithreading can improve the responsiveness in the client application.  The UI thread can process requests from the user while background threads execute other operations.

Incorrect


What is the difference between CPU and I/O bound operations?

A CPU-bound operation needs a thread to execute.  In a client application, it can make sense to execute a CPU-bound operation on another thread to improve responsiveness.  In a server application, you don’t want an extra thread for a CPU-bound operation.  Asynchronous I/O operations don’t require a thread while executing.  Using asynchronous I/O frees the current thread to do other work and improves scalability.

Incorrect

Does using multithreading with the TPL offer the same advantages for your server application?

Using multithreading in a server environment can help you distribute operations over multiple CPUs.  This way, you can improve performance.  Using the TPL to create another thread to execute a CPU-bound operation while the originating thread has to wait for it won’t help you with increasing performance.

Incorrect

You have a lot of items that need to be processed. For each item, you need to perform a complex calculation. Which technique should you use?

Correct
Incorrect

You are creating a complex query that doesn’t require any particular order and you want to run it in parallel. Which method should you use?

Correct
Incorrect

You are working on an ASP.NET application that retrieves some data from another web server and then writes the response to the database. Should you use async/await?

Correct
Incorrect