4

I'm currently learning about the Java Memory Model and I came about the different kinds of Actions.

There are two of them which I don't fully understand:

  • External Actions and
  • Thread divergence actions

Please explain those two action types and give examples for them and for their special properties regarding compiler-reordering and happens-before relation. Oh, and are native calls also external actions?

I think they wouldn't define thread divergence actions if there was nothing special about them. So what makes them special so that they need to be defined as a special kind of action?

2 Answers 2

7

The existing answer already correctly defines what these actions are.

As for the reordering: look at the following example of a thread-divergence action:

class ThreadDivergence { 

  int foo = 0; 

  void thread1() { 
    while (true); // thread-divergence action
    foo = 42; 
  } 

  void thread2() { 
    assert foo == 0; 
  } 
}

You would expect this assertion to never fail. Without thread-divergence actions, this could not be guaranteed because the unreachable definition of setting foo = 42 could be reordered to be executed first. This is forbidden by the JMM.

Similarly, external actions are added to avoid surprising outcomes:

class Externalization { 

  int foo = 0; 

  void method() { 
    jni(); // external action
    foo = 42; 
  } 

  native void jni(); /* { 
    assert foo == 0; 
  } */ 
}

Assuming that the JNI method was implemented to run the same assertion, you would not expect this to fail. The JIT compiler cannot determine the outcome of anything external such that the JMM forbidds such reorderings, too.

Sign up to request clarification or add additional context in comments.

1 Comment

Any JVM communicates with "external world" via native methods which are "External Actions". Also external actions have one additional parameter: either exception thrown by that action or the result that is returned by the native method.
0

If I understand correctly, External Action is something visible outside of the Java VM process, such as displaying a window, writing to a file or connectiong to a socket.

Thread Divergence Action must be entering an infinite while(true) loop.

5 Comments

And what are their special properties regarding compiler-reordering? Are all native methods external actions?
or asking the other way, is there a way how a non native method can be external?
The only thing that may be non-native and external that occurrs to me is UNSAFE. But I am not sure if it's non-native...
I think they wouldn't declare divergence actions if there was nothing special about them. So what makes them special?
OK, I am just guessing now, but I think the special part about divergence actions is that once a thread has one, it no longer matters. It's in an infinite loop and will never emerge.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.