Understanding exception hierarchy in Java And Thread Life Cycle, Thread States in Java
It starts with showing the Java class
structure for handling errors and exceptions. We will then look at three major
types of exceptions in Java which are direct sub-classes of
java.lang.Exception
,
java.lang.RuntimeException
and java.lang.Error
respectively,
along with examples for each type.Important classes of Java Exception Hierarchy
Above class diagram depicts the 4 fundamental classes which are at the heart of exception handling in Java –java.lang.Throwable
is at the root of Java’s exception hierarchy. All types of exception are descendants ofThrowable
.java.lang.Exception
is direct sub-class ofThrowable
.Exception
is the root class for all checked exceptions in Java.java.lang.RuntimeException
is direct sub-class ofException
.RuntimeException
is the root class for all unchecked exceptions in Java.java.lang.Error
is direct sub-class ofThrowable
and represents unrecoverable JVM errors.
Three main types of exceptions in Java
From a programming perspective you will rarely work with Throwable class. Rather, it is considered a good programming practice to avoid using Throwable directly in your code. The remaining three classes – Exception, RuntimeException and Error represent the three important types of exceptions in Java.
Let us take a look at the 3 main types of Exceptions
in Java-
- Exceptions – Also
known as checked exceptions, these are the sub-classes of
java.lang.Exception
class. Whenever a piece of code is likely to throw a checked exception, then either it must be caught using a try-catch block or the signature of the method containing such code should specify this exception in the throws clause.
Examples–FileNotFoundException
,IOException
. - Runtime Exceptions – Also
known as unchecked exceptions, these are the sub-classes
of
java.lang.RuntimeException
. These exceptions are specified for scenarios which neither need to be specifically caught using try-catch block nor do the methods which can throw them need to include these exceptions in the throws clause.
Examples–ArrayIndexOutOfBoundsException
,NullPointerException
. - Errors – Errors
are sub-classes of
java.lang.Error
class. These exceptions are thrown by JVM when it encounters severe issues such as if it runs out of memory and similar serious error conditions. These errors should not be caught and should be dealt with on priority as they may lead to significant glitches in deployed code.
Examples–OutOfMemoryError
,StackOverflowError
.
Summary
In this article we first looked at the exception hierarchy in Java described via its class diagram.This was followed by brief explanation of the important classes in the diagram. Finally, we looked the three main types of exceptions in Java – Exceptions, Runtime Exceptions and Errors and understood their individual purpose.
Java Thread Life Cycle
Let us start by getting a high level understanding of the 6 thread states in Java with the diagram shown next –
Let us start by getting a high level understanding of the 6 thread states in Java with the diagram shown next –
- New – A newly created thread object instance on which the start() method has not yet been invoked is in the new state. To learn how to instantiate threads in the proper way check out this
- Runnable – A thread in new state enters the runnable state when the Thread.start() method is invoked on it. There are 2 important points to note regarding the runnable state –
- Although the thread enters the runnable state immediately on invoking the start() method, but it is not necessary that the thread immediately starts executing. A thread runs when the logic it holds in its run() method can be executed by the processor. In case the thread logic needs any resource which is not available then the thread waits for the resource to become available.
- Secondly, a thread in runnable state may run for some time and then get blocked for a monitor lock, or enter the waiting/timed_waiting states as it waits for the opportunity/time to enter runnable state again.
- Blocked – A running thread may enter the blocked state as it waits for a monitor lock to be freed. It may also be blocked as it waits to reenter a monitor lock after being asked to wait using the Thread.wait() method.
- Waiting – A thread enters the waiting state when it is made to wait for a go-ahead signal to proceed. The go-ahead in this case is given by another thread and can be given in the following 3 scenarios –
- Thread waiting due to Thread.wait() method being called on it: The other thread can use Thread.notify() or Thread.notifyAll() to give the go-ahead to the waiting thread.
- Thread waiting as it itself has asked for joining another thread using Thread.join(): The waiting thread gets a go-ahead when the thread its waiting for ends.
- Thread waiting due to LockSupport.park()method being invoked on it: The waiting thread resumes when LockSupport.unPark() is called with the parked thread object as the parameter.
- Timed_Waiting – A thread which is waiting as it has been specifically ‘instructed’ to wait for a specified waiting time is in a timed_waiting state. A thread can be made to wait for a pre-determined amount of time in the following ways –
- Thread made to wait using Thread.sleep() method.
- Threads being asked to wait for a permit for a specified amount of time using LockSuport.parkNanos() and LockSupport.parkUntil() methods.
- Threads being made to wait for a fixed amount of time using Thread.wait(long millis) or Thread.join(long millis, int nanos).
- Terminated – A thread enters its ‘final resting’ state or terminated state when it has finished executing the logic specified in its run() method.
No comments:
Post a Comment