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

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.
主站蜘蛛池模板: 南汇区| 汝阳县| 平塘县| 营口市| 奈曼旗| 阳高县| 腾冲县| 蓝山县| 海安县| 福海县| 通海县| 灌南县| 科技| 巫山县| 扎鲁特旗| 忻州市| 罗甸县| 新河县| 永定县| 申扎县| 泽普县| 邯郸市| 溧水县| 萍乡市| 富民县| 如皋市| 湟源县| 浠水县| 宁明县| 安塞县| 西和县| 兰州市| 榆社县| 大埔区| 新田县| 互助| 叙永县| 辉南县| 慈利县| 凤山市| 深泽县|