• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Multithreading - Concurrency - Parallelism - Synchronization Concepts

 
Ranch Hand
Posts: 441
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As per my understanding, multithreading is a concept where the process is divided into multiple threads which can run in parallel. This can be achieved using synchronization. I would like to understand what is the concept of parallelism, concurrency and how does it differ from synchronization?
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I use these terms in the following way:

ParallelWhen two or more tasks don't depend on each other, they can be performed in any order, possibly at the same time.
ConcurrentPerforming parallel tasks at the same time, using multiple threads.
SynchronizationMaking sure that concurrently executed parallel tasks use shared resources correctly. This means updating main memory at certain times and waiting for resources to become available.


Synchronization is not used to achieve multi-threading. You can have a programming using multiple threads without ever having to synchronize between them. Synchronization is necessary when two threads need access to the same resources.
 
Vaibhav Gargs
Ranch Hand
Posts: 441
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Stephan. So, how does parallelism differ from normal multithreading? Ain't both of them the same thing?

And, can we achieve parallelism on single core single processor? If yes, then how since at one time only one thread will be running.
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some people use parallel and concurrent/multi-threaded interchangeably, yes.

However, parallelism refers to a quality of the code, not to how it's being executed. A program with a high level of parallelism will just run sequentially if there is only one thread to run on. Code with a low level of parallelism won't get executed concurrently when there are extra threads available.

Like I said in my previous post, parallel tasks are just tasks that don't have to be performed in a particular order. If you chop your application up in separate parallel tasks, they CAN be performed concurrently. That doesn't mean that they necessarily will be. A good example of this is running JavaScript in your browser. JavaScript promotes writing highly parallel code, using promises and callbacks. However, JavaScript also only runs on one thread in the browser. It's up to the browser to determine which task gets performed when.

Something similar is true for Java when using multiple 'green' threads on a system that only has one core and no features like hyper-threading. If you use multiple threads, the thread scheduler will just switch between execution contexts every now and then, and the parallel tasks will appear to run at the same time, even though they're not really.

Here is an example of parallel code that isn't multi-threaded:

It's up to the implementation of the executor service in which order these tasks are performed. In practice, helpMother() will be called before helpFather(), but in theory this is not necessary. Helping mother and helping father are parallel tasks, but they are not performed concurrently.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic