并发和并行java_并发和并行的区别

本文探讨了并发和并行的概念,强调两者之间的区别。并发指的是多个任务在重叠的时间段内启动、运行和完成,而并行则是在多核处理器上任务实际同时运行的情况。并发依赖于CPU时间分片,而并行则需要硬件的多处理单元。理解这两个概念对于Java多线程编程至关重要。
摘要由CSDN通过智能技术生成

java编程实战第16届大奖提名图书

69元

(需用券)

去购买 >

b6f47c20a9d0656d6a65fb34b3dad2c6.png

前言

看了很多文章都没有讲好这个问题, 一会一个火车的例子, 一会一个白菜的例子,看了也不知所云。找到一篇从CPU的角度讲这个问题的文章,抄下来了。

https://howtodoinjava.com/java/multi-threading/concurrency-vs-parallelism/

并发和并行

并发是指多个任务,这些任务在重叠的时间段内以无特定顺序启动,运行和完成。并行是指多个任务或唯一任务的多个部分在逻辑上同时运行的情况,例如在多核处理器上。请记住,并发和并行性不是一回事。让我们更详细地了解当我说“并发与并行”的意思。

并发

当我们谈论至少两个或更多任务时,并发这个定义是适用的。当一个应用程序实际上可以同时执行两个任务时,我们将其称为并发应用程序。尽管这里的任务看起来像是同时运行的,但实际上它们可能不一样。它们利用操作系统的CPU时间分片功能,其中每个任务运行其任务的一部分,然后进入等待状态。当第一个任务处于等待状态时,会将CPU分配给第二个任务以完成其一部分任务。

操作系统根据任务的优先级分配CPU和其他计算资源,例如内存;依次处理所有任务,并给他们完成任务的机会。对于最终结果来看,用户感觉所有任务都是同时运行的,这称为并发。

并行

并行不需要两个任务存在。通过为每个任务或子任务分配一个内核,它实际上使用多核CPU基础结构同时运行部分任务或多个任务。

并行性本质上要求具有多个处理单元的硬件。在单核CPU中,您可能会获得并发性,但不能获得并行性。

并发与并行之间的区别现在,让我们列出并发与并行之间的显着区别。并发是两个任务可以在重叠的时间段内启动,运行和完成的时间。并行是指任务实际上在同一时间运行,例如。在多核处理器上。

并发是由独立执行的进程组成,而并行性是同时执行(相关的)计算。

并发就是一次处理很多事情。并行是关于一次做很多事情。

一个应用程序可以是并发的,但不能是并行的,这意味着它可以同时处理多个任务,但是没有两个任务可以同时执行。

一个应用程序可以是并行的,但不能是并发的,这意味着它可以同时处理多核CPU中一个任务的多个子任务。

一个应用程序既不能是并行的,也不能是并发的,这意味着它一次顺序地处理所有任务。

一个应用程序可以是并行的,也可以是并发的,这意味着它可以同时在多核CPU中同时处理多个任务。

这就是并发与并行的关系,这是Java多线程概念中非常重要的概念。

学习愉快!

Concurrency vs. Parallelism.

Concurrency means multiple tasks which start, run, and complete in overlapping time periods, in no specific order.Parallelism is when multiple tasks OR several part of a unique task literally run at the same time, e.g. on a multi-core processor. Remember that Concurrency and parallelism are NOT the same thing.

Let’s understand more in detail that what I mean when I say Concurrency vs. Parallelism.

Concurrency

Concurrency is essentially applicable when we talk about minimum two tasks or more. When an application is capable of executing two tasks virtually at same time, we call it concurrent application. Though here tasks run looks like simultaneously, but essentially they MAY not. They take advantage of CPU time-slicing feature of operating system where each task run part of its task and then go to waiting state. When first task is in waiting state, CPU is assigned to second task to complete it’s part of task.

Operating system based on priority of tasks, thus, assigns CPU and other computing resources e.g. memory; turn by turn to all tasks and give them chance to complete. To end user, it seems that all tasks are running in parallel. This is called concurrency.

Parallelism

Parallelism does not require two tasks to exist. It literally physically run parts of tasks OR multiple tasks, at the same time using multi-core infrastructure of CPU, by assigning one core to each task or sub-task.

Parallelism requires hardware with multiple processing units, essentially. In single core CPU, you may get concurrency but NOT parallelism.

Differences between concurrency vs. parallelism

Now let’s list down remarkable differences between concurrency and parallelism.

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 .

That’s all about Concurrency vs. Parallelism, a very important concept in java multi-threading concepts.

Happy Learning !!

java 11官方入门(第8版)教材

79.84元

包邮

(需用券)

去购买 >

f0f3f55624fb396b1764d42d6df88864.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值