---
# CP Lecture Notes

# 2010 TO DO for next time

## 2. Java and Concurrency
* Why do we do the following in the clock instead of setting a boolean?
  - while(Thread.currentThread() == clockThread) {

## 3. Safety
- Swapped Checking Safety and Conditional Synchronization
* Add safety property to check for conditional synchronization example?

## 4. Safety Patterns
- 10-15 minutes short; ends abruptly; add something?

## 5. Liveness
* What is best practice for handling InterruptedException

## 7. Asynchrony
* Forgot to show Lab solutions -- add a slide as a reminder!
* Slide 19 - tail calls - access to state in sendNotification should be synchronized! (via a getter)
* Early replies -- show a simpler example with just a new Thread but no Slot?
* Show Fibonacci example with Thread Pool?

## 8. Condition Objects
- where is the deadlock cycle in nested monitors?
* Finished in 60 minutes!
* BoundedCounterCondition: explain logic
  - why do we need to sync on notMin? (could lose signals) 

## 9. Fairness
* Add examples for kinds of priority in slide 7?
* Add examples for each of the fairness definitions in slide 8
* When can we use pass-throughs and lock-splitting? (slide 9)
  - limited applicability?
* Add some explanation of the priority operator used in slide 18

## 10. Petri Nets
* Remove second EToys implementation slide (leftover draft version?)

## 11. Lab 2
- Golf exercise has a race condition? Put a player 5 and two players 1 in the queue.  When the player 5 is done, both players 1 get balls but only 1 is hired out!

## 12. Architecture
* Remove old WordCounter (move to parallelism lecture)

## 13. Parallelism (notes)
- Ask Cortesi for material from his parallelism course?
- "parallelism" means simultaneous or "physically at the same moment" -- but what does this mean? In physics "simultaneous" is not well-defined due to relativity effects.
- Would be nice to have a high-level description of one of these cluster-based physics simulations!
Alternatively get a real example from the cluster users!
- Steps to create parallel programs: compare to Carriero & Gelernter
- Check out Rodric Rabbah slideware.
- NB: with many kinds of parallelism, total time is often bounded by the slowest process (task and hybrid forms).
- "Stealing queue" for thread pools to steal tasks from other threads?
- Select max in parallel -- can we simplify the code? Split into algorithm and decomposition? Needs a diagram?
- Similarly can we solve megesort in a way that separates the algorithm from the management o parallelism?
- Problem with word counter: contention due to synchronization (too coarse in original version) leads to slowdown already after 2 threads.
- With word counter tuning see that extra threads may help by ensuring that there is always a thread to do something. With threads equal to cores might be slower due to waiting threads. Optimum is often # cores + 1. Threshold may vary.

---
# 2010 CHANGES

- Changed course title to: "Concurrency: State Models and Design Patterns"
  (contrast Pascal Felber's course on foundations and algorithms)

## 2. Java and Concurrency
- FSP syntax: guarded action does not require choice

## 3. Safety
- Added busy-wait logic as note to slide

## 7. Liveness and Asynchrony
- Adapted future sequence diagram to example solution!
- Moved JUC discussion here

## 8. Condition Objects
- Discuss how to fix the slot nested monitor
- Discuss more solutions to Nested Monitors
  - lock just a single object (host or CO)
  - remove host synchronization (if immutable)
- Added example of JUC semaphores

## 12. Architecture
* Simple thread pool example -- do something concrete
- (1) Fibonacci or (2) filling an array with values (one task per array slot)

* Add something on transactional memory?
http://en.wikipedia.org/wiki/Software_transactional_memory
AtomJava, JSTM, Deuce

## 13. Parallelism
* Add a lecture on parallelism (FP and JR)

- See: http://groups.csail.mit.edu/cag/ps3/index.shtml

---
# JCP topics:
- p 35 Stale data; p 37 Volatile variables; p 39 Publication & escape
- p 42 Thread confinement; p 52 Safe publication
- p 94 Synchronizers (Latches, Barriers etc)
- p 101 Efficient caching
* p 113 Thread pools; p 170 Thread pool size
- p 211 Open calls (DL avoidance)
- p 216 Thread dumps; p 218 Avoid thread priorities
- p 225 Amdahl's Law
- p 321 Compare-and-swap; p 329 Lock-free algorithms
- p 337 Java Memory Model

---
# 2009 CHANGES (FS-09)
## General:
- Fixed template (logo)
- Added roadmaps
- Removed transparency & shading
- Typewriter -> Courier
- Migrated examples to svn
- add pointer to svn project name for each example in slides!

## 1. Intro
- updated overview, reading list, schedule

## 2. Java and Concurrency
- simplified Clock (use Date instead of Calendar)
- added AccountBAD to illustrate race condition

## 3. Safety and Synchronization
- merged in safety properties from Liveness lecture
- added UML diagram for Slot classes
- made Slot, Producer, Consumer generic
- added ProducerConsumerTest

## 4. Safety Patterns
- added immutable Complex example
- implemented BalkingBoundedCounter
- added test to provoke race condition if synchronization is missing
- made ExpandableArray generic
- implemented LinkedCell
- implemented and extended ResourceVariable example

## 5. Liveness and Guarded Methods
- moved Safety stuff to lecture 3
- merged rest with old lecture 6 (Guarded methods)
- refactored and updated BoundedCounter examples

## 6. Lab session
- updated to generics

## 7. Liveness and Asynchrony [was 8]
- implemented all examples
- refactored Future to no longer use Slot

## 8. Condition Objects [was 9]
- minor cleanup
- replaced CountCondition by new Permit implementation

## 9. Fairness [was 10]
- revised optimistic bounded counter

## 10. Petri Nets
- updated Petit Petri web page
- added some slides on Petit Petri

## 11. Lab2
- updated to generics
- renamed classes:
	ReadersWriters -> SafeReadersWriters
	ReadWrite -> ReadWritePolicy
	SafeReadWrite -> SafeReadWritePolicy
	WritersPriorityReadWrite -> WritersPriorityPolicy
	FairReadWrite -> FairReadWritePolicy
12. Architecture [was 11]
- use generic Slot and autoboxing for active prime example
- implemented Tuple space example with Fly

---
# 2005 CHANGES (W06-1)
Various cosmetic changes, notes etc.

## 1. Introduction
- Converted UML figures to OmniGraffle

## 2. Java and Concurrency
- Changed Java overview slide
- Moved TwoThreadDemo.main to SimpleThread.main
- Rewrote Clock applet as an application (Frame)
- Added Account example to illustrate wait and notify

## 6. Guarded methods
- fixed and animated race condition example

## 8. Liveness and Asynchrony
- Revised Future and FutureDemo to force some interleaving

## 9. Condition Objects
- Added material from Bowbeer, Goetz, Holmes, Lea and Peierls
  [courtesy Doug lea]

## Labs
- converted all lab applets to applications

---
# 2003 CHANGES

## 1. Intro
- added sequence diagram to OCCAM example

## 2. Java and Concurrency
- Added FSP Syntax Summary
- added call to start() (java.lang.Runnable)
- added clock applet screenshot

## 5. Safety and Liveness Properties
- added note & question about transparency
- added property Ok (Busy-Wait revisited)
- added SAFE COIN property and REPICK property
- added race condition to dining phils table

## 6. Liveness and Guarded Methods
- added notify vs notifyAll race condition

## 8. Liveness and Asynchrony
- fixed race condition in Future implementation!
  - put val back into slot
- added some examples

## 10. Fairness and Optimism
- added notes to explain how RW-PROGRESS *lowers* priority
  of release ops!

## 13. Petri Nets
- made special non-animated version

## Lab 1
- made problems & solutions different packages (for netscape)
- created ant build files for all
- cleaned up some HTML
- moved redundant package imports
- Switched Rotator and Garden exercises
- Added more hints to Rotator exercise

## Lab 2
- Removed thread.stop from Rotatingpanel.java!!!
- Added figures to RW showing how to overlap times!
- fixed layout RW (cuts off writer #4)
- fixed layout of single lane bridge! (cuts off menu choice)

---
