Is there a difference between concurrency and parallelism in java?

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??


It depends on who is defining it. The people who created the Go programming language call code Concurrent if it is broken up into pieces which could be treated in parallel, whereas Parallelism implies that those pieces are actually running at the same time.

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:

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 concurrent manner: 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.

Also of interest:

  • Concurrency is not Parallelism (presentation video)
  • Concurrency is not Parallelism? (Discussion on StackOverflow

  • I suppose it depends on your definitions, but my understanding goes roughly like this:

  • Concurrency refers 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.


    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

    Concurrency is more about multitasking which is executing many actions but not necessary CPU intensive problem.

    链接地址: http://www.djcxy.com/p/13922.html

    上一篇: 多线程和多核的差异

    下一篇: java中的并发和并行有区别吗?