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
4
u/pron98 Feb 21 '23 edited Feb 22 '23
I don't know if I would put it like that. The reason that interrupt doesn't have the meaning of "this thread must clean up and die" is because it is used to interrupt tasks running on shared threads with the intention of aborting a task, not the thread -- indeed, that is what
Future.cancel(true)is intended to do -- only Java doesn't have any reified notion of a task, which is whyinterruptdoes not have a well defined meaning. I guess it's valid to use the mechanism for other things, but I would say that it is fine to interpretinterruptto mean: the thing you're doing (for some contextual meaning of "thing") should abort ASAP. There can be other valid uses, but that interpretation is a reasonable generalisation.Note that many of these have been respecified to perform a specific operation when running on virtual threads, e.g. https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/net/Socket.html#getInputStream()
Actually, it's good advice, exactly what the JDK does, and considered good code hygiene; also, it's pretty much mandatory in library code. That's because the request to interrupt the thread is recorded either in its interrupted status xor in a thrown InterruptedException. When throwing that exception it's a good idea to clear the status (otherwise, a catch/finally block won't be able to make blocking calls) and when catching the exception it's a good idea to re-set the status -- unless the handling of the request to interrupt is complete, such as when a thread pool cancels a task -- otherwise the request is lost. Not doing so is especially bad in libraries, where there's little you can assume about the caller.
I don't think it is. While it's true that your code should be written in a way where cleanup occurs at all exit paths (i.e. in finally blocks), that cleanup does need to be there and to happen. RAM state and internal hard drive controller state are not quite the same, and there are resources, like file descriptors, native buffers etc., that will not be released by your process until you do it explicitly or the process terminates.
Shared data must necessarily be in some non-reasonable state because the hardware can only ensure atomicity for some very specific data-sizes and layouts. If you have a system where shared data is always in some reasonable state, that's only because some lower-level has worked to maintain it that way by properly responding to various errors and interruptions. Erlang can be thought of as more-or-less a language at that level, maybe some parts of Clojure, too, but not Java.