"memory fence"

Aleksey Shipilev aleksey.shipilev at
Wed Mar 9 11:06:32 UTC 2016


In recently committed, there is a
synchronize() method [1], which is used as "memory fence" [2]:

 public synchronized void synchronize() {}

 public void execute(Runnable r, Supplier<AccessControlContext>
ctxSupplier) {
   Runnable r1 = () -> {
     try {;
     } catch (Throwable t) {



How's that supposed to work? Is that supposed to guard from bad Runnable

The problem is, once you get $r via the race, there is no way to recover
with local synchronization (IOW: There is no way to sanitize a racy
input, once it happened. Races are bad like that) And if $r got to you
properly, you don't need to do anything special too (IOW: API may as
well assume it is coming from the current thread).

Therefore, I think synchronize() method there is superfluous.

In fact, assuming that a synchronized method has any *detached* memory
semantics is wrong too -- compilers are known to elide associated
fences. E.g. if ExecutorWrapper is known to never escape a thread, or a
single thread locks on it, and biases a lock towards itself.



More information about the core-libs-dev mailing list