Concurrency

Let's look at the concept of concurrency using a simple example of a few daily routine tasks and the way we can perform them.

Imagine you start your day and need to get six things done:

  • Make hotel reservation
  • Book flight tickets
  • Order a dress
  • Pay credit card bills
  • Write an email
  • Listen to an audiobook

The order in which they are completed doesn't matter, and for some of the tasks, such as  writing an email or listening to an audiobook, you need not complete them in a single sitting. Here is one possible way to complete the tasks:

  1. Order a dress.
  2. Write one-third of the email.
  3. Make hotel reservation.
  4. Listen to 10 minutes of audiobook.
  5. Pay credit card bills.
  6. Write another one-third of the email.
  7. Book flight tickets.
  8. Listen to another 20 minutes of audiobook.
  9. Complete writing the email.
  10. Continue listening to audiobook until you fall asleep.

In programming terms, we have executed the above tasks concurrently. We had a complete day and we chose particular tasks from our list of tasks and started to work on them. For certain tasks, we even decided to break them up into pieces and work on the pieces between other tasks.

We will eventually write a program which does all of the preceding steps concurrently, but let's take it one step at a time. Let's start by building a program that executes the tasks sequentially, and then modify it progressively until it is purely concurrent code and uses goroutines. The progression of the program will be in three steps:

  1. Serial task execution.
  2. Serial task execution with goroutines.
  3. Concurrent task execution.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.37.20