Does calling interrupt() on a thread create happens-before relation with the interrupted thread

Viewed 306

In other words I want to know if changing variable before interrupt is always visible when interrupt is detected inside interrupted thread. E.g.

private int sharedVariable;

public static void interruptTest() {
    Thread someThread = new Thread(() -> {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            // Is it here guaranteed that changes before interrupt are always visible here?
            System.out.println(sharedVariable);
        }
    });
    someThread.start();
    Thread.sleep(1000);
    sharedVariable = 10;
    someThread.interrupt();
}

I tried to find answer in Java language specification and in Summary page of the java.util.concurrent package mentioned in Java tutorial but interrupt was not mentioned.

I know about volatile and other synchronize primitives but do I need them?

1 Answers
Related