public class SomeThreadedClass extends Thread { private static Vector sharedObject; //shared data private Integer localObject; //not shared private void accessSomeSharedObject() { // change values in sharedObject ... } public synchronized void doSomeOperation() { accessSomeSharedObject(); } public void run() { while(someCondition) { doSomeOperation(); // accesses shared data doSomeOtherOperation(); // no shared data } } public void doSomeOtherOperation() { // change values in localObject ... } ... } |
The doSomeOperation
synchronizes on this
.
Each instance of SomeThreadedClass
observes a separate this
; thus, the synchronized method does not block the other
threads from accessing sharedObject
and a race condition can
occur (in fact, each SomeThreadedClass
instance executes in a
separate run
method).
© 1996-2000 Marty Hall, 1999-2000 Lawrence M. Brown