0

In Java, we can call the interrupt() method to interrupt a thread, and the interrupted thread can then manage that interruption (either by catching InterruptedException, or after checking with Thread.interrupted()).

The OS can also suspend a thread e.g. if it wants to switch in another thread.

Are suspending and interrupting two completely distinct operations? can the OS also interrupt a thread?

alfer
  • 355
  • 2
  • 8
  • Does this answer your question? [Stop, interrupt, suspend and resume a java thread](https://stackoverflow.com/questions/23795360/stop-interrupt-suspend-and-resume-a-java-thread) – fantaghirocco Feb 26 '21 at 13:40
  • it is related, yes, but not exactly my question – alfer Feb 26 '21 at 13:52

2 Answers2

1

Are suspending and interrupting two distinct operations?

As you have described those operations, yes, they are obviously distinct. One happens at the Java level, and the other at the OS level.

Perhaps you mean to ask whether Java thread interrupts are implemented via OS-level thread suspension, but again no. As you have defined suspension, this is the mechanism the OS uses when it schedules a different thread on the CPU. When the suspended thread resumes, then, it will proceed as if nothing had happened. But the point and effect of Java thread interruption is to produce different behavior in the interrupted thread. This is not a scheduling operation, nor even much related to scheduling.

can the OS also interrupt a thread?

Java uses underlying OS facilities to implement thread interruption, so in that sense yes, of course. But the OS does not itself have any sense of Java-level thread semantics, and it knows Java threads themselves only to the extent that they are implemented via OS threads. There is no native facility to reliably or specifically perform Java thread interruption without going through Java.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
1

Suspending and interrupting are complete opposites of each other. The whole purpose of Thread.interrupt() is to get the attention of a thread that is suspended, waiting for something.

Thread.interrupt() does two things;

  1. It sets a flag that the thread can check. Thread.currentThread.isInterrupted(), and

  2. If the thread is in any number of different library calls that suspend the thread to wait for something (e.g., any file I/O call, Thread.sleep(), and I don't know what all else,) then the thread will be resumed, and the library call will throw an InterruptedException instead of returning its usual result.

https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/lang/Thread.html

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57
  • Thanks for your answer. "The whole purpose of Thread.interrupt() is to get the attention of a thread that is suspended". This could happen if we interrupt a thread that was suspended after calling e.g. Thread::sleep. But we can also interrupt a thread that is not currently suspended? – alfer Feb 26 '21 at 13:59
  • @alter, Yes. You can _always_ interrupt another thread. If the thread is not suspended, then the interrupt just sets the flag. An interruptible thread should periodically check the flag and take appropriate action if the flag is set. If an interrupted thread fails to clear the flag before calling a blocking function, then the function will _immediately_ throw the exception without ever blocking. – Solomon Slow Feb 26 '21 at 14:03
  • I have another question: can the OS suspend a thread whenever it wants (the thread could be at any point of execution of a method) or are there conditions? – alfer Mar 26 '21 at 18:55
  • 1
    @alfer, That depends what you mean by "suspend." If you are asking whether the OS can remove the thread from its CPU in order to give another thread a turn, then yes. In a _preemptive_ multitasking system (i.e., in most modern operating systems), the OS can do that without warning. But that thread is still in the RUNNABLE state--it's not waiting for anything except another turn on the CPU. Some people would say that "suspended" means _not_ RUNNABLE. They would say that "suspended" means that the thread is waiting for IO to complete or, waiting for some other thread to do something. – Solomon Slow Mar 27 '21 at 01:26
  • thank you. You say a thread can be in a RUNNABLE state yet be suspended. Does it help us as java programmers to know about thread states, or is it mostly background information to understand the internals? – alfer Mar 27 '21 at 11:08
  • @alfer, A Java program can ask any thread object for the [state of the thread](https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/lang/Thread.html#getState()). `RUNNABLE` means that there is no reason _that has any significance for the program_ why the thread should not be running. Java can't tell the difference between a RUNNABLE thread that actually is running, and a RUNNABLE thread that is waiting its turn to use a CPU. In most modern OSs, the scheduling of RUNNABLE threads on CPUs happens automatically, and there seldom is any reason for the programmer to think about it. – Solomon Slow Mar 27 '21 at 17:16
  • My _personal_ preference is to only say "suspended" when I mean, _not_ RUNNABLE. IMO, that makes the most sense from an application developer's point of view. A programmer who works in the OS kernal (or, one who wants to learn about operating systems) might have a different opinion of what "suspended" means. – Solomon Slow Mar 27 '21 at 17:19