Is it necessary for a thread in java to be in ready state before it gets interrupted by interrupt method? I tried to check this by typing the above given code below.
class MyThread extends Thread
{
public void run() {
try
{
for(int i =0;i<10;i++) {
System.out.println("I am lazy thread");
Thread.sleep(2000);
}
}
catch(InterruptedException e) {
System.out.println("I got interrupted");
}
}
}
public class SleepAndInterruptDemonstrationByDurga {
public static void main(String[] args) {
MyThread t= new MyThread();
t.start();
t.interrupt();
System.out.println("End of main thread");
}
}
The output what I got was always was the below one even after trying many times
End of main thread
I am lazy thread
I got interrupted
Why can't the output be
I am lazy thread
I got interrupted
End of main thread
as according to the code it can be seen that the interrupt method is called first by the main thread. Ultimately I want to ask that are there any situations possible when at first the interrupt call is executed before the the thread got started?