Which of the following can be used to stop a thread?
-
Thread.Interrupt()
-
Thread.Abort()
-
Both A) and B)
-
None of the Above
Thread.Interrupt() causes a thread to throw ThreadInterruptedException when it enters a wait state, while Thread.Abort() raises ThreadAbortException to terminate the thread. Both methods can stop thread execution, though Abort() is deprecated in modern .NET due to potential data corruption.
In .NET threading, Thread.Abort() forcibly terminates a thread by raising a ThreadAbortException in it, which (unless caught and suppressed) unwinds and ends the thread — a direct, if dangerous and now-deprecated, way to stop it. Thread.Interrupt() is subtly different: it only wakes a thread that is blocked in a wait/sleep/join state by throwing a ThreadInterruptedException at that point; if unhandled, this exception also terminates the thread, but Interrupt() does nothing to a thread that's actively running (non-blocked) code. Because both methods can, in the right circumstances, result in a thread ending, introductory-level material often lists "Both A and B" as usable ways to stop a thread, which is what's marked here.