views:

88

answers:

2

From the java sources, it look like to drops into native code. Is the cost roughly equivalent to a volatile read or does it need to acquire a lock of some type?

A: 

The method isInterrupted is used to checks whether the Thread is interrupted or not and it does not affect the performance. Also it does not reset, if thread was already interrupted. Also See the following links: link text

link text

Venkats
+1  A: 

I don't know whether it acquires a lock or not but I ran a quick test and for me isInterrupted() is roughly 100 times slower than reading a volatile variable. Now whether that would matter or not in your application, I can't tell you.

JRL
Could you post the benchmark that you used?
Michael Barker
@Michael: I did a very crude testing. Basically just looping a few million times and in the loop calling isInterrupted() for the first test, and reading the variable for the second. No other threads involved. I also kept a reference to the value so the loop wouldn't be optimized away.
JRL
Beware of micro-benchmarks! Especially on a JVM. There are many factors that can easily skew the results. The ideal thing to do is to implement this in your application and test it for real. More details at http://www.ibm.com/developerworks/java/library/j-jtp02225.html.
mdma