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.
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.
- Coupling:
Coupling refers to temporal coupling.   This couples processes in time in order to perform events in parallel and conserve time.   Temporal coupling actually helps you decouple processes to see what can be done simultaneously.
Classes and objects are decoupled.   They must do separate tasks that do not rely on each other.   If they are to be done "simultaneously", it should not matter which is done first because they do not affect each other's outcome.
- Decoupling:
Decoupling refers to structural decoupling.   When programs or functions are too reliant on or interlinked with other programs or functions, your code becomes less flexible and more prone to bugs.   Decoupling is a way to increase orthogonality in your code.
Concurrency and ordering are two important aspects of time in code:Why do we use concurrency?
- Concurrency
You must know what can be done at the same time.
- Ordering
You must know what order these things must be done in.
Threads are a way you can achieve concurrency.
- Saves time and utilizes resources more efficiently.
- It produces cleaner code.
They can:
- help you decouple your code.
- make you think about your design rather than programming by coincidence.
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.
Here is a pdf file of the UML Diagram Legend .
- 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.
Blender Exercise
- Open blender
- Open pina colada mix
- Put mix in blender
- Measure 1/2 cup white rum
- Pour in rum
- Add 2 cups of ice
- Close blender
- Liquefy for 2 minutes
- Open blender
- Get glasses
- Get pink umbrellas
- Serve
Blender UML Diagram Hamburger Exercise
- Heat grill
- Make hamburger patties
- Put patties on the grill
- Slice cheese
- Put cheese on the patties
- Put hamburger buns on the grill
- Take the buns off the grill
- Takes the patties off the grill
- Put the patties on the bun
- Slice tomatoes
- Wash lettuce
- Get condiments
- Put condiments on bun
- Put tomatoes on the bun
- Put lettuce on the bun
- Serve
Hamburger UML DiagramBack 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.
Here is one possible answer:
- Call mom
- Listen to new CD
- Do math homework
- Read CSC Homework Chapter
- Prepare dinner
- Eat dinner
- Write computer program
- Check email
- Watch TV program
- Brush your teeth
- Go to sleep
Tasks UML DiagramBack to Top of Page