Synchronization: Arbitrating Conention for Shared Resources

1. Synchronizing a Section of Code

synchronized(someObject) {
  code
}

Normal interpretation: once a thread enters the code, no other thread can enter until the first thread exits.

Stronger interpretation: once a thread enters the code, no other thread can enter any section of code that is synchronized using the same “lock” tag.

2. Synchronizing an Entire Method

public synchronized void someMethod() {
  body
}

Note that this is equivalent to:

public void someMethod() {
  synchronized(this) {
    body
  }
}

© 1996-99 Marty Hall, 1999 Lawrence M. Brown