Basic HTML version of Foils prepared 18 May 97

Foil 11 Threads and Synchronization - Example

From Java Tutorial - Spring 1997 Part 4: Multithreading, useful Java classes, I/O, Networking, and the future Peking Tutorial, Web Certificate -- Spring-Summer 1997. by Nancy J. McCracken,Geoffrey C. Fox


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 Thu Jan 8 1998