Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 906
Tomas Avatar asked Jul 23 '17 19:07

Tomas


People also ask

What happens when you call interrupt () on a running thread?

interrupt() occurs while that thread is executing. The . interrupt() method sets the "interrupted" flag for that thread and interrupts any IO or sleep operations. It does nothing else, so it's up to your program to respond appropriately- and check its interrupt flag, via Thread.

What happens if the current thread is interrupted by another thread in thread InterruptedException ()?

If the target thread does not poll the interrupted status the interrupt is effectively ignored. Polling occurs via the Thread. interrupted() method which returns the current thread's interrupted status AND clears that interrupt flag. Usually the thread might then do something such as throw InterruptedException.

What is the use of the interrupt () function of thread class?

An interrupt is an indication to a thread that it should stop what it is doing and do something else. It's up to the programmer to decide exactly how a thread responds to an interrupt, but it is very common for the thread to terminate.

What is the difference between sleep () and interrupt () method?

You use interrupt() when you want to interrupt() another Thread which may be waiting on something. You use sleep() when you want this thread to pause for a bit.


1 Answers

Yes, interrupting a thread T2 from a thread T1 creates a happens-before relationship between T1 and T2, as described in the JLS 17.4.4. Synchronization Order:

If thread T1 interrupts thread T2, the interrupt by T1 synchronizes-with any point where any other thread (including T2) determines that T2 has been interrupted (by having an InterruptedException thrown or by invoking Thread.interrupted or Thread.isInterrupted).

Now that only implies that T1 synchronizes-with the detection of the interrupt T2, while you asked about happens-before. Luckily the former implies the latter from 17.4.5. Happens-before Order:

Two actions can be ordered by a happens-before relationship. If one action happens-before another, then the first is visible to and ordered before the second.

If we have two actions x and y, we write hb(x, y) to indicate that x happens-before y.

  • ...
  • If an action x synchronizes-with a following action y, then we also have hb(x, y).

So you are safe to access sharedVariable knowing that it has (at least) the value written by T1, even without volatile.

like image 109
BeeOnRope Avatar answered Oct 29 '22 11:10

BeeOnRope