Basic HTML version of Foils prepared 17 Nov 97

Foil 14 Threads and Synchronization - Example

From Java Tutorial, July 1, 1996 CEWES Tutorial, CPS606, JSU Class CSC499 -- July 22-25 1997, Fall 97. by Nancy J. McCracken,Geoffrey C. Fox, Tom Scavo

Thread Synchronization Examples for Foil 14
Suppose that several threads are updating a bank balance (i.e., several threads can access one instance of class Account below). Then a thread that finds insufficient funds to debit an account can wait until another thread adds to the account:
public class Account
{ int bankBalance; ...
  • public synchronized void DebitAcct (int amt)
  • { while ((bankBalance - amt) < 0) wait();
  • bankBalance -= amt; ... }
  • public synchronized void CreditAcct (int amt)
  • { bankBalance += amt;
  • notify(); ... } }



© 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 Wed Apr 1 1998