Is there a difference between concurrency and parallelism in java?

前端 未结 8 1365
时光说笑
时光说笑 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:22

    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?

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

    From oracle documentation page:

    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 ForkJoinPool API.

    Refer to below posts for more details:

    Parallel programming with threads in Java ( Java specific )

    Concurrency vs Parallelism - What is the difference? ( Language agnostic)

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