线程并发和并行_并发性,并行性和圣诞老人​​的许多线程

线程并发和并行

Consider the following: Santa brings toys to all the good girls and boys.

考虑以下几点:圣诞老人为所有好的男孩和男孩带来玩具。

There are 7,713,468,100 people in the world in 2019, around 26.3% of which are under 15 years old. This works out to 2,028,642,110 children (persons under 15 years of age) in the world this year.

2019年全球有7,713,468,100人 ,其中26.3%在15岁以下。 今年,全世界有2028642110名儿童(15岁以下)。

Santa doesn’t seem to visit children of every religion, so we’ll  generalize and only include Christians and non-religious folks.  Collectively that makes up approximately 44.72% of the population. If we assume that all kids take after their parents, then 907,208,751.6 children would appear to be Santa-eligible.

圣诞老人似乎并没有拜访每一个宗教的孩子,因此我们将一概而论,只包括基督徒和非宗教人士。 总的来说, 约占人口的44.72% 。 如果我们假设所有孩子都紧追其父母,那么907,208,751.6个孩子似乎符合圣诞老人资格。

What percentage of those children are good? It’s impossible to know; however, we can work on a few assumptions. One is that Santa Claus functions more on optimism than economics and would likely have prepared  for the possibility that every child is a good child in any given year. Thus, he would be prepared to give a toy to every child. Let’s assume it’s been a great year and that all 907,208,751.6 children are getting toys.

这些孩子中好孩子的百分比是多少? 不可能知道; 但是,我们可以进行一些假设。 一个是圣诞老人在经济上更多地是出于乐观而不是经济学,并且可能已经准备好在任何一年中每个孩子都是好孩子。 因此,他将准备给每个孩子一个玩具。 假设今年是非常棒的一年,所有907,208,751.6名儿童都在获取玩具。

That’s a lot of presents, and, as we know, they’re all made by Santa’s elves at his North China Pole workshop. Given that there are 365 days in a year and one of them  is Christmas, let’s assume that Santa’s elves collectively have 364 days to create and gift wrap 907,208,752 (rounded up) presents. That works out to 2,492,331.74 presents per day.

那是很多礼物,而且我们知道,它们都是圣诞老人的精灵在他的华北极地工坊上制作的。 假设一年中有365天,其中一个是圣诞节,那么我们假设圣诞老人的精灵们总共有364天的创作时间和907,208,752(向上取整)礼物包装。 每天计算为2,492,331.74礼物。

Almost two-and-a-half million presents per day is a heavy workload for any workshop. Let’s look at two paradigms that Santa might employ to hit this goal: concurrency, and parallelism.

每天几乎有两百五十万份礼物对任何车间来说都是沉重的工作量。 让我们看一下圣诞老人可能会使用的两种范例:并发和并行性。

顺序过程 (A sequential process)

Suppose that Santa’s workshop is staffed by exactly one, very hard working, very tired elf. The production of one present involves four  steps:

假设圣诞老人的车间只有一个非常勤奋,非常疲倦的小精灵。 一份礼物的制作涉及四个步骤:

  1. Cutting wood

    砍柴
  2. Assembly and gluing

    组装和胶合
  3. Painting

    绘画
  4. Gift-wrapping

    礼品包装

With a single elf, only one step for one present can be happening at any instance in time. If the elf were to produce one present at a time from beginning to end, that process would be executed sequentially. It’s not the most efficient method for producing two-and-a-half million presents per day; for instance, the elf would have to wait around doing nothing while the glue on the present was drying before moving on to the next step.

对于一个小精灵,任何时候在任何时候都只能对一个礼物执行一个步骤。 如果小精灵从头到尾每次都产生一个礼物,则该过程将依次执行。 这不是每天产生两百五十万份礼物的最有效方法。 例如,当礼物上的胶水干燥时,小精灵将不得不等待任何动作,然后再继续下一步。

并发 (Concurrency)

In order to be more efficient, the elf works on all presents concurrently.

为了提高效率,小精灵同时处理所有礼物。

Instead of completing one present at a time, the elf first cuts all the wood for all the toys, one by one. When everything is cut, the elf assembles and glues the toys together, one after the other. This concurrent processing means that the glue from the first toy has time to dry (without needing more attention from the elf) while the remaining toys are glued together. The same goes for painting, one toy at a time, and finally wrapping.

精灵没有一次完成一个礼物,而是首先一个个地砍掉所有玩具的所有木头。 切割完所有东西后,小精灵一个接一个地将玩具组装并粘在一起。 这种同时进行的处理意味着第一个玩具的胶水有时间变干(不需要小精灵更多的注意),而其余的玩具则被胶合在一起。 绘画,一次玩具和最后包装都一样。

Since one elf can only do one task at a time, a single elf is using the day as efficiently as possible by concurrently producing presents.

