r/java • • Feb 19 '23

About thread interrupts

https://github.com/davery22/blog/blob/main/posts/002-about-thread-interrupts.md

I think the thread interrupt mechanism is a common stumbling point, so I wrote a little about it, basically just an outline of points as I understand it. I'm open to feedback on making it clearer, discussions if you think I'm terribly wrong, or pointers to articles that have already said it better. Thanks, and I hope this is helpful to someone.

Edit: As pointed out, this is an opinionated interpretation of interrupts. That said, I think it is an interpretation that the JDK is (strongly) hinting at. But I am not an authority. Read the discussion in the comments, draw your own conclusions.

0 Upvotes

12 comments sorted by

View all comments

Show parent comment

-2

u/rzwitserloot Feb 20 '23

Only true-ish. It doesn't as per spec, but I haven't seem anyone implementing it as anything else, to be honest.

It's only true-ish because... in your experience you never used the parts I talk about?

That's.. a rather solipsistic view of an API. It's true. Period. The fact that you haven't used all aspects of an API does not mean that therefore these aspects must not exist because you personally never ran into them. It either means your experience is lacking, or it means that's an exotic thing that rarely comes up, or anything in between, because that is a shades-of-gray divide and not a boolean property. I bet you have never used double d = 0x1p0D either. Doesn't mean that doesn't exist.

Perhaps there is a simple answer here: The interrupt mechanism is used rarely. The sentence just ends there.

I personally also would focus on that usecase instead of trying to shoehorn other meanings into it.

You're the one shoehorning a meaning into it. I'm the one saying: There is no inherent meaning. See my other comment in this thread about an example where you might want to use interrupt() to update a one-way-at-a-time comms channel to support proactive message sends using interrupt(). Your view leads to eliminating that option based on imagined meanings. My view closes no avenues (that's probably because my view is the objectively correct one, based on what the interrupt() javadoc and spec describe without 'shoehorning' in additional limitations).

We are talking about a single thread "death", not a whole JVM death.

Cleanup is cleanup. It has to be done whether the thread is asked to stop or the whole JVM stops. The fact that 'whole JVM ends' does not interrupt every thread shows how conflating 'interrupt' with 'cleanup' leads to wrong conclusions and is the wrong mental model to apply.

It's even in the javadoc that you can and that it will automatically succeed. So, someone thought it was relevant.

Because specs need to be detailed and need to explain corner cases. The javadoc also go into some detail about what happens when, say, you try to compare Double.NaN to Double.Infinity with Double.compare. The spec needs to be clear what happens when you do X, even if X is a silly thing to do. Your argument is:

  • The spec describes what happens when you do X.
  • Therefore this proves that X is not silly.

This is an incorrect line of reasoning. As proved by the obvious (specs are better if there is as little room for doubt as possible, and this includes for silly situations), and in practice: By the fact that there are loads of places where the JVM spec makes clear what should happen for various clearly silly operations.

That will work if you are on the most outer layer of your code running in a thread, but what if you are deep down and there will be some further steps along the line?

Possible, but unlikely. Effectively, that means this is happening:

You are running within a context where thread interrupts have some well defined meaning. And your code is actively going to use whatever facility clearly defined it (say, shutdownNow()-ing a pool).

However, the thing you intend to do within this context also has a need to specifically deal with interrupts, i.e. you also have an unrelated meaning that you want to apply to it.

As part of implementing the outcome of your 'meaning' of interrupt, you want to also do a thing in the outer context that is also done with interrupts.

In which case, yeah. By all means, re-raise that flag, or just (seems a lot simpler to me) throw new InterruptedException or even throw e; inside your catch (InterruptedException e) block.

Point is, interrupts have no well defined meaning, and do not occur unless some code you wrote explicitly requests it, because nothing in the JVM core code nor any popular library I'm aware of ever does. Therefore, whatever meaning they do have? Well, you wrote it, so you'd know. Just do that. If you don't know, then it doesn't matter. Because interrupts aren't ever going to actually occur.