2

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?

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
Deep Roy
  • 89
  • 10

4 Answers4

3

Is it necessary for a thread in java to be in ready state before it gets interrupted by interrupt method?

The interrupt method doesn't really interrupt the thread. It only sets the interrupted status of the thread on which it is called. That said, if you reverse the call to the interrupt method and the start method, you will notice that the Thread doesn't get interrupted.

MyThread t = new MyThread();
t.interrupt();
t.start();

This should confirm that for the Thread.interrupt method to have an effect on a Thread, the minimum requirement would be that start was called on the Thread before the interrupt method.

Note There is no thread state called the ready state. What you are referring to is the RUNNABLE state which indicates that start was called on the Thread

Chetan Kinger
  • 15,069
  • 6
  • 45
  • 82
1

What happens here is

1) the thread you start needs time to get ready to run, so “end of main thread” is probably going to print first, though that’s not guaranteed.

2) the interrupt flag is set before the new thread is done starting, but nothing checks the status of that flag until the thread sleeps. When you call interrupt on a thread that only sets the flag, the thread doesn’t do anything to respond unless you put in calls to things like sleep or isInterrupted. So “I am lazy Thread” will show up before “I got interrupted”.

Interruption is voluntary and requires the cooperation of the interrupted thread. The thread can’t act on the interrupt flag status until it is running, because some code has to check the flag and act on it.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
  • I appreciate your response but what I am asking is that whether the thread on which the interrupt call has been made , needs to be in a ready state before sleep is called . Then before getting into interrupt state the thread must be in sleep state. Thus transitively to get the thread interrupted the thread must be firstly in ready state. Isn't it? – Deep Roy Nov 11 '17 at 14:00
  • @Deep: is that better? – Nathan Hughes Nov 11 '17 at 14:04
  • Moreover there can be times that the child thread gets called first after the t.start() in the code – Deep Roy Nov 11 '17 at 14:05
  • For the thread to act on the interrupt state it has to be running. Don’t know how to say it more clearly. – Nathan Hughes Nov 11 '17 at 14:09
  • yeah ...that's what I wanted to hear that the thread to get interrupted ..it must be in a ready state before the interrupt has been called...I got it...thank you – Deep Roy Nov 11 '17 at 16:00
0

It is possible that the line System.out.println("End of main thread"); was executed before the threads start. On my computer the program may print End of main thread first, second or last.

As for your question, Java thread don't have a state called "ready" nor "interrupted" (see documentation). Thread.interrupt() just causes a thread to change its state to "TERMINATE".

Also see here:

Interrupting a thread that is not alive need not have any effect.

Dabiuteef
  • 465
  • 4
  • 14
0

If you want the main thread to wait for the other thread to finish what it has to do you should do t.join().

Bruno
  • 11
  • 1