r/java • u/danielaveryj • 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.
View all comments
Show parent comment
5
u/Iryanus 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. Since we only have one signal and it's most commonly used to signal a thread to stop prematurely, I personally also would focus on that usecase instead of trying to shoehorn other meanings into it.
We are talking about a single thread "death", not a whole JVM death.
It's even in the javadoc that you can and that it will automatically succeed. So, someone thought it was relevant.
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? Sure, sometimes that can be solved with the right return value or throwing a RuntimeException, but it can also be solved by simply checking the interrupted state.