Consulting the JavaDocs and the source code of the Thread.interrupt()
method in Java SE 7
I found this:
public void interrupt() {
@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.
Yeah, source says interrupting a dead thread has no effect. So it'll be threadsafe inherently.
It says "Interrupting a thread that is not alive need not have any effect."
Interrupts this thread. Unless the current thread is interrupting itself, which is always permitted, the checkAccess() methodof this thread is invoked, which may cause a SecurityException to be thrown. If this thread is blocked in an invocation of the wait(), wait(long), or wait(long,int) methods of the Object class, or of the join(), join(long), join(long,int), sleep(long), or sleep(long,int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException. If this thread is blocked in an I/O operation upon an java.nio.channels.InterruptibleChannel interruptiblechannel then the channel will be closed, the thread's interruptstatus will be set, and the thread will receive a java.nio.channels.ClosedByInterruptException. If this thread is blocked in a java.nio.channels.Selector then the thread's interrupt status will be set and it will return immediately from the selection operation, possibly with a non-zero value, just as if the selector's java.nio.channels.Selector wakeup method were invoked. If none of the previous conditions hold then this thread's interrupt status will be set. Interrupting a thread that is not alive need not have any effect.
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.