并发与并行的区别

Concurrency is when two tasks can start, run, and complete in
overlapping time periods. Parallelism is when tasks literally run at
the same time, eg. on a multi-core processor. Concurrency is the
composition of independently executing processes, while parallelism is
the simultaneous execution of (possibly related) computations.
Concurrency is about dealing with lots of things at once. Parallelism
is about doing lots of things at once. An application can be
concurrent – but not parallel, which means that it processes more than
one task at the same time, but no two tasks are executing at same time
instant. An application can be parallel – but not concurrent, which
means that it processes multiple sub-tasks of a task in multi-core CPU
at same time. An application can be neither parallel – nor
concurrent, which means that it processes all tasks one at a time,
sequentially. An application can be both parallel – and concurrent,
which means that it processes multiple tasks concurrently in
multi-core CPU at same time. Vipin Jain. Differences between
concurrency vs. parallelism
翻译成中文:
并发是两个任务可以在重叠的时间段内启动,运行和完成。并行是任务在同一时间运行,例如,在多核处理器上。
并发是独立执行过程的组合,而并行是同时执行(可能相关的)计算。
并发是一次处理很多事情,并行是同时做很多事情。
应用程序可以是并发的,但不是并行的,这意味着它可以同时处理多个任务,但是没有两个任务在同一时刻执行。
应用程序可以是并行的,但不是并发的,这意味着它同时处理多核CPU中的任务的多个子任务。
一个应用程序可以即不是并行的,也不是并发的,这意味着它一次一个地处理所有任务。
应用程序可以即是并行的也是并发的,这意味着它同时在多核CPU中同时处理多个任务。

我再来用我自己的话分析一下:
并行才是我们通常认为的那个同时做多件事情,而并发则是在线程这个模型下产生的概念。并发表示同时发生了多件事情,通过时间片切换,哪怕只有单一的核心,也可以实现“同时做多件事情”这个效果。根据底层是否有多处理器,并发与并行是可以等效的,这并不是两个互斥的概念。

举个我们开发中会遇到的例子,我们说资源请求并发数达到了1万。这里的意思是有1万个请求同时过来了。但是这里很明显不可能真正的同时去处理这1万个请求的吧!如果这台机器的处理器有4个核心,不考虑超线程,那么我们认为同时会有4个线程在跑。也就是说,并发访问数是1万,而底层真实的并行处理的请求数是4。如果并发数小一些只有4的话,又或者你的机器牛逼有1万个核心,那并发在这里和并行一个效果。也就是说,并发可以是虚拟的同时执行,也可以是真的同时执行。而并行的意思是真的同时执行。

结论是:并行是我们物理时空观下的同时执行,而并发则是操作系统用线程这个模型抽象之后站在线程的视角上看到的“同时”执行。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值