HELP! * GREY=local LOCAL HTML version of Foils prepared July 10 1996

Foil 117 Threads and Synchronization - Example

From Basic Lectures on Java Language Applets Graphics Networking Trip to China and Icase Tutorial -- July 12-28 and June 10-13 96. by Geoffrey C. Fox * Critical Information in IMAGE

public class AddApplet extends Applet implements Runnable {
  • Thread t;
  • boolean go_ahead = false;
  • int a, b, totle;
  • public void start() {
    • t = new Thread(this);
    • t.start();
  • }
  • public synchronized void run() {
    • while ( true ) {
    • while ( !go_ahead ) wait(); // wait for notify signal
    • totle = a + b;
    • go_ahead = false;
    • System.out.println("totle = "+totle);
    • }
  • }
  • public synchronized void adder(int i, int j) {
    • a = i; b = j;
    • notify(); // Notify the thread that a,b are ready
  • }
}



Northeast Parallel Architectures Center, Syracuse University, npac@npac.syr.edu

If you have any comments about this server, send e-mail to webmaster@npac.syr.edu.

Page produced by wwwfoil on Tue Feb 18 1997