Does a thread need to be in a RUNNABLE state before it can be interrupted?

半城伤御伤魂 提交于 2020-12-12 11:54:46

问题


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?


回答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.




回答2:


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




回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/47238048/does-a-thread-need-to-be-in-a-runnable-state-before-it-can-be-interrupted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!