Homework 7Threads |
Modified: |
Assignment 1 - Threads
You are given a program that can control the sequence in which
threads execute rather than in any order as is the norm. Each thread is
numbered 1, 2, or 3. By invoking a Manager class method enter(n),
threads can be forced to wait to execute in their sequence turn. By
invoking a Manager class method exit(n) a thread signals that it
has completed and the next thread in sequence can execute.
to the run( ) method so that any thread can print Start but thread 3 always prints Working 0-4 and Completed before thread 2, thread 2 always prints Working 0-4 and Completed before thread 1.
Assignment 2 - Threads
The purpose of the assignment is to gain some experience implementing and understanding simple threaded applications. There are four short programs to implement.
public class HW7_1 {
public static void main(String args[]) throws Exception {
First first;
Last last;
System.out.println("------HW7-1-----");
first = new First();
last = new Last();
System.out.println("Start");
last.start();
first.start();
System.out.println("Finish");
}
}
class First extends Thread
{ public void run(){
System.out.print("F");
System.out.print("i");
System.out.print("r");
System.out.print("s");
System.out.println("t");
}
}
class Last extends Thread
{ public void run(){
System.out.print("L");
System.out.print("a");
System.out.print("s");
System.out.println("t");
}
}
|
First
Finish
LFLFLFLFLFLFLFLFLaiaiaiaiaiaiaiaiasrsrsrsrsrsrsrsrst
st
st
st
Hints:
Last
First
Last
First
Last
First
Last
:
Hint:
Share a single object between all threads. Synchronize execution using the shared object.
First
Last
First
Last
:
Complete the begin and next methods of class Turn to synchronize two threads to take turns to print in the order above. No other modifications are needed.
Hint:
public class HW7_4 {
public static void main(String args[]) throws Exception {
System.out.println("------HW7-4-----");
Turn turn = new Turn(0);
for (int i = 0; i<50; i++)
new First(turn).start();
for (int i = 0; i<50; i++)
new Last(turn).start();
}
}
class Turn {
int next;
Turn(int next) { this.next = next; }
synchronized void begin(int me) {
}
synchronized void next(int them) {
}
}
class First extends Thread
{ Turn turn;
public First(Turn turn) { this.turn = turn;}
public void run(){
turn.begin(0);
System.out.print("F");
System.out.print("i");
System.out.print("r");
System.out.print("s");
System.out.println("t");
turn.next(1);
}
}
class Last extends Thread
{ Turn turn;
public Last(Turn turn) { this.turn = turn;}
public void run(){
turn.begin(1);
System.out.print("L");
System.out.print("a");
System.out.print("s");
System.out.println("t");
turn.next(0);
}
}
|
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.
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);
}
}
|
Document last modified: