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

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.
主站蜘蛛池模板: 霍邱县| 长岭县| 白玉县| 介休市| 科技| 青铜峡市| 德惠市| 池州市| 景谷| 石泉县| 阿克陶县| 陇西县| 邓州市| 松江区| 乐至县| 望都县| 湟中县| 卫辉市| 海城市| 开阳县| 水城县| 大埔区| 鱼台县| 柯坪县| 长春市| 兴业县| 桃园县| 灵璧县| 绵竹市| 桓仁| 雅江县| 织金县| 尼木县| 辉南县| 松溪县| 南澳县| 大邑县| 稻城县| 秭归县| 罗定市| 双桥区|