Re: JavaMemoryModel: correct synchronization

From: Bill Pugh (pugh@cs.umd.edu)
Date: Mon Feb 23 2004 - 14:55:16 EST


A couple of people seem to be confused by this. Java does not require
that code follow the Eraser protocol, which requires that shared memory
be accessed only while a lock is held (with a few exceptions for stuff
such as initialization).

This model simply doesn't work for a lot of cases. Instead, the JMM
simply requires that all access to shared data be ordered by
synchronization.

For example, consider the following code fragment, that might be part
of some simulation engine:

PriorityBlockingQueue<Event> q = new PriorityBlockingQueue();

public void run() {
    while (...) {
      Event e = q.take();
          // do work to update e,
      // setting the time for the next event
      q.put(e);
      }
     }

Now, PriorityBlockingQueue is thread safe, and performs synchronization
when taking and putting objects. Assume that I have multiple threads
pulling stuff out of the queue.

Then I have exactly the situation people have been taking about: a
shared object, that is being mutated by multiple threads. However,
synchronization ensures happens-before edges between successive
accesses to that object by multiple threads.

        Bill

-------------------------------
JavaMemoryModel mailing list - http://www.cs.umd.edu/~pugh/java/memoryModel



This archive was generated by hypermail 2b29 : Thu Oct 13 2005 - 07:00:58 EDT