由于一个小精灵一次只能执行一项任务,因此,一个小精灵通过同时生产礼物来尽可能高效地利用这一天。

平行性 (Parallelism)

Hopefully, Santa’s workshop has more than just one elf. With more elves, more toys can be built simultaneously over the course of a day. This simultaneous work means that the presents are being produced in parallel. Parallel processing carried out by multiple elves means more work happens at the same time.

希望圣诞老人的工作室有多个精灵。 随着更多的精灵,一天中可以同时制造更多的玩具。 同时进行的工作意味着礼物是并行制作的。 由多个精灵执行的并行处理意味着更多工作同时进行。

Elves working in parallel can also employ concurrency. One elf can still tackle only one task at a time, so it’s most efficient to have multiple elves concurrently producing presents.

并行工作的精灵也可以使用并发。 一个精灵一次仍然只能完成一项任务,因此让多个精灵同时制作礼物是最有效的方法。

Of course, if Santa’s workshop has, say, two-and-a-half million  elves, each elf would only need to finish a maximum of one present per day. In this case, working sequentially doesn’t detract from the workshop’s efficiency. There would still be 7,668.26 elves left over to fetch coffee and lunch.

当然,如果圣诞老人的车间有200万个半精灵,那么每个小精灵每天最多只需要完成一个礼物。 在这种情况下,顺序工作不会影响车间的效率。 仍然有7,668.26个精灵可以吃咖啡和午餐。

圣诞老人和线程 (Santa Claus, and threading)

After all the elves’ hard work is done, it’s up to Santa Claus to deliver the presents – all 907,208,752 of them.

在所有精灵的辛苦工作完成之后,由圣诞老人来送礼物-全部907,208,752。

Santa doesn’t need to make a visit to every kid; just to the one household tree. So how many trees does Santa need to visit? Again with broad generalization, we’ll say that the average number of children per household worldwide is 2.45, based on the year’s predicted fertility rates. That makes 370,289,286.4 houses to visit. Let’s round that up to 370,289,287.

圣诞老人不需要去拜访每个孩子。 只是一棵家庭树。 那么圣诞老人需要参观几棵树? 再次进行广泛的概括,我们可以说,根据该年的预测生育率,全世界每个家庭的平均孩子数为2.45 。 共有370,289,286.4房屋可供参观。 让我们将其四舍五入为370,289,287。

How long does Santa have? The lore says one night, which means one earthly rotation, and thus 24 hours. NORAD confirms.

圣诞老人有多长时间? 传说说一晚,意味着一轮地球旋转,因此是24小时。 NORAD确认

This means Santa must visit 370,289,287 households in 24 hours (86,400 seconds), at a rate of 4,285.75 households per second, never mind the time it takes to put presents under the tree and grab a cookie.

这意味着圣诞老人必须在24小时内(86,400秒)访问370,289,287户家庭,速率为每秒4,285.75户家庭,不要在意将礼物放在树下并抓住饼干所需的时间。

Clearly, Santa doesn’t exist in our dimension. This is especially likely given that, despite being chubby and plump, he fits down a chimney (with a lit fire, while remaining unhurt) carrying a sack of toys containing presents for all the household’s children. We haven’t even considered the fact that his sleigh carries enough toys for every believing boy and girl around the world, and flies.

显然,圣诞老人在我们的维度中并不存在。 考虑到尽管他胖乎乎而丰满,但他还是装了一个烟囱(用点燃的火,同时保持了顺畅),装着一袋装着礼物给所有家庭孩子的玩具。 我们甚至都没有考虑过他的雪橇可以携带足够的玩具供世界各地每个相信的男孩和女孩飞行的事实。

Does Santa exist outside our rules of physics? How could one entity manage to travel around the world, delivering packages, in under 24 hours at a rate of 4,285.75 households per second, and still have time for milk and cookies and kissing mommy?

圣诞老人存在于我们的物理规则之外吗? 一个实体如何能够在24小时内以每秒4,285.75户家庭的速度在世界范围内旅行,运送包裹,仍然有时间喝牛奶,饼干和亲吻妈妈?

One thing is certain: Santa uses the Internet. No other technology has yet enabled packages to travel quite so far and quite so quickly. Even so, attempting to reach upwards of four thousand households per second is no small task, even with the best gigabit internet hookup the North Pole has to offer. How might Santa increase his efficiency?

可以肯定的是,圣诞老人使用Internet。 尚无其他技术使包裹能够走得如此之远,如此之快。 即使这样,即使北极提供了最佳的千兆互联网连接,尝试每秒达到四千个家庭以上也不是一件容易的事。 圣诞老人如何提高效率?

There’s clearly only one logical conclusion to this mystery: Santa Claus is a multithreaded process.

显然只有一个合乎逻辑的结论:圣诞老人是一个多线程进程。

单线程 (A single thread)

