Suggested Projects


Skill-Building Exercises

  1. Modifying a Thread:   Modify ClockPanel1 to call TreePrinterUtils.printThreads() at the end of main() and also the tenth time paintComponent() is called. Notice what happened to the main thread?

  2. Why Threads are Necessary:   The following code doesn’t do what it was intended to do. Fix it. When the user clicks the “Start” button, the program should start incrementing the count variable. Whenever the user presses the “Update Display” button, the label should show the current value of count.

Example 16.26: chap16.exercises.BadCounter.java

image from book
 1     package chap16.exercises; 2 3     import java.awt.BorderLayout; 4     import java.awt.Container; 5     import java.awt.event.ActionEvent; 6     import java.awt.event.ActionListener; 7 8     import javax.swing.JButton; 9     import javax.swing.JFrame; 10    import javax.swing.JLabel; 11 12    public class BadCounter extends JFrame { 13      private long count = 0; 14      public BadCounter() { 15        Container contentPane = getContentPane(); 16 17        JButton startButton = new JButton("Start"); 18        startButton.addActionListener(new ActionListener() { 19          public void actionPerformed(ActionEvent e) { 20            while (count < Long.MAX_VALUE) { 21              count++; 22            } 23          } 24        }); 25     26        final JLabel countLabel = new JLabel("Press \"Start\" to start counting"); 27     28        JButton updateButton = new JButton("Update Display"); 29        updateButton.addActionListener(new ActionListener() { 30          public void actionPerformed(ActionEvent e) { 31            countLabel.setText(String.valueOf(count)); 32          } 33        }); 34 35        contentPane.add(BorderLayout.NORTH, startButton); 36        contentPane.add(BorderLayout.CENTER, countLabel); 37        contentPane.add(BorderLayout.SOUTH, updateButton); 38 39        pack(); 40        setDefaultCloseOperation(EXIT_ON_CLOSE); 41      } 42      public static void main(String[] arg) { 43        new BadCounter().show(); 44      } 45     }
image from book

  1. Shifting the Reponsibility for Synchronization:   Modify chap16.race.Breaker to use an instance of SynchedLongSetter and verify that this approach prevents race conditions.

  2. Choosing Your Locks:   Without changing chap16.deadlock.Breaker at all, rewrite Caller so that only one thread can call Caller.call() at the same time, only one thread can call Caller.answer() at the same time (this is already true of the first version of Caller) but without vulnerability to deadlock. Hint: use different locks. Test this by running chap.16.deadlock.Breaker.

  3. (a) Producer-Consumer Practice:   Following the template below, implement DigitHolder in a new class named BufferedDigitHolder. It should maintain an array of digits. The consumer should be able to retrieve digits as long as there are unconsumed digits and the producer should be able to store digits as long as there is space to put them without overwriting unretrieved digits. Modify PiPanelProdCons to use an instance of BufferedDigitHolder and test its behavior for various sizes between 1 and 1000. It should work correctly for any size greater than zero. Pay attention to System.out to make sure your BufferedDigitHolder is operating correctly.

Example 16.27: chap16.exercises.BufferedDigitHolder.java

image from book
 1      //insert package declarations and imports as desired 2      import chap16.prodcons.DigitHolder; 3 4      public class BufferedDigitHolder implements DigitHolder { 5        private final int[] digits; 6        //insert fields here as desired 7 8        public BufferedDigitHolder(int maxSize) { 9          //insert code here as desired 10         digits = new int[maxSize]; 11       } 12       public synchronized void store(int digit) { 13         //insert code here as desired 14         System.out.println("store: size = " + size()); 15       } 16       public synchronized int retrieve() { 17         int digit; 18         //insert code here as desired 19         System.out.println("retrieve: size = " + size()); 20         return digit; 21       } 22       /** 23        * Returns the current number of stored, unretrieved digits 24        */ 25       public synchronized int size() { 26         //insert code here as desired 27       } 28     }
image from book

  1. (b) Assimilating Your Knowledge:   Producing pi’s digits takes longer than consuming them, and it takes longer and longer as more and more digits have been produced. So, slow the consumer thread down to make it a little slower than the producer when the program starts. As the producer continues to produce digits, its speed will decrease until at some point it will be slower than the consumer. System.out should show the BufferedDigitHolder’s size increase to its maximum size while the producer is quicker than the consumer. Then when the consumer’s speed overtakes the producers speed, you should see the BufferedDigitHolder’s size decrease until it is down to one digit.




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net