What is the difference between concurrency and parallelism?

前端 未结 30 3257
清歌不尽
清歌不尽 2020-11-22 00:21

What is the difference between concurrency and parallelism?

Examples are appreciated.

30条回答
  •  独厮守ぢ
    2020-11-22 01:21

    Think of it as servicing queues where server can only serve the 1st job in a queue.

    1 server , 1 job queue (with 5 jobs) -> no concurrency, no parallelism (Only one job is being serviced to completion, the next job in the queue has to wait till the serviced job is done and there is no other server to service it)

    1 server, 2 or more different queues (with 5 jobs per queue) -> concurrency (since server is sharing time with all the 1st jobs in queues, equally or weighted) , still no parallelism since at any instant, there is one and only job being serviced.

    2 or more servers , one Queue -> parallelism ( 2 jobs done at the same instant) but no concurrency ( server is not sharing time, the 3rd job has to wait till one of the server completes.)

    2 or more servers, 2 or more different queues -> concurrency and parallelism

    In other words, concurrency is sharing time to complete a job, it MAY take up the same time to complete its job but at least it gets started early. Important thing is , jobs can be sliced into smaller jobs, which allows interleaving.

    Parallelism is achieved with just more CPUs , servers, people etc that run in parallel.

    Keep in mind, if the resources are shared, pure parallelism cannot be achieved, but this is where concurrency would have it's best practical use, taking up another job that doesn't need that resource.

提交回复
热议问题