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?
+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
2010-04-25 10:49:02
Could you post the benchmark that you used?
Michael Barker
2010-04-25 13:00:51
@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
2010-04-25 13:04:59
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
2010-04-27 10:46:58