官术网_书友最值得收藏!

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.
主站蜘蛛池模板: 永济市| 纳雍县| 五莲县| 丰镇市| 满洲里市| 西藏| 清苑县| 石渠县| 砀山县| 塔城市| 廉江市| 西青区| 石楼县| 承德市| 久治县| 汶上县| 齐齐哈尔市| 上高县| 库车县| 穆棱市| 西华县| 互助| 鄄城县| 闻喜县| 新余市| 西宁市| 星子县| 城固县| 北宁市| 泸溪县| 宁蒗| 泸溪县| 余庆县| 临夏市| 乐亭县| 五家渠市| 江都市| 巴中市| 中卫市| 长兴县| 普宁市|