Cover by Morgan Skinner, Karli Watson, Jay Glynn, Bill Evjen, Christian Nagel

Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo

Chapter 21

Tasks, Threads, and Synchronization

WHAT’S IN THIS CHAPTER?

  • An overview of multi-threading
  • Working with the Parallel class
  • Tasks
  • Cancellation framework
  • Thread class and thread pools
  • Threading issues
  • Synchronization techniques
  • Timers

WROX.COM CODE DOWNLOADS FOR THIS CHAPTER

The wrox.com code downloads for this chapter are found at http://www.wrox.com/remtitle.cgi?isbn=1118314425 on the Download Code tab. The code for this chapter is divided into the following major examples:

  • Parallel
  • Task
  • Cancellation
  • ThreadClass
  • Synchronization
  • DataFlow

OVERVIEW

There are several reasons for using threading. Suppose that you are making a network call from an application that might take some time. You don’t want to stall the user interface and force the user to wait idly until the response is returned from the server. The user could perform some other actions in the meantime or even cancel the request that was sent to the server. Using threads can help.

For all activities that require a wait — for example, because of file, database, or network access — a new thread can be started to fulfill other tasks at the same time. Even if you have only processing-intensive tasks to do, threading can help. Multiple threads of a single process can run on different CPUs, or, nowadays, on different cores of a multiple-core CPU, at the same time.

You must be aware of some issues when running multiple threads, however. Because they can run during the same time, you can easily get into problems if ...

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required