Sie sind auf Seite 1von 11

Concurrency, Parallelism, Threads,

Processes, Async and Sync — Related? Ĕ


G. Abhisek Follow
Dec 29, 2018 · 5 min read

Recently, a friend of mine asked me his queries on Concurrency and Parallelism.


When I started explaining him his queries, we started discussing other related
concepts and nomenclatures such as Threads -> Multi-threaded and Single,
Asynchronous and Synchronous. At a point, we were confused with queries like:

How is concurrency related to parallelism?

What is synchronous and asynchronous execution?

What is the importance of synchronous and asynchronous programming in concurrency


and parallelism?

How threads fit along with all these concepts?

Many of us sometimes get confused with such queries.

Hold on for a moment and tr y to answer the above queries and visualize the
concepts by yourself.
If you are like, “I am confused? ” Then we will have an awesome time in this discussion ؉

So let us check whether you have grasped it right. We would analyse and
understand what actually they are and their relationship between one another. We
will keep our discussion easy and concise.

Concurrency & Parallelism


Concurrency
Consider you are given a task of singing and eating at the same time. At a given
instance of time either you would sing or you would eat as in both cases your mouth
is involved. So in order to do this, you would eat for some time and then sing and
repeat this until your food is finished or song is over. So you performed your tasks
concurrently.

You can sing or eat at a time not simultaneously. Don’t mind being Jerry ؉

Concurrency means executing multiple tasks at the same time but not necessarily
simultaneously. In a concurrent application, two tasks can start, run, and complete
in overlapping time periods i.e Task-2 can start even before Task-1 gets completed.

In the computer science world, the way how concurrency is achieved in various
processors is different. In a single core environment (i.e your processor is having a
single core), concurrency is achieved via a process called context-switching. If its a
multi-core environment, concurrency can be achieved through parallelism.

Execution of tasks in a single core environment. Tasks are context switched between one another.

Parallelism
Consider you are given two tasks of cooking and speaking to your friend over the
phone. You could do these two things simultaneously. You could cook as well as
speak over the phone. Now you are doing your tasks parallelly.

Parallelism means performing two or more tasks simultaneously. Parallel computing


in computer science refers to the process of performing multiple calculations
simultaneously.

Two tasks are being performed simultaneously over the same time period.

How is concurrency related to parallelism?


Concurrency and Parallelism refer to computer architectures which focus on
how our tasks or computations are performed.

In a single core environment, concurrency happens with tasks executing


over same time period via context switching i.e at a particular time period, only a
single task gets executed.

In a multi-core environment, concurrency can be achieved via parallelism


in which multiple tasks are executed simultaneously.

Threads & Processes


Threads
Threads are a sequence of execution of code which can be executed independently
of one another. It is the smallest unit of tasks that can be executed by an OS. A
program can be single threaded or multi-threaded.

Process
A process is an instance of a running program. A program can have multiple
processes. A process usually starts with a single thread i.e a primar y thread but
later down the line of execution it can create multiple threads.
Distribution of Processes and Threads in an application.

Synchronous and Asynchronous


Synchronous
Imagine you were given to write two letters one to your mom and another to your
best friend. You can not at the same time write two letters unless you are a pro
ambidextrous.

In a synchronous programming model, tasks are executed one after another. Each
task waits for any previous task to complete and then gets executed.

Asynchronous
Imagine you were given to make a sandwich and wash your clothes in a washing
machine. You could put your clothes in the washing machine and without waiting
for it to be done, you could go and make the sandwich. Here you performed these
two tasks asynchronously.

In an asynchronous programming model, when one task gets executed, you could
switch to a different task without waiting for the previous to get completed.
Synchronous and Asynchronous in a single and multi-
threaded environment
Synchronous
Single Threaded:

Each task gets executed one after another. Each task waits for its previous task to get executed.

Multi-Threaded:

Tasks get executed in di erent threads but wait for any other executing tasks on any other thread.

Asynchronous
Single Threaded:

Tasks start executing without waiting for a di erent task to nish. At a given time a single task gets
executed.

Multi-Threaded:

Tasks get executed in di erent threads without waiting for any tasks and independently nish o their
executions.

What is the role of synchronous and asynchronous programming in


concurrency and parallelism?
Asynchronous programming model helps us to achieve concurrency.

Asynchronous programming model in a multi-threaded environment is a way to


achieve parallelism.

Summing up
Concurrency and Parallelism ->
Way tasks are executed.

Synchronous and Asynchronous ->


Programming model.

Single Threaded and Multi-Threaded ->


The environment of task execution.
Concurrency vs Multi-threading vs
Asynchronous Programming
: Explained
Posted on July 29, 2015 by Brij

Recently, I was speaking in an event and I asked a question about Asynchronous


programming to the audience, I found that many were confused between multi-threading and
asynchronous programming and for few, it was same. So, I thought of explaining these terms
including an additional term Concurrency. Here, there are two completely different concepts
involved, First – Synchronous and Asynchronous programming model and second – Single
threaded and multi-threaded environments. Each programming model (Synchronous and
Asynchronous) can run in single threaded and multi-threaded environment. Let’s discuss each
in detail.

Synchronous Programming model – In this programming model, A thread is assigned to


one task and starts working on it. Once the task completes then it is available for the next
task. In this model, it cannot leave the executing task in mid to take up another task. Let’s
discuss how this model works in single and multi-threaded environments.

