cs205: engineering software?
(none)
05 April 2010

CS205 Notes 22 (13 October 2006)

cs205 Friday 13 October 2006

Upcoming Schedule

PS5 Teams

Here are the teams for Problem Set 5:
Sam Brunjes, Patrick Harrison, Kramer Sharp

Grahame Burke, Emily Lam, Rachel Phillips

Michael Lew, Mike Liu, Richard Hsu

Object-Oriented Design

(from Wednesday's class)

What is the difference between an is-a and has-a relationship?









Concurrency

Why is it useful for a programming language to support multiple threads?






Why is concurrent programming harder than sequential programming?






Why is concurrent programming easier than sequential programming?




Yarn Program

class Counter {
   private int count;
   public Counter () { count = 0; }
   public void increment () { count++; }
   public void decrement () { count--; }
   public int getValue () { return count; }
}
  
class IncThread extends Thread {
   private Counter c;
   public IncThread (Counter p_c) { c = p_c; }
   public void run () {
       while (true) {
           c.increment ();
           System.err.println ("Running inc thread: " + currentThread () 
                                       + " / Value: " + c.getValue ());
       }
   }
}

class DecThread extends Thread {
   private Counter c;
   public DecThread (Counter p_c) { c = p_c; }
   public void run () {
       while (true) {
            c.decrement ();
            System.err.println ("Running dec thread: " + currentThread () 
                                        + " / Value: " + c.getValue ());
      }
   }
}

public class Yarn {
    public static void main (String args[]) {
         Counter c = new Counter ();
         IncThread ithread = new IncThread (c);
         DecThread dthread = new DecThread (c);
         ithread.start ();
         dthread.start ();
    }
}

Concurrency Problems

What causes a race condition?




What causes a deadlock?




Java Synchronization

Every object in Java has an associated lock. The synchronized statment is used to create a critical region:
   synchronized (expr) { statements }
Execution will stall until this thread can acquire the lock associated with expr. The lock is held for the duration of the synchronized statement.

Dining Philosophers

The most famous synchronization problem is Djikstra's Dining Philosophers. Five hungry philosophers are sitting around a circular table. Each has an order of General Gao's Chicken to eat, and there is a single chopstick between each philosopher:
A philosopher needs two chopsticks to eat.