Concurrency Flashcards
sequential computation
each task -> required to complete computation, terminates before the next one can start
concurrent computation
tasks execute in overlapping time periods
synchronous programming
task -> started
program waits for it to complete before moving on
asynchronous programming
task -> started
program moves on before it completes
concurrency
two or more computations are being executed in overlapping time periods
3 types of concurrent activities
- processes
- parallelism
- threads
processes
chunky large coarse grained, few in numbers
parallelism
fine grained, large in number, small actions
threads
in between
concurrency models
shared data state, dataflow, message passing
shared data state
“flock of sheep grazing in grass”
dataflow
“data flowing through the computer agents”
message passing
workers share no data -> communicate via mailbox that are unique to each computational agent
thread is an abstraction for
executing a program
thread encapsulates what 3 things
- instructor pointer
- call stack
- memory
can a single program call multiple threads at the same time?
yes, all with separate pointers, call stacks, and memory
threads in java
lightweight sub processes that share memory
what is the one thread every java program has
main
main thread can cause
new threads to come into existence and run concurrently
thread states
new, runnable, non-runnable, running, terminated
2 ways of implementing threads
- extending thread class
- implement Runnable -> thread intended class
issues of concurrent tasks interfering on shared data
busy waiting
race conditions
dead lock
starvation
ways to prevent issues with concurrent tasks
test and set
spin waiting
mutex lock
semaphores
monitors
condition “variable”
container of threads that are waiting for a certain condition
mutex
class like structure wrapping around a shared resource
allows for locking and mutual exclusion
3 main operations on condition variables
wait c, m
signal c
broadcast c
wait c, m
where c is a condition var and m is a mutex (lock) of the monitor
signal c (notify c)
called by thread to indication the assertion Pc is true
broadcast c (notifyAll c)
similar op to signal but wakes up all threads in c’s wait queue