Solutions to Self-Study Exercises


[Page 702 (continued)]

Solution 14.1

public class PrintOdds implements Runnable {   private int bound;   public PrintOdds(int b) {    bound = b;   }   public void print() {     for (int k = 1; k < bound; k+=2)       System.out.println(k);   }   public void run() {     print();   } } 


Solution 14.2

On my system, the experiment yielded the following output, if each thread printed its number after every 100,000 iterations:

1111112222222211111111333333322222221111113333333 222224444444433333344444445555555544444555555555555 


This suggests that round-robin scheduling is being used.


[Page 703]
Solution 14.3

If each thread is given 50 milliseconds of sleep on each iteration, they tend to run in the order in which they were created:

123451234512345...


Solution 14.4

The garbage collector runs whenever the available memory drops below a certain threshold. It must have higher priority than the application, since the application won't be able to run if it runs out of memory.

Solution 14.5

To improve the responsiveness of an interactive program, the system could give a high priority to the threads that interact with the user and a low priority to those that perform noninteractive computations, such as number crunching.

Solution 14.6

If the JVM were single threaded, it wouldn't be possible to break out of an infinite loop, because the program's loop would completely consume the CPU's attention.

Solution 14.7

If round-robin scheduling is used, each thread will get a portion of the CPU's time, so the GUI thread will eventually get its turn. But you don't know how long it will be before the GUI gets its turn, so there might still be an unacceptably long wait before the user's actions are handled. Thus, to guarantee responsiveness, it is better to have the drawing thread sleep on every iteration.

Solution 14.8

If Dotty's priority is set to 1, a low value, this does improve the responsiveness of the interface, but it is significantly less responsive than using a sleep() on each iteration.

Solution 14.9

In a real bakery only one customer at a time can take a number. The take-a-number gadget "enforces" mutual exclusion by virtue of its design: There is room for only one hand to grab the ticket, and there is only one ticket per number. If two customers got "bakery rage" and managed to grab the same ticket, it would rip in half and neither would benefit.

Solution 14.10

One experiment to run would be to make the clerk's performance very slow by using large sleep intervals. If the algorithm is correct, this should not affect the order in which customers are served. Another experiment would be to force the clerk to work fast but the customers to work slowly. This too should not affect the order in which the customers are served.

Solution 14.11

You should observe that the waiting line builds up as customers enter the bakery, but the clerk should still serve the customers in the correct order.

Solution 14.12

A two-ball version of Pong would require the following changes to the original version:

  1. A new Ball() constructor that has parameters to set the initial location and direction of the ball.

  2. The PongApplet should create a new Ball instance, start it, and draw it.




Java, Java, Java(c) Object-Orienting Problem Solving
Java, Java, Java, Object-Oriented Problem Solving (3rd Edition)
ISBN: 0131474340
EAN: 2147483647
Year: 2005
Pages: 275

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