Basic HTML version of Foils prepared Sept 21 1998

Foil 10 Synchronization

From Java Tutorial 98- 4: Multi-Treading, Useful Java Classes, I/O and Networking NAVO Tutorial -- Sept 23 1998. by Geoffrey C. Fox, Nancy McCracken


In Java, two threads can communicate by accessing a shared variable (shared-memory model).
If two threads can both modify an object, that is, they can both execute a method that assigns to a shared variable, then the modifications must be synchronized.
This is easy - just declare the method to be synchronized!
This means that Java will ensure that only one thread executes the method at a time.
Suppose more than one thread can access an account:
  • public class Account
  • { int bankBalance; ...
  • public synchronized void CreditAcct(int amt)
  • { ... bankBalance += amt; ... }}



© 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 Sat Nov 28 1998