What are the 'synchronized barriers'?

Viewed 326
2 Answers

First of all, as @markspace points out, the document you are looking at is not intended to as a document for Java programmers trying to understand how to write threaded code correctly.

"This is an unofficial guide to implementing the new Java Memory Model (JMM)"

So if you are reading it for that purpose, you are likely to confuse yourself unnecessarily. You should instead be either using Java's higher level concurrency mechanisms (best!), or reading and understanding the JMM spec.


The synchronization barrier (not a "synchronized barrier") is (I believe) referring to terminology the Java Memory Model.

JLS 17.4.4 defines a synchronizes-with relation between various actions. This relation implies a synchronization between two threads:

The source of a synchronizes-with edge is called a release, and the destination is called an acquire.

I am not sure about this (because the Cookbook document does not elaborate), but I think that "a synchronization barrier" in the Cookbook is referring to a physical implementation of a synchronizes-with edge ... whatever that might be.

So what the document is saying here is that the memory barriers it refers to are not the same thing as the mechanisms that implement locks and so on. Which is kind of obvious really.

Look at the name... ... for compiler writers, this should make it clear. Doug Lea, among other people, have build a draft document (a starting point) that compiler writers can start with.

The problem is that a JVM can go beyond, or ignore totally that document, as long as that would be legal. For example:

public void go() {
    
     synchronized(this) {
        int x = 1;
     }

     synchronized(this) {
        int y = 2;
     }

}

That document says that there will be "memory barriers" inserted, basically x = 1 and y = 2 can not move outside the synchronized block. In practice, a JVM will do:

   public void go() {
    
     synchronized(this) {
        int x = 1;
        int y = 2;
     }

}

which is an optimization called "lock coarsening", without any problems. So the document is really just a starting point to define some basic rules.

The "synchronized barriers" refers to this chapter; and it defines the rules for correctly "synchronizing" access to shared variables for proper memory effects to have effect.

Related