Single Threaded – If we have couple of tasks to be worked on and the current system
provides just a single thread, then tasks are assigned to the thread one by one. It can be
pictorially depicted as

Here we can see that we have a thread (Thread 1 ) and four tasks to be completed. Thread
starts workingon the tasks one by one and completes all. (The order in which tasks will be
taken up, does not affect the execution, we can have different algorithm which can define the
priorities of tasks)

Multi-Threaded – In this environment, we used to have multiple threads which can take up
these tasks and start working on that. It means we have a pool of threads (new threads can
also be created based on the requirement and available resources) and bunch of tasks. So
these thread can work on these as

Here we can see that we have four threads and same number of tasks to complete. So each
thread takes up one task and completes that. This is an ideal scenario but in normal cases,
we used to have more number of tasks than the number of available threads. So, whichever
thread gets free, takes up another task. As already mentioned that spawning new thread is
not an option each time because it requires system resources like CPU, memory.

Now, Let’s talk about Asynchronous model and how does it behave in single and multi-
threaded environment.

Asynchronous Programming Model – In contrary to Synchronous programming model,


here a thread once start executing a task it can hold it in mid, save the current state and start
executing another task.

Here we can see that a single thread is responsible to complete all the tasks and tasks are
interleaved to each other.

If our system is capable of having multiple threads then all the threads can work in
asynchronous model as well
Here we can see that same task say T4, T5, T6.. are handled by multiple thread. This is the
beauty of this scenario. As you can see that T4 was started first in Thread 1 and completed
by Thread 2. Similarly, T6 is completed by Thread 2, Thread 3 and Thread 4. It shows the
maximum utilization of the threads.

So till now we have discussed four scenarios –

1. Synchronous Single Threaded


2. Synchronous Multi-Threaded
3. Asynchronous Single Threaded
4. Asynchronous Multi-Threaded

Let’s discuss one more term – Concurrency.

Concurrency

In simple terms, concurrency means processing multiple requests at a time. As we have


discussed two scenarios where multiple requests were getting processed, Multi-threaded
programming and asynchronous model (single and multi-threaded both). In case of
asynchronous model whether single or multi-threaded, multiple tasks are in progress at a
time, some are in hold state and some are getting executed. There are many flavors of it but
that is beyond the scope of this post.

As discussed earlier, the new age is of asynchronous programming. Why is it so important?

Benefits of Asynchronous Programming

There are two things which are very important for any application – Usability and
Performance. Usability because say a user clicks on a button to save some data. This
requires multiple smaller tasks like reading and populating data in internal object, establishing
connection with SQL and saving it there etc. As SQL runs on another machine in network and
runs under different process, it could be time consuming and may take bit longer. So, if the
application runs on a single thread then the screen will be in hanged state till all the tasks
completes which is a very bad user experience. That’s why nowadays many applications and
new frameworks completely rely on asynchronous model.

Performance of an application is also very important. It has been seen that while executing a
request, around 70-80% of the time gets wasted while waiting for the dependent tasks. So, it
can be maximum utilized by asynchronous programming where once the task is passed to
another process (say SQL), current thread saves the state and available to take another task.
When the SQL task completes any thread which is free, can take it up further.

Asynchrony in ASP.NET

Async in ASP.NET can be a major boost in the throughput of your application. Let’s see, how
IIS processes a request.

When a request is received by IIS, it takes a thread from CLR thread pool (IIS does not own
any thread-pool itself instead uses CLR thread pool) and assigns to it which further processes
the request. As number of threads are limited and new ones can be created at a certain limit
then if the thread wastes most of the time in waiting then it’s going to hit hard to your
application’s overall performance. But if you write asynchronous code (which now become
very easy and can be written almost similar to synchronous using the new keywords
async/await in C#) then it will be way faster and throughput of your server will increase
significantly because instead of waiting something to complete, it will be available to thread
pool to take the new requests. If the application has a lot of dependency and long running
process then for those application, async programming will be not less than a boon.

So now we have understood the difference in multi-threading, asynchronous programming


and the kind of benefit we can get using asynchronous programming model.
What is asynchronous and synchronous in Java?
Java code executes line by line. Suppose if you are reading a set of large files having exam
details of the schools of the whole state, where one file has data of one school's exam results.

Further you want to sort the student records by total marks per class and show on UI. If you write
this as normal java code (i.e. without multithreading), each line would execute one by one. You
cannot process another file if one file is not yet done processing. If you want to do any parallel
operation for example: Creating DB connection, you can't do, because it will happen only after
your file processing is done. This sequential behaviour can be termed as Synchronous
behaviour.

Now you can very well read and process a sub-group of files without interrupting the processing
of other files by executing tasks in parallel (aka Threads).This phenomena is called
as Multithreading . Caller (the one who needs the result) can be notified when the processing is
complete and the result can then be used for further processing. So, the execution which happens
independently, without interrupting the Normal flow of execution is called Asynchronous call.

There are a couple of ways in which you can make asynchronous calls in Java depending upon
your requirement. (But contrlling them well is extremely important and tricky at times.)

In addition to Runnable interface (which is used to just execute tasks but doesn't return anything
to caller) ,you can read about Callable interface and Future objects in java. (Which can return
data to the caller)

This snippet is from oracle blog

Das könnte Ihnen auch gefallen