[Texas PLT logo]

COMP 402: Production Programming


Today's Menu:

Homework Discussion

Bounded Buffer Student Solution

  1. We should use generics whenever you can. That means we should used ArrayList _buffer=new ArrayList(); instead of ArrayList _buffer=new ArrayList(); . That way, _buffer can only contain elements of type T. But this is not related to concurrency. In DrJava, however, we don't want to use "raw" types, i.e. classes where you could specify a type. For all Java collections (ArrayList, LinkedList, HashSet, HashMap, etc.), for example, we want to specify the types they contain.
  2. You really only need to notify when the count goes from 0 to 1 or from NUM_BUFFER to NUM_BUFFER-1. In other cases, the situation will not have changed.

The other solutions were similar. What differed was the use of an array instead of a list (there are no generic arrays), the use of notify() instead of notifyAll(), and item 2 above.

Reader Writer Lock Student Solution 1

  1. This may wake up threads that still cannot do anything: If a writer is woken up and gets the lock back first, then all readers and other writers will have to wait.
  2. Since it's nondeterministic which thread gets the lock back first, this solution may be unfair.

Reader Writer Lock Student Solution 2

  1. This solution still wakes up every thread, but the order in which we allow threads to continue is determined by a queue, so it is fair.
  2. The occurrences of waitingReaders--; and waitingWriters--; are not in the proper place; they may be executed even when the matching waitingWriters++; and waitingReaders++; are not executed.
  3. The code that wakes up threads is flawed. If readers have been woken up already, a writer should not be woken up. Otherwise, you will have readers and a writer running concurrently. The code should really look like this:

Instructor Solutions

There are other ways to implement the homework, but here are our solutions:


URL: http://www.cs.rice.edu/teaching/402/09-spring/lectures/conc3/index.shtml
Copyright © 2008-2010 Mathias Ricken