Introduction to concurrency Flashcards

1
Q

Concurrency in the Real World: Coffee Shop

A
  • 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.)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Concurrency Components

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Concurrency: Phenomenon

A

Multiple, independent loci of computation (actors, processes, threads) interacting over shared resources.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Concurrency: In Computer Systems…

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What Makes Concurrency Challenging

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Why Study Concurrency in Computing?

A
  1. 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
  2. For responsiveness of software to environmental events
    • E.g., to be responsive to user input.
  3. 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Java Threads

A

– 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”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Java Threads
(The “Main” Program part)

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Java Threads
Before showing the MessagePrinter class…

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Java
Thread creation in Java

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Java Threads
MessagePrinter class

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Java Threads
Three steps to thread creation in Java

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Java Threads Alternative

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Summary

A

– 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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly