Exception and interrupt in thread in Java
What happens when an exception is thrown in a thread?
- If the exception is not caught, the thread "dies" (goes into the dead state).
- If an uncaught exception handler is installed, it will take over. Thread.UncaughtExceptionHandler is an interface, defined as a nested interface for other handlers that are called when a thread stops abruptly due to an uncaught exception. If the thread is about to stop due to an uncaught exception, the JVM checks it for the presence of UncaughtExceptionHandler using Thread.getUncaughtExceptionHandler(), and if it finds such a handler, it will call the uncaughtException() method on it, passing this thread and the exception as arguments ...
Difference between interrupted() and isInterrupted()
The Java thread termination mechanism is implemented using an internal flag known as the interrupt status. Interrupting the thread by calling Thread.interrupt() sets this flag. The Thread.interrupted() and isInterrupted() methods allow you to check if a thread is interrupted.
When an interrupted thread checks the interrupt status by calling the static method Thread.interrupted(), the interrupt status is cleared.
The non-static isInterrupted() method is used by one thread to check the interrupt status of another thread without changing the interrupt flag.
Read also:
- Race condition in Java
- Stopping thread in Java
- Keywords volatile, synchronized, transient, native in Java
Comments
Post a Comment