package com.thread;
public class Reader extends Thread {
Calculator c;
public Reader(Calculator calc) {
c = calc;
}
public void run() {
synchronized(c) {
try {
System.out.println("Waiting for calculation...");
c.wait();
} catch (InterruptedException e) {
}
System.out.println("Total is: " + c.total);
}
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
// starts three threads that are all waiting to receive
// the finished calculation
new Reader(calculator).start();
new Reader(calculator).start();
new Reader(calculator).start();
// start the calculator with its calculation
calculator.start();
}
}
class Calculator extends Thread {
int total;
public void run() {
synchronized(this) {
for(int i = 0; i < 100; i ++) {
total += i;
}
notifyAll();
}
}
}
Monday, June 29, 2009
Using notifyAll()
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment