Lec6 - Modern Development Concept - Concurrency
Lec6 - Modern Development Concept - Concurrency
Two tasks are being performed simultaneously over the same time
period.
Processes and
Threads
Process: A process is an instance of a running
program that is isolated from other processes on
the same machine. In particular, it has its own
private section of the machine’s memory. The
process abstraction is a virtual computer . It makes
the program feel like it has the entire machine to
itself – like a fresh computer has been created, with
fresh memory, just to run that program.
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 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 different threads but wait for any other
executing tasks on any other thread.
Asynchronous in a single and multi-threaded
environment.
Asynchronous
Single
Threaded:
Race conditions:
The possibility of incorrect results in the presence of
unlucky timing is so important in concurrent
programming that it has a name: race conditions.
The most common type of race condition is check-
then-act.
Example 1:
Thread Unsafe
Sequence.
Let’s consider the Sequence.java class in Example
1, it is suitable to lost updates. The counter
increase instruction may look like a single action
because of its
compact syntax, it is not atomic, meaning that it does
not execute as a single indivisible operation, instead
it is shorthand for a sequence of 3 discrete
operations: fetch the current value, add one to it, and
write the new value back. This is an example of a
read-modify-write (or check-then- act) operation, in
which the resulting state is derived from a previous
state.
Synchronous and
Asynchronous ->
Programming model.