University of Virginia, Department of Computer Science
CS201J: Engineering Software, Fall 2003

Notes: Tuesday 28 October 2003
Schedule
Schedule Revisions: The schedule below reflects changes from the original schedule:

Programming Concurrency
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 ();
    }
}

CS201J University of Virginia
Department of Computer Science
CS 201J: Engineering Software
Sponsored by the
National Science Foundation
cs201j-staff@cs.virginia.edu