
190 Introduction to Concurrency in Programming Languages
Listing 8.5: Illustration of unnecessary whole-method locking.
public s ynchronized void f () {
// safe code ...
if ( rare_conditon == true ) {
// operations requirin g concurren c y control
}
// more safe code ...
}
values while the critical section is executing in a different thread, but before
it actually requires it. This other thread will be able to access the value then,
avoiding the blocking scenario that would result if a critical section acquired
all locks before starting. The result of this is a lower probability of two threads
serializing.
Two-phase locking and other similar schemes of relaxed ...