Week 2 What Is Concurrent and Parallel Programming
Week 2 What Is Concurrent and Parallel Programming
Interacting,
concurrent software
components of a
C o m m u n ic a tio n system:
N e w
t o kr
single machine ->
shared memory
interactions
multiple machines ->
network interactions
• Speed
– If a task takes time t on one processor, shouldn’t it
take time t/n on n processors?
• Availability
– If one process is busy, another may be ready to help
• Distribution
– Processors in different locations can collaborate to
solve a problem or work together
• Humans do it so why can’t computers?
– Vision, cognition appear to be highly parallel activities
• Threads
– Think of a thread as a system “object” containing the
state of execution of a sequence of function calls
– Each thread needs a separate run-time stack
– Pass threads as arguments, return as function results
• Communication abstractions
– Synchronous communication
– Asynchronous buffers that preserve message order
• Concurrency control
– Locking and mutual exclusion
– Atomicity is more abstract, less commonly provided
slide 36
CT074-3-2 Concurrent Programming
The challenge of concurrent
programming
• Concurrent programming is difficult
– Carefully coordinate access to shared data
– Race conditions may create scheduler-dependent
bugs
– Hard to detect and reproduce
• Programming languages offer features to
support concurrent programming
– Synchronization mechanisms:
• semaphores, locks, monitors
– Still have to deal with deadlocks, granularity issues
stop(), or
st run() returns
op
()
Terminated
The predicate isAlive() can be
used to test if a thread has been started
but not terminated. Once terminated, it
cannot be restarted.
Alive
Running sl
su ee
sp p (
en )
d(
)
yield() dispatch
suspend()
start()
Runnable Non-Runnable
resume()
stop(), or
Also, wait() makes a Thread Non-Runnable, run() returns
and notify() makes it Runnable.
• Access modifiers
– Public - may be accessed by any class
– Protected - by subclass
– Private - by the class itself
– No modifier - "package protected", accessed
by classes from the same package.
Q&A