Re: thread related questions
EJP wrote:
puzzlecracker wrote:
does interrupt method blocks, waiting for A to finish wait/sleep and
then interrupting it?
No, it interrupts it immediately.
Does it asynchronouysly let jvm know to cancel
thread A upon the return, and those returns immediately?
It returns immediately. I have no idea whether the operation is
synchronous or asynchronous, or whether this is even specified, but I
doubt that it matters.
Kinda a good point. I assume that interrupt() is basically a Unix
signal. That is, an interrupt is a little flag that gets set in the
process control block for that process, and then the kernel dispatches
appropriately.
This is pretty easy to envision if there's only one CPU. When the
kernel (or JVM) is setting the interrupt flag, all process are waiting
in some queue, and the kernel is the only thing executing. With
multiple CPUs, there's the issue that ThreadA may be under execution
when some other process tries to interrupt it.
What happens I think has to be architecture dependent. These are
software interrupts, not hardware interrupts, and unless there is some
way in hardware CPU-B can slap CPU-A and tell it to stop executing,
there's no way to deliver the interrupt immediately. I guess one has to
assume that the interrupt is asynchronous for both threads, and it's
only processed when the JVM actually gets around to it.