java java中并发和并行之间有区别吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14130266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a difference between concurrency and parallelism in java?
提问by user1877082
I have been doing some research in Google and cant quite get my head around the differences (if any) between concurrent and parallel programs in java. Some of the information I have looked at suggests no differences between both. Is this the case??
我一直在谷歌做一些研究,但不能完全理解 Java 中并发和并行程序之间的差异(如果有的话)。我查看的一些信息表明两者之间没有区别。有这种情况吗??
回答by StriplingWarrior
It depends on who is defining it. The people who created the Go programming language call code Concurrentif it is broken up into pieces which couldbe treated in parallel, whereas Parallelism implies that those pieces are actually running at the same time.
这取决于谁来定义它。如果 Go 编程语言被分解成可以并行处理的部分,那么创建 Go 编程语言的人会调用代码 Concurrent,而 Parallelism 意味着这些部分实际上是同时运行的。
Since these are programming principles, the programming language has no bearing on how they are defined. However, Java 8 will have more features to enable both concurrency and parallelism without messing up your code too much. For example, code like this:
由于这些是编程原则,因此编程语言与它们的定义方式无关。但是,Java 8 将具有更多功能来启用并发性和并行性,而不会过多地弄乱您的代码。例如,像这样的代码:
List<Integer> coolItemIds = new List<Integer>();
for(Item item : getItems())
{
if(item.isCool())
{
int itemId = item.getId();
coolItemIds.add(item);
}
}
... which is non-concurrent and non-parallel, could be written like this (my syntax is probably wrong, but hopefully you get the idea):
...这是非并发和非并行的,可以这样写(我的语法可能是错误的,但希望你能明白):
Iterable<Item> items = getItems();
Iterable<Item> coolItems = items.filter(item -> item.isCool());
Iterable<Integer> coolItemIds = coolItems.map(item -> item.getId());
The above code is written in a concurrentmanner: none of the given code requires that the coolItems be filtered one at a time, or that you can only call getId()
on one item at a time, or even that the items at the beginning of the list need to be filtered or mapped before items at the end. Depending on what type of Iterable
is returned from getItems()
, the given operations may or may not run in parallel, but the code you've written is concurrent.
上面的代码是并发编写的:没有一个给定的代码要求一次过滤一个coolItems,或者一次只能调用getId()
一个项目,甚至列表开头的项目需要在最后的项目之前进行过滤或映射。根据Iterable
返回的类型getItems()
,给定的操作可能会也可能不会并行运行,但您编写的代码是并发的。
Also of interest:
还有兴趣:
- Concurrency is not Parallelism(presentation video)
- Concurrency is not Parallelism?(Discussion on StackOverflow
回答by Patrick87
I suppose it depends on your definitions, but my understanding goes roughly like this:
我想这取决于你的定义,但我的理解大致是这样的:
- Concurrencyrefers to things happening in some unspecified order. Multitasking - executing multiple programs by interleaving instructions via time slicing - is an good way to think about this sense of concurrency.
- Parallelism(or "true" parallelism) refers to things happening at literally the same time. This requires hardware support (coprocessors, multi-core processors, networked machines, etc.). All parallelism is concurrent, but not all concurrency is parallel.
- 并发是指以某种未指定的顺序发生的事情。多任务处理——通过时间切片交错指令来执行多个程序——是考虑这种并发感的好方法。
- 并行性(或“真正的”并行性)是指几乎同时发生的事情。这需要硬件支持(协处理器、多核处理器、联网机器等)。所有的并行都是并发的,但并非所有的并发都是并行的。
As far as I'm aware, neither term is Java-specific, or has any Java-specific nuances.
据我所知,这两个术语都不是特定于 Java 的,也没有任何特定于 Java 的细微差别。
回答by Alexandar
Parallelization(or Parallelism or Parallel computing) is a form of computation in which many calculations are carried out simultaneously. In essence, if a CPU intensive problem can be divided in smaller, independent tasks, then those tasks can be assigned to different processors
并行化(或并行或并行计算)是一种计算形式,其中许多计算同时进行。本质上,如果 CPU 密集型问题可以划分为更小的独立任务,那么这些任务可以分配给不同的处理器
Concurrencyis more about multitasking which is executing many actions but not necessary CPU intensive problem.
并发更多是关于多任务处理,它执行许多操作但不是必需的 CPU 密集型问题。
回答by Tom Anderson
I don't think the two terms have well-defined distinct meanings. They're both terms of art rather than technical terms.
我认为这两个术语没有明确定义的不同含义。它们都是艺术术语而不是技术术语。
That said, the way i interpret them is that something is concurrentif it can be done at the same time as other things, and parallelif it can be done by multiple threads at the same time. I take this usage largely from the JVM garbage collection documentation, which says things like
也就是说,我解释它们的方式是,如果某件事可以与其他事情同时完成,则它是并发的,如果它可以由多个线程同时完成,则它是并行的。我主要从JVM 垃圾收集文档中使用这种用法,它说的是
The concurrentmark sweep collector, also known as the concurrentcollector or CMS, is targeted at applications that are sensitive to garbage collection pauses. It performs most garbage collection activity concurrently, i.e., while the application threads are running
在并发标记清除收集器,也被称为并发收集器或CMS,是针对那些对垃圾收集暂停敏感的应用。它执行最垃圾收集活动的同时,即,而应用程序线程运行
and
和
CMS collector now uses multiple threadsto perform the concurrent marking task in parallelon platforms with multiple processors.
CMS 收集器现在使用多个线程在具有多个处理器的平台上并行执行并发标记任务。
Admittedly, this is a very specific context, and it is probably unwise to generalise from it.
诚然,这是一个非常具体的上下文,从中进行概括可能是不明智的。
回答by mahi
If you program using threads (concurrent programming), it's not necessarily going to be executed as such (parallel execution), since it depends on whether the machine can handle several threads.
如果您使用线程进行编程(并发编程),则不一定会这样执行(并行执行),因为这取决于机器是否可以处理多个线程。
Here's a visual example. Threads on a non-threaded machine:
这是一个直观的例子。非线程机器上的线程:
-- -- --
/ \
>---- -- -- -- -- ---->>
Threads on a threaded machine:
线程机器上的线程:
------
/ \
>-------------->>
The dashes represent executed code. As you can see, they both split up and execute separately, but the threaded machine can execute several separate pieces at once.
破折号代表执行的代码。如您所见,它们都分开并单独执行,但线程机器可以同时执行多个单独的部分。
Please refer this What is the difference between concurrent programming and parallel programming?
请参考这个并发编程和并行编程的区别是什么?
回答by Ravindra babu
From oracle documentation page:
从 oracle 文档页面:
In a multithreaded process on a single processor, the processor can switch execution resources between threads, resulting in concurrent execution.
在单处理器上的多线程进程中,处理器可以在线程之间切换执行资源,从而导致并发执行。
In the same multithreaded process in a shared-memory multiprocessor environment, each thread in the process can run on a separate processor at the same time, resulting in parallel execution.
在共享内存多处理器环境中的同一个多线程进程中,进程中的每个线程可以同时在单独的处理器上运行,从而导致并行执行。
When the process has fewer or as many threads as there are processors, the threads support system in conjunction with the operating environment ensure that each thread runs on a different processor.
当进程的线程数与处理器数相同或更少时,线程支持系统会结合运行环境确保每个线程运行在不同的处理器上。
Java SE 7 further enhanced parallel processing by adding ForkJoinPoolAPI.
Java SE 7 通过添加ForkJoinPoolAPI进一步增强了并行处理。
Refer to below posts for more details:
有关更多详细信息,请参阅以下帖子:
Parallel programming with threads in Java( Java specific )
使用 Java 中的线程进行并行编程(Java 特定)
Concurrency vs Parallelism - What is the difference?( Language agnostic)
并发与并行 - 有什么区别?(语言不可知)
回答by Daniel Hajduk
Concurrency is an architectural design pattern, which allows you to run multiple operations at once (which can but dont have to be executed in parallel).
并发是一种架构设计模式,它允许您一次运行多个操作(可以但不必并行执行)。
In case of a single core execution of such operations, parallelizm can be "simulated" by for example context switching ( assuming your programming language uses threads for parallel execution).
在此类操作的单核执行的情况下,parallelizm 可以通过例如上下文切换来“模拟”(假设您的编程语言使用线程进行并行执行)。
Lets say you have two threads, in one you enqueue jobs. Second one awaits untill any job exists and picks it up for execution. Despite using a single core processor, both of them are running and communicating (via queue).
假设您有两个线程,在一个线程中将作业排入队列。第二个等待,直到任何作业存在并拿起它执行。尽管使用单核处理器,但它们都在运行和通信(通过队列)。
This is a concurrent execution - even through threads are executed sequentially on a single core (share it).
这是一个并发执行 - 即使通过线程在单个核心上顺序执行(共享它)。
Parallel version of the same exercise would look similar with with one difference:
同一练习的并行版本看起来很相似,但有一个区别:
Execution of threads would happen on a multi-core processor. Threads would be running in parallel to each other and not sequentially (each on his own core).
线程的执行将发生在多核处理器上。线程将彼此并行运行,而不是按顺序运行(每个线程都在自己的核心上)。