Homework 7

Threads

Modified

Assignment

Assignment 1 - Threads

Assignment 2 - Threads

Assignment 3 - Sequential versus Threaded Execution - The Busy Wait

    The purpose of the assignment is to gain some intuition on sequential versus thread behavior. The following implements a sequential version for approximating pi using Simpson's Rule, a numerical integration algorithm. Roughly put, integration is performed by dividing an interval into n parts (n is even), computing the area of the function for each interval and computing an average area.

    The sequential version below uses 6 intervals (i.e. new Simpson( 6 ); ), increasing the number of intervals increases the accuracy of the approximation of pi.

  1. Run the following sequential version once to observe its behavior.
  2. Increase the number of intervals, compare computed pi with published values.
  3. Make minor modifications (only two or three are necessary) to class Simpson so that objects execute as a thread. Note that the main thread will wait until the Simpson thread completes (i.e. while ( !s.getDone())...). This is a crude form of thread synchronization called busy wait in which the main thread is busy waiting while the Simpson thread has not signaled done (i.e. done = true).
  4. Run the threaded version. You should see print waiting by the main thread and computing by the Simpson thread.
  5. Increase the number of intervals to a much larger number and rerun.
  6. Comment out the following line and rerun the threaded version.

    while ( !s.getDone()) System.out.println("Waiting");
     

    public class SimpsonS {
        public static void main(String args[]) {
            System.out.println("----Simpson's----");
    
            Simpson s = new Simpson( 6 );
            s.start();
    
            while ( !s.getDone()) System.out.println("Waiting");
    
            System.out.println("Approximation of pi: "+s.getArea());
        }
    }
    
    class Simpson {
        int n=0;
        double area=0.0;
        boolean done=false;
    
        public Simpson(int n) { this.n = n; }
    
        public void start() { run(); }
    
        public void run() {
            area = f(0)-f(n);
    
            for(int i=1; i <= n/2; i++) 
                area+= 4.0*f(2*i-1)+2*f(2*i);
            area /= (3.0*n);
            done = true;
       }
    
       public double getArea() { return area; }
       public boolean getDone() { return done; }
    
       public double f(int i) {
          double x;
          x = (double) i / (double) n;
          System.out.println("Computing f "+4.0/(1.0+x*x));
          return 4.0/(1.0+x*x);
       }
    } 

Turn in

  1. A program listing for the threaded Simpson.
  2. Execution output of c.
  3. Comments on comparison of results of a, b, c, d and e.
  4. This is a good problem for parallel execution; indicate:
    1. what part(s) would be threaded,
    2. whether threads require synchronization,
    3. how the final result might be computed.

Document last modified: