Introduction to concurrency Flashcards
Concurrency in the Real World: Coffee Shop
- Customer places Order with Cashier, who places the Order in a queue (as a cup with drink name on it). Customer and Cashier are free to go on to other business.
- Barista takes Orders from the queue and can make two drinks at a time. When a drink is ready, it is placed on counter for Customer to pick up.
- Concurrent because multiple actors: Customer, Cashier, Barista and the two coffee machines.
- Actors decoupled: work concurrently on several Orders at the same time! Highly efficient!
- (A synchronous alternative: Customer places for Order with Cashier and waits there till it is
ready.)
Concurrency Components
Concurrency: Phenomenon
Multiple, independent loci of computation (actors, processes, threads) interacting over shared resources.
Concurrency: In Computer Systems…
- Multiple processes inside an OS
- One or more threads in a single process
- Thread: An actor, that is, an independent locus of computation, realized as a sequence of instructions with own program counter
- Scheduler: Method for selecting which thread to run from the pool of active threads
- E.g., round robin
- Shared resources
- Memory (global, heap)
- Hardware devices
What Makes Concurrency Challenging
- To coordinate the actors (as minimally as possible!) so that collectively, they produce only correct outcomes.
- Examples of Correct Outcomes
- All drink orders should eventually be served, but none more than once.
- Only one person should be out buying food at any time.
- Rule out certain manners of traffic accidents
Why Study Concurrency in Computing?
- Computing concerns representing (in software) the real-world, which is inherently concurrent because of the presence of multiple actors
- Coffee shop
- Business and social interactions
- Teaching
- Multiple Readers and Writers for a file
- Internet of Things: Multiple sensors
- For responsiveness of software to environmental events
- E.g., to be responsive to user input.
- For utilizing computing resources, e.g., CPUs, more efficiently
- Structure system/program into multiple actors that can work on tasks independently so that even if some are blocked waiting for some
information, the others can proceed.
- Structure system/program into multiple actors that can work on tasks independently so that even if some are blocked waiting for some
Java Threads
– Consider a MessagePrinter class with method
print_string() that prints a string in a window
– Conventionally, calling program waits “a long time” for print operation completion before able to continue…
– Can use a thread in MessagePrinter object
– Allow calling program to continue “immediately”
Java Threads
(The “Main” Program part)
Java Threads
Before showing the MessagePrinter class…
Java interfaces review
– Interface similar to classes: method signatures with no method bodies (implementations)
– Implementing an interface is like extending a class
– Must provide an implementation of all method signatures in the interface
Java
Thread creation in Java
Java Threads
MessagePrinter class
Java Threads
Three steps to thread creation in Java
Java Threads Alternative
Summary
– A concurrent system is one that has several independent loci of computation (informally and interchangeably referred to as actors, threads, processes, and so on)
– The challenge is to ensure correct program outputs even when resources are shared between actors
– Concurrency in software is desirable for many reasons
– For modelling concurrency in the real-world
– For making responsive software
– For taking advantage of computing resources
– Don’t let CPU idle if there is a task that can use the CPU while others are blocked on IO (input/output)
– Take advantage of multiprocessor, multicore systems (logically systems with more than one CPU)