Let’s work outward. Think of a thread as one particular task, or the most granular sequence of instructions that Santa might execute. One thread might execute the task, put present under tree. A thread is a component of a process, in this case, Santa’s process of delivering presents.

让我们向外努力。 将线程视为一项特定任务,或圣诞老人可能执行的最精细的指令序列。 一个线程可以执行任务, put present under tree 。 线程是流程的组成部分,在这种情况下,即圣诞老人的送礼流程。

If Santa Claus is single-threaded, he, as a process, would only be able to accomplish one task at a time. Since he’s old and a bit forgetful, he probably has a set of instructions for delivering presents, as well as a schedule to abide by. These two things guide Santa’s thread until his process is complete.

如果圣诞老人是单线程的 ,那么作为一个进程,他一次只能完成一项任务。 由于他很老,有点健忘,因此他可能有一套传送礼物的指示以及遵守时间表。 这两件事指导圣诞老人的工作,直到他的过程完成。

Single-threaded Santa Claus might work something like this:

单线程圣诞老人可能会像这样工作:

  1. Land sleigh at Timmy’s house

    蒂米家的雪橇
  2. Get Timmy’s present from sleigh

    从雪橇上获取蒂米的礼物
  3. Enter house via chimney

    通过烟囱进入房子
  4. Locate Christmas tree

    找到圣诞树
  5. Place Timmy’s present under Christmas tree

    将蒂米的礼物放在圣诞树下
  6. Exit house via chimney

    通过烟囱退出房屋
  7. Take off in sleigh

    在雪橇上起飞

Rinse and repeat… another 370,289,286 times.

冲洗并重复……另外370,289,286次。

多线程 (Multithreading)

Multithreaded Santa Claus, by contrast, is the Doctor Manhattan of the North Pole. There’s still only one Santa Claus in the world; however, he has the amazing ability to multiply his consciousness and accomplish multiple instruction sets of tasks simultaneously. These additional task workers, or worker threads, are created and controlled by the main process of Santa delivering presents.

相比之下, 多线程圣诞老人是北极的曼哈顿博士 。 世界上仍然只有一个圣诞老人。 但是,他具有惊人的能力,可以提高自己的意识并同时完成多个指令集。 这些额外的任务工作程序或工作程序线程是由圣诞老人发送礼物的主要过程创建和控制的。

Each worker thread acts independently to complete its instructions. Since they all belong to Santa’s consciousness, they share Santa’s memory and know everything that Santa knows, including what planet they’re running around on, and where to get the presents from.

每个辅助线程独立执行操作以完成其指令。 由于他们都属于圣诞老人的意识,因此他们共享圣诞老人的记忆,并且知道圣诞老人所知道的一切,包括他们在哪个星球上奔跑,以及从哪里获得礼物。

With this shared knowledge, each thread is able to execute its set of  instructions in parallel with the other threads. This multithreaded  parallelism makes the one and only Santa Claus as efficient as possible.

有了这些共同的知识,每个线程都能够与其他线程并行执行其指令集。 这种多线程并行性使唯一的圣诞老人尽可能高效。

If an average present delivery run takes an hour, Santa need only spawn 4,286 worker threads. With each making one delivery trip per hour,  Santa will have completed all 370,289,287 trips by the end of the night.

如果平均目前的交付运行需要一个小时,那么圣诞老人只需要生成4,286个工作线程即可。 圣诞老人每隔一个小时进行一次送货旅行,那么到深夜时,它将完成所有370,289,287旅行。

Of course, in theory, Santa could even spawn 370,289,287 worker threads, each flying to one household to deliver presents for all the  children in it! That would make Santa’s process extremely efficient, and also explain how he manages to consume all those milk-dunked cookies without getting full. 🥛🍪🍪🍪

当然,从理论上讲,圣诞老人甚至可以产生370,289,287个工作线程,每个线程都飞往一个家庭,为其中的所有孩子分发礼物! 这将使Santa的流程非常高效,并解释了他如何设法在不加满牛奶的情况下消耗掉所有那些挤奶的饼干。 🥛🍪🍪🍪

高效而快乐的多线程圣诞节 (An efficient and merry multithreaded Christmas)

Thanks to modern computing, we now finally understand how Santa Claus manages the seemingly-impossible task of delivering toys to good girls and boys the world-over. From my family to yours, I hope you have a wonderful Christmas. Don’t forget to hang up your stockings on the router shelf.

多亏了现代计算技术,我们现在终于了解了圣诞老人如何处理向世界各地的好女孩和男孩提供玩具这一看似不可能的任务。 从我的家人到你的家人,我希望你过一个愉快的圣诞节。 不要忘记将长袜挂在路由器架子上。

Of course, none of this explains how reindeer manage to fly.

当然,这都不能解释驯鹿如何飞行。

翻译自: https://www.freecodecamp.org/news/concurrency-parallelism-and-the-many-threads-of-santa-claus/

线程并发和并行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值