Concurrency


Introduction | Concepts of Concurrency | Blender/Hamburger Exercises | Class Exercise

Return To Home Page


Introduction

Once upon a time there was a soda shop that had an original jukebox.   Regulars would come into the restaurant and hope that they would get to play a song on the jukebox.   In order to do this they would have to add their quarter to the line of quarters accumulating on top of the machine.   They would memorize where their quarter was in line, and no regular was ever rude enough to put a quarter that wasn't their own into the jukebox.   When one song ended, the owner of the quarter next in line would put their quarter in and choose their song.

Now you ask, wouldn't that get frustrating and tedious?   You may even think that the design for that jukebox wasn't very clever.   This jukebox was designed with only one thread of control.   This means that only one thing could be done at a time.   This jukebox could only take one quarter and then play one song.   It wasn't until the task of playing the song was finished that the task of handling another quarter and song selection could occur.

If that design seems silly to you, you are in favor of a multi-threaded design, or a design that allows the jukebox to perform more than one task at a time.   In the jukeboxes we are accustomed to, you can insert a quarter to reserve your song while another song is playing.   One thread is in charge of playing the current song while another thread is in charge of putting requested songs on queue.   This thread takes the place of lining quarters up on the outside of the machine.
Back to Top of Page

Concepts of Concurrency

Coupling VS Decoupling
Coupling and decoupling are both described as good practices, but how can this be if their names indicate they are contradictory? Threads are an example of both coupling and decoupling.

Concurrency and ordering are two important aspects of time in code: Why do we use concurrency?
Threads are a way you can achieve concurrency.
They can:
Back to Top of Page

Blender and Hamburger Exercises

Here are a few examples to help you "Analyze Workflow to Improve Concurrency." Lists of actions are analyzed in order to improve their concurrency.   UML diagrams are included to illustrate how concurrency increases the efficiency of making pina coladas or hamburgers.
This is a legend illustrating the symbols used in UML diagrams.

Here is a short description of each of the symbols.
  • Ovals or rounded boxes:
    These have the actions written inside of them.   In our examples, the actions are accompanied with their original number in the list of actions.

  • Thick black lines:
    These are called synchronization bars.   Once all the actions leading to a synchronization bar are accomplished, then you can continue along any lines leaving the bar.

  • Thin lines:
    These lines connect actions.   If an action does not have one of these lines entering it, then it can be started at any time before the next synchronization bar.
Here is a pdf file of the
UML Diagram Legend .
Blender Exercise
  1. Open blender
  2. Open pina colada mix
  3. Put mix in blender
  4. Measure 1/2 cup white rum
  5. Pour in rum
  6. Add 2 cups of ice
  7. Close blender
  8. Liquefy for 2 minutes
  9. Open blender
  10. Get glasses
  11. Get pink umbrellas
  12. Serve





Blender UML Diagram
Hamburger Exercise
  1. Heat grill
  2. Make hamburger patties
  3. Put patties on the grill
  4. Slice cheese
  5. Put cheese on the patties
  6. Put hamburger buns on the grill
  7. Take the buns off the grill
  8. Takes the patties off the grill
  9. Put the patties on the bun
  10. Slice tomatoes
  11. Wash lettuce
  12. Get condiments
  13. Put condiments on bun
  14. Put tomatoes on the bun
  15. Put lettuce on the bun
  16. Serve

Hamburger UML Diagram
Back to Top of Page

Class Exercise

Take this list of tasks and create a UML diagram that improves its concurrency.
It might help if you first put them in groups that need to be done in a certain order.

  1. Call mom
  2. Listen to new CD
  3. Do math homework
  4. Read CSC Homework Chapter
  5. Prepare dinner
  6. Eat dinner
  7. Write computer program
  8. Check email
  9. Watch TV program
  10. Brush your teeth
  11. Go to sleep
Here is one possible answer:

Tasks UML Diagram
Back to Top of Page