Hi All,
I need some help to understand the output of the code below:
As such the code compiles and runs normally as if none of the interrupt methods were ever there.
1. If we comment only Line 1 then both the threads run simultaneously and they complete normally.
2. If we comment only Line 2 then, both
Thread A and B would throw interrupted exception. Thread A when sleep method is invoked and Thread B when join method is invoked.
3. If we comment only Line 3 then both the threads run simultaneously and they complete normally.
4. If we comment Line 1 and Line 2 both the threads run simultaneously and they complete normally.
5. If we comment Line 1 and Line 3 both the threads run simultaneously and they complete normally.
6. If we comment Line 2 and Line 3 both Thread A and B would throw interrupted exception. Thread A when sleep method is invoked and Thread B when join method is invoked.
These are the results I have arrived during my
testing on this piece of code.
On this forum I searched on interrupt methods and found that we can assume that interrupt method sets a flag that the thread has been interrupted and any method (wait, join or sleep) that blocks the thread on which interrupt method is invoked would result in InterruptedException. This helps in explaining the result 6 above. But I am not sure of the rest.
Please advise.
Thanks