Threads Lab II

CS 245

Oct. 13, 2008

  1. Download Synch1.java from the class home page. Read through the program documentation, so that you understand what the program does. Compile and run it.
    1. What are the three synchronization problems possible here?

    2. Following the execution of consumer.start() in init(), exactly how many threads are executing?

  2. Download Synch2.java from the class home page. Compare the implementation of the Queue class there with the original Queue class. This is the only change between the two programs. Compile and run Synch2.java.
    1. Notice there are two commented-out declarations in the Synch2 class as well as two commented-out statements in that class' init() method.

      Predict what will happen if you allow those four statements to execute. Confirm your prediction by removing the comment symbols and compiling and running the program. Re-insert the comment symbols.

    2. A queue of depth one isn't very useful. Modify the implementation of the Queue class so that it can store up to five items.

      To test your revised Queue class, modify the Producer class so that it produces 20 integers and sleeps for only 100 ms. between producing items.

      You may find the following helpful if you've forgotten how to declare an array in Java:

      int[] values = new int[5];
      
      ...
      
      values[head] = v;
      



Thomas P. Kelliher 2008-10-10
Tom Kelliher