Consulting the JavaDocs and the source code of the Thread.interrupt()
method in Java SE 7
I found this:
public void interrupt() {
if (this != Thread.currentThread())
checkAccess();
synchronized (blockerLock) {
Interruptible b = blocker;
if (b != null) {
interrupt0(); // Just to set the interrupt flag
b.interrupt(this);
return;
}
}
interrupt0(); //1, Outside of the synchronized block
}
//...
private native void interrupt0();
As can be seen, the native method invocation at //1
is outside of the synchronized block. So, is it safe if don't put a call to the interrupt()
method into a synchronized
block?
Thread t;
//something else
t.interrupt(); //Not in a synchronized block
Will it be thread-safe? What if more than 1 thread will try to interrupt it simultaneously? How will the native method interrupt0
behave then?
Synchronization is needed when Object is mutable. If shared Object is immutable or all the threads which share the same Object are only reading the Object's state not modifying then you don't need to synchronize it.
In case of synchronized keyword, a thread can be blocked waiting for lock, for an indefinite period of time and there was no way to control that. ReentrantLock provides a method called lockInterruptibly(), which can be used to interrupt thread when it is waiting for lock.
Synchronization is accomplished by controlling the exciter current and the engine speed of the generator. The need for synchronization arrives, particularly when two or more alternators are working together to supply the power to the load.
In Java Threads, if any thread is in sleeping or waiting state (i.e. sleep() or wait() is invoked), calling the interrupt() method on the thread, breaks out the sleeping or waiting state throwing InterruptedException.
I would say yes ... it is thread-safe.
Reasons:
If it was necessary for applications to call interrupt()
in a synchronized
block, then the the spec (the javadoc) would say so, and also say what object you needed to synchronize on to get thread-safety. In fact, the javadoc says nothing about this.
If it was necessary for applications to call interrupt()
in a synchronized
block, then the Oracle Java Tutorial on Concurrency would mention it on this page. It doesn't.
If external synchronization on the Thread
object was necessary to make the interrupt()
call thread-safe, then it is hard to explain why the method is doing internal synchronization as well. (They could / would have made the entire method synchronized if it was necessary.)
The above evidence is (IMO) convincing, though not absolute proof. If you wanted proof that interrupt()
is thread-safe, you would get it by a thorough analysis of the native code implementation for interrupt0()
. I haven't looked at the native code, but I would expect that interrupt0
is internally thread-safe, and that that is sufficient to make the interrupt
method thread-safe.
@xehpuk's question deserves more attention:
Why would it need synchronization? And on which object?
The whole point of synchronization---the only point---is to protect data from corruption. We use synchronization when it is impossible for one thread to advance the state of the program without creating a temporary invalid state that other threads must not be allowed to see.
In that case, we synchronize the code block that creates the temporary, invalid state, and we must also synchronize every code block that ever looks at the state.
So, when we talk about interrupting a thread, what states are we talking about?
Well, without looking at the code, it seems like there would be only two: Not-interrupted and interrupted, and both of them are valid. There's no obvious invalid state to go through to get from one to the other: To get from not-interrupted to interrupted seems like one atomic operation. So, a reasonable programmer would expect that there's no need for synchronization.
Of course, there might be some internal details that I have skipped over, but internal details should be hidden from the programmer. A reasonable programmer would expect that, if there is a need for synchronization, then it either would taken care of inside the interrupt()
method, or else it would be very clearly documented as the caller's responsibility.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With