线程并发和并行

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.

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

  1. java 并发和并行_并发和并行性有什么区别?

    并发和并行性有什么区别? 示例被赞赏. #1楼 并发性:具有共享资源潜力的多个执行流 例如:两个线程争用一个I / O端口. 平行主义:将问题分成多个相似的块. 例如:通过在文件的每半部分上运行两个进 ...

  2. winform判断线程有没有完成_并发编程系列1:线程池的架构实现、大小配置、及四种线程池使用...

    △ 公众号回复关键词"架构" 即可领取<1500+BAT架构及面试专题合集> 本篇为线程池系列文章之一,不经常使用线程池的童鞋,还有对几种线程的使用不甚了解的童鞋,可以 ...

  3. 多核cpu是并发还是并行_多核CPU及其带来的并发更改

    摩尔定律-戈登·摩尔在1965年的预测,即每个集成电路组件的数量将每18到24个月翻一番-举行了真,并预计将保持真,直到2015 - 2020年(参见相关主题 ). 直到2005年,CPU时钟速率也一 ...

  4. 线程并发库和线程池的作用_并发–顺序线程和原始线程

    线程并发库和线程池的作用 不久前,我参与了一个项目,该项目的报告流程如下: 用户会要求举报 报告要求将被翻译成较小的部分 基于零件/节的类型的每个零件的报告将由报告生成器生成 组成报告的各个部分将重新 ...

  5. 简述java 线程四个状态_面四个选项中,哪些是线程进入阻塞状态的原因?_学小易找答案...

    [简答题]通过继承 Thread 类的方式创建两个线程,在 Thread 构造方法中指定线程的名字,并将这两个线程的名字打印出来. [论述题]请运用所学原理解释,为什么在孩子成长的过程中,家长要不断地 ...

  6. java 线程间变量共享_多线程:(五)多个线程之间共享数据

    简单的总结一下多线程共享数据的方式.其实,最重要的,是一个思想,一个面向对象的编程思想的建立. 说两点: 一.如果每个线程执行的代码相同,可以使用同一个Runnable对象,这个Runnable对象中 ...

  7. java线程同步的实现_【Java多线程系列三】实现线程同步的方法

    packagecom.concurrent.test;importjava.util.Stack;importjava.util.concurrent.ArrayBlockingQueue;impor ...

  8. python网络编程基础(线程与进程、并行与并发、同步与异步、阻塞与非阻塞、CPU密集型与IO密集型)...

    python网络编程基础(线程与进程.并行与并发.同步与异步.阻塞与非阻塞.CPU密集型与IO密集型) 目录 线程与进程并行与并发同步与异步阻塞与非阻塞CPU密集型与IO密集型 线程与进程 进程 前言 ...

  9. Java基础(29)线程与进程、并发与并行、多线程的三种启动方式、买票案例

    1. 进程与线程 1. 进程与线程的概述: (1)进程: 进程就是正在运行的程序,是系统进行资源分配和调用的独立单位,每一个进程都有他自己的内存空间和系统资源 比如正在运行的应用程序(QQ,微信,QQ ...

最新文章

  1. mac hdmi 不能调整音量_如何使用Mac的媒体键在DisplayPortHDMI或Thunderbolt监视器上调整扬声器音量...
  2. 分类数据的分析-卡方检验运用
  3. Linux学习笔记(二十二)yum更换国内源、yum下载rpm包、源码包安装
  4. Centos 7.4版本升级内核3.10+ 到4+过程
  5. oc 把view添加到rootcontrollerview控制的view
  6. 一二线城市知名 IT 互联网公司名单
  7. Three.js地球开发—1.经纬度转球面坐标
  8. STM32芯片无法正常工作的现象及原因分析集锦
  9. 置信区间的置信区间_什么是置信区间,为什么人们使用它们?
  10. springboot中banner图制作
  11. 商业银行普惠金融可持续发展综合能力呈现梯队化,专项领域各有所长
  12. 安卓手机如何打开.crx文件_如何在安卓手机上使用Chrome插件
  13. [转载] 中美德突发事件应急指挥组织结构初探
  14. NeoCognitron
  15. 应对机器学习中类不平衡的10种技巧
  16. Layui官方js正则验证手机号,邮箱,网址,日期,身份证
  17. 【计算机组成原理】计算机组成原理——概述篇
  18. 高中数学必修一:函数性质图像知识点总结(名师总结)
  19. [面向对象与多线程综合实验]数据挖掘系统
  20. XSS 和 CSRF 详解

热门文章

  1. ColorMatrix颜色矩阵让图片千变万化
  2. python怎么做类型标注
  3. 电商网站中用户评论模块剖析
  4. 免费python基础笔记_python的基础练习笔记
  5. 其实,男人也需要被疼爱
  6. 利用 ransac 算法拟合平面
  7. 盛大创新院许式伟:影响我一生的五个重要选择
  8. IBM的APAR搜索入口地址
  9. 使用Zerotier+Moon结点内网穿透
  10. 学Java 这样入门 28天轻松掌握