Is there a difference between concurrency and parallelism in java?

前端 未结 8 1364
时光说笑
时光说笑 2021-01-30 13:35

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

相关标签:
8条回答
  • 2021-01-30 14:00

    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
    0 讨论(0)
  • 2021-01-30 14:05

    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 concurrent if it can be done at the same time as other things, and parallel if 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

    The concurrent mark sweep collector, also known as the concurrent collector 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

    and

    CMS collector now uses multiple threads to perform the concurrent marking task in parallel on platforms with multiple processors.

    Admittedly, this is a very specific context, and it is probably unwise to generalise from it.

    0 讨论(0)
  • 2021-01-30 14:08

    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.

    0 讨论(0)
  • 2021-01-30 14:14

    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).

    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).

    0 讨论(0)
  • 2021-01-30 14:15

    Question is quite old, but I'd like to summarize these two in a very clear and concise manner:

    1. Concurrency - think of multitasking by one actor:
      is when x number of processes/threads (x>1) compete for the same resource. In case of concurrency, when two processes/threads are executed on one CPU, they're not really parallel, meaning, that CPU clock will be switching back and forth from process to process in a super fast way, which will depict the illusion of parallelism, but again - it's 1 CPU shared among different processes/threads. Imagine 5 instructions are to be executed, and they compete to get the resource of CPU, in order to get executed;

    2. Parallelism - think of a multiple tasks where each task is taken care by a separate actor:
      is when x number of processes/threads (x>1) execute in parallel, at the same time. Imagine if there are 5 processes/threads, and there are 5 CPU cores, which means that each core can independently execute each thread/process.

    0 讨论(0)
  • 2021-01-30 14:16

    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.

    0 讨论(0)
提交回复
热议问题