1.

How will you terminate an alive thread?

Answer»

There is no direct way to terminate a thread but using the two FOLLOWING ways it can be done. 

  1. Calling ‘interrupt()’ method on the thread object (e.g. threadObj), this will throw an InterruptedException inside the run method of thread. This exception will indicate that we should release the resources GRACEFULLY and then exiting the ‘run’ method of thread will terminate the thread.
  2. In case if the InterruptedException is swallowed by the code or ignored as well as If inside the thread’s ‘run’ method if any LOOPING is there, if YES then ADDING a method check in that loop that if this thread should exit now or not, is done by checking ‘Thread.currentThread().isInterrupted()’ that returns true and then exit the run method.


Discussion

No Comment Found

Related InterviewSolutions