Interrupting a thread in Java does not mean stopping the execution of this thread. This is not a stop, this is an interrupt. A thread can be interrupted when some fundamental and important change occurs, telling the thread that its execution context, its task, or its environment has changed in some significant way. The flow response to this message is implementation specific. It can be a stop, it can be a restart or any other action. A thread that does not handle interrupts cannot be interrupted, but its behavior can be changed, for example, using a shared variable.
For example, imagine that you have several threads, all of them are viewing part of the problem space for a solution. When one thread finds a solution, it can interrupt other threads because their search for a solution no longer matters. A solution has already been found.
Or imagine one continuous workflow and one network communication flow. Each time a network stream receives a messsage, it interrupts the workflow with the message. Based on what the message and context are, a workflow can decide what to do next. For example, if the message was "STOP", it could immediately stop all execution. If the message was "RESET", it can start again from scratch or maybe not from scratch and reuse previous work based on the execution context.
Is it right to have more than 10 methods, all of which throw InterruptedException ? Is there a better way to do this?
No, thatโs fine if you know what youโre doing. If you are doing interrupts to just stop threads, there is no need to throw InterruptedExceptions. The Thread run () method is the first, and the exception will not go further than the stack.
Is it right to inflate methods with checks on isInterrupted ()?
Depending on the context. Checks were usually added before some important code. Usually it is added as the first element in the loop block.
At the end of the catch block of InterruptedException, do you need to execute 'return' or 'null' specific values โโto make Thread available to the GC? If I recreate Thread, it will take longer than usual initialization.
No. Once Thread exists from the run () method, it remains at the mercy of the GC. Shared variables will not be GC'ed if they are still referenced by other objects.