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.
public class sequencedThread { public static void main(String args[]) throws Exception { Manager manager = new Manager(3); for (int i=1; i<=3; i++) new NumberedThread(i, manager).start(); HW.println("main completed"); } } class NumberedThread extends Thread { int n; Manager manager; public NumberedThread(int n, Manager manager) { this.n = n; this.manager = manager; } public void run() { HW.println(" [Start Thread " + n + "]"); for(int i=0; i<5; i++) HW.println(" {Working Thread " + n + " <" + i + ">}"); HW.println(" [Completed Thread " + n + "]"); } } class Manager { int next; Manager(int next) { this.next = next; } public synchronized void enter(int n) { HW.println(" (enter - Thread " + n + " next=" + next + ")"); while (n != next) { HW.println(" (enter - wait Thread " + n + " next=" + next + ")"); try { wait(); } catch (Exception e) { } if ( n != next) notify(); } } public synchronized void exit(int n) { HW.println(" (exit - Thread " + n + " next=" + next + ")"); while (n != next) { HW.println(" (exit - wait Thread " + n + " next=" + next + ")"); try { wait(); } catch (Exception e) { } if ( n != next) notify(); } next--; notify(); } } class HW { static void print(String s) { try { Thread.sleep(10); } catch(Exception e){} System.out.print(s); } static void println(String s) { HW.print(s+"\r\n"); } }
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; HW.println("------HW7-1-----"); first = new First(); last = new Last(); HW.println("Start"); last.start(); first.start(); HW.println("Finish"); } } class First extends Thread { public void run(){ HW.print("F"); HW.print("I"); HW.print("R"); HW.print("S"); HW.println("T"); } } class Last extends Thread { public void run(){ HW.print("l"); HW.print("a"); HW.print("s"); HW.println("t"); } } class HW { |
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 {
HW.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);
HW.print("F");
HW.print("I");
HW.print("R");
HW.print("S");
HW.println("T");
turn.next(1);
}
}
class Last extends Thread
{ Turn turn;
public Last(Turn turn) { this.turn = turn;}
public void run(){
turn.begin(1);
HW.print("l");
HW.print("a");
HW.print("s");
HW.println("t");
turn.next(0);
}
}
class HW {
static void print(String s) {
try { Thread.sleep(10); } catch(Exception e){}
System.out.print(s);
}
static void println(String s) {
HW.print(s+"\r\n");
}
}
|
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()) HW.println("Waiting");
public class SimpsonS {
public static void main(String args[]) {
HW.println("----Simpson's----");
Simpson s = new Simpson( 6 );
s.start();
while ( !s.getDone()) HW.println("Waiting");
HW.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;
HW.println("Computing f "+4.0/(1.0+x*x));
return 4.0/(1.0+x*x);
}
}
class HW {
static void print(String s) {
try { Thread.sleep(10); } catch(Exception e){}
System.out.print(s);
}
static void println(String s) {
HW.print(s+"\r\n");
}
}
|
Document last modified: