并发和并行有什么区别?

做并发编程之前,必须首先理解什么是并发,什么是并行,什么是并发编程,什么是并行编程。

并发(concurrency)和并行(parallellism)是:

  1. 解释一:并行是指两个或者多个事件在同一时刻发生;而并发是指两个或多个事件在同一时间间隔发生。
  2. 解释二:并行是在不同实体上的多个事件,并发是在同一实体上的多个事件
  3. 解释三:在一台处理器上“同时”处理多个任务,在多台处理器上同时处理多个任务。如hadoop分布式集群

所以并发编程的目标是充分的利用处理器的每一个核,以达到最高的处理性能。

各位是如何理解并行和并发的呢?

原创文章,转载请注明: 转载自并发编程网 – ifeve.com本文链接地址: 并发和并行有什么区别?



方 腾飞

花名清英,并发网(ifeve.com)创始人,畅销书《Java并发编程的艺术》作者,蚂蚁金服技术专家。目前工作于支付宝微贷事业部,关注互联网金融,并发编程和敏捷实践。微信公众号aliqinying。
FavoriteLoading添加本文到我的收藏
    • 泡泡
    • 01/08. 2013 10:23pm

    并行: 就是等于号

    并发: 就是大于号

      • 泡泡
      • 01/08. 2013 10:34pm

      通俗点说 并发就是不同线程同时干一件事情
      并行就是不同线程同时干不同的事情

    • 天涯
    • 03/08. 2013 9:31am

    Quote“并行(concurrency)和并发(parallellism)”
    这是在误导!!
    请好好查查字典再发帖子,你刚好把两个词的英文写反了!!

    • 匿名
    • 04/17. 2013 10:10am

    《并发的艺术》中大概是这样写的,系统中有多个任务同时存在可称之为“并发”,系统内有多个任务同时执行可称之为“并行”;并发是并行的子集。比如在单核CPU系统上,只可能存在并发而不可能存在并行。

      • 低调求生存
      • 06/07. 2013 11:11pm

      You can get concurrency on a single core processor using preemptive time-shared threads. But what you cannot achieve on a single core processor is parallelism.

    • 低调求生存
    • 06/07. 2013 11:08pm

    感觉你的描述还是体会不出什么具体的区别,下面是Stackoverflow上的摘要:
    It’s well understood that concurrency is decomposition of a complex problem into smaller components. If you cannot correctly divide something into smaller parts, it’s hard to solve it using concurrency. but it is wrong b/c those smaller components may depend on each other in a sequential manner to complete, so even if you divide into small components, it does not mean you achieve concurrency / parallelism.
    In all my classes of parallel and distributed algorithms (both in BS and MS) we never talked about “concurrency we obtained and now let’s see how to obtain parallelism”. If you use the word concurrency to describe and algorithm then you imply parallelism and vice versa.
    When you have the abstract form of an algorithm in mind, you then have to choose if you will implement it with Message Passing or Shared Memory or maybe Hybrid. You will also have to consider the type of memory access (NUMA, UMA, etc) and the Topology used (Hypercube, Torus, Ring, Mesh, Tree, etc)
    This seems a lot of work to someone who just wants something, maybe even simple, done in a parallel way (e.g. parallel for).
    And it is a lot of work especially if you change the topology (so you can have all of its advantages).
    So you write the parallel code (be it simple or complex) and the VM or compiler will choose what seems to be the best way to go, even running it in a sequential way! (an example would be Task Parallel Library for .net).And it should mention that I am talking about concurrency in a program / algorithm and not between independent programs that run in a system.

    From an implementation point of view, if you say “parallelism” you usually intend a program that runs on the local computer or a cluster (Shared Memory communication), and “distributed” when you run the program on a grid (Message Passing communication).

    so this also mean concurrency as the abstract principle and parallel as the way it is implemented [Shared Memory, Message Passing, Hybrid between both; Type of memory acces (numa, uma, etc)].

    BTW:You can get concurrency on a single core processor using preemptive time-shared threads. But what you cannot achieve on a single core processor is parallelism.

    Reference:
    Concurrency is not Parallelism(it’s better): http://concur.rspace.googlecode.com/hg/talk/concur.html

    • ada
    • 07/24. 2013 9:28am

    并发是逻辑上的同时发生,并行更多是侧重于物理上的同时发生。并发往往是指程序代码的结构支持并发,并发的程序在多cpu上运行起来才有可能达到并行,并行往往是描述运行时的状态。

    • Anonymous
    • 10/29. 2013 11:08pm

    所以并发编程的目标是充分的利用处理器的每一个核,以达到最高的处理性能。

    那并行编程的目标是什么??

    • 并行编程的目标也是达到最高的处理速率,如hadoop利用多台机器进行并行处理。

    • zhengJackson
    • 11/01. 2013 11:18am

    天涯 :
    Quote“并行(concurrency)和并发(parallellism)”
    这是在误导!!
    请好好查查字典再发帖子,你刚好把两个词的英文写反了!!

    到底是哪个?文章是还没来得及修改?

    • ligang7121087
    • 04/04. 2016 11:49pm

    3.解释三:在一台处理器上“同时”处理多个任务,在多台处理器上同时处理多个任务。如hadoop分布式集群
    并发:在一台处理器上”同时”处理多个任务
    并行:在多台处理器上同时处理多个任务,比如说hadoop分布式集群

    • Simba_cheng
    • 10/18. 2016 11:03pm

    并行 : 双十一 , 零时零分零秒,三十万用户同时登录淘宝网

    并发 : 双十一 , 零时零分零秒 至 两点零分零秒 共有一百万用户登录淘宝网.

  1. No trackbacks yet.

You must be logged in to post a comment.

return top