1

Is the interruption bit of a java.lang.Thread guaranteed to be set, when a thread is interrupted after it was started (Thread#start() was called) but before it begins its execution (its run method is not yet called by the JVM)?

So that within the thread Thread.interrupted() returns true or a InterruptedException is thrown when a method is called, that reacts to interruption in this way,

Or with an example: Will the following snippet of Java code always, under any circumstances, print true?

AtomicBoolean flag = new AtomicBoolean(false);
Thread thread = new Thread(() -> {
    while (!flag.get()) {}
    System.out.println(Thread.interrupted());
});
thread.start();
thread.interrupt();
flag.set(true);

The java-doc of java.lang.Thread#interrupt() says:

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

And the doc of java.lang.Thread#interrupted() says:

A thread interruption ignored because a thread was not alive at the time of the interrupt will be reflected by this method returning false.

My assumption is that alive from these docs refers to the description in Thread#isAlive():

A thread is alive if it has been started and has not yet died.

From this, my answer would be yes. But I read in some sources that thread interruptions, which are done before the thread is running, are ignored. When I ran the code snippet it always printed true (but we know this isn't a guarantee in concurrent programming). Therefore I'm searching for clarification.

Jonas
  • 121,568
  • 97
  • 310
  • 388
  • 2
    Your code checks the flag as soon as the thread starts. Problem is that the gap betwen `thread.start()` and `thread.interrupt()` could be anything. You are up to the mercy of the scheduler. It could wait for 10 minutes before calling `thread.interrupt()`. At which point the thread is already finished. It's extremely *unlikely* to take 10 minutes, but still. – Michael Nov 02 '20 at 21:44
  • @Michael: That's a valid point. But I'm mainly concerned about the time-span between Thread start and begin of the thread's execution. I can update the example accordingly. – Hannes Wellmann Nov 02 '20 at 21:52
  • I don't see an update, but what Michael is trying to say is that the call to `interrupt` might happen before the call to `interrupted` in the started thread. So, no, it might print `false`. – Savior Nov 02 '20 at 22:04
  • I just updated the example. My point is: What happens if interrupt() is called after the thread is started but is not yet running? Is the interrupted bit of the thread is guaranteed to be set or not? – Hannes Wellmann Nov 02 '20 at 22:11
  • @HannesWellmann I think not. You've posted documentation that is mentioning exactly that. – akuzminykh Nov 02 '20 at 22:12
  • Does this answer your question? [Does a thread need to be in a RUNNABLE state before it can be interrupted?](https://stackoverflow.com/questions/47238048/does-a-thread-need-to-be-in-a-runnable-state-before-it-can-be-interrupted) – Savior Nov 02 '20 at 22:13
  • @Savior: i don't think that answers the question to the desired degree of certainty. – Nathan Hughes Nov 02 '20 at 22:15
  • @Savior thanks for your suggestion but unfortunately Nathan Hughes is right. – Hannes Wellmann Nov 02 '20 at 22:19
  • Then https://stackoverflow.com/questions/45269123/does-calling-interrupt-on-a-thread-create-happens-before-relation-with-the-int – Savior Nov 02 '20 at 22:23
  • @akuzminykh the posted doc mentions that it is ignored when the thread is not alive. But the question is when a thread is considered "alive" regarding interruption? When start() has been called or when the thread has started execution? – Hannes Wellmann Nov 02 '20 at 22:23
  • 1
    @HannesWellmann Ah, now I see. According to [this](https://stackoverflow.com/q/17293304/12323248) it's already alive when `start` is called. Therefore the interrupt should be garanteed to be visible. The [JLS](https://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html) states the same. – akuzminykh Nov 02 '20 at 22:26
  • @Savior thanks again, but I don't see an answer there and think that this isn't a question of the Java memory model. – Hannes Wellmann Nov 02 '20 at 22:31
  • @akuzminykh thanks for the other answer, but in the end this just refers to the java-doc of Thread#isAlive() which I already mentioned in the question. But I could not find anything about if "alive" from the interrupt/interrupted docs refers to the definition in isAlive(). To which exact section in the JLS do you refer? – Hannes Wellmann Nov 03 '20 at 12:16
  • @HannesWellmann It's official documentation; we should assume that the authors have put much effort into being precise with their words. I don't think we'll get further here, so let's do this: *"But I read in some sources that thread interruptions, which are done before the thread is running, are ignored."*, could you post where you got this from so I can check it myself please? – akuzminykh Nov 04 '20 at 03:37
  • @akuzminykh you're probably right, but it it would be nicer if it were explicitly stated. – Hannes Wellmann Nov 07 '20 at 19:49
  • @akuzminykh for example I found this post, where the asking person said it in the introduction: "_Particularly, interrupting a thread between the call to start() and the invocation of run() has no effect._" https://codereview.stackexchange.com/questions/229119/a-thread-that-can-be-interrupted-before-starting Maybe this is a myth and some more people have heard. Therefore this question was intended to gain clarity. – Hannes Wellmann Nov 07 '20 at 19:54
  • @HannesWellmann I think the statement in the question was just wrong and noone noticed because it's such a specific (but nearly irrelevant) detail .. Think about it like that: If an interrupt before `run` actually starts was ignored, then how would you write algorithms around that? You start a thread but you never know when the JVM/OS starts the execution. It could be theoretically anywhen. So any interrupt after `start` would have the (small) chance to be ignored. That's not very deterministic. So rather assume, like everything indicates, an interrupt after `start` *will* have an effect. – akuzminykh Nov 08 '20 at 08:01
  • @akuzminykh that sounds reasoned. Thanks for your comments. – Hannes Wellmann Nov 08 '20 at 16:22

1 Answers1

-1

It depends on scheduler when it is actually starting your thread and when finishing and you dont have control over thread scheduler. It is not gauranteed that thread is immediately started when you call thread.start(). It is possible that main thread has got chance to excute thread.interrupt(); before your thread got CPU chance.

Or with an example: Will the following snippet of Java code always, under any circumstances, print true?

If you have written this in main method then it will always print true because main is deamon thread in java and runs until its child thread gets completed. If main thread exited before thread was scheduled then nothing will be printed

Pandey Amit
  • 657
  • 6
  